Loading conditional component in CakePHP

I use the DebugKit component in my project, but I want to enable it only on the intermediate server and not load it when starting from the production server.

I know that I can disable it using the debug configuration value, but I want to keep this value at level 1 for both servers.

I tried to conditionally define the constant 'DEBUG_KIT' in bootstrap.php as the name of the component (for example, "DebugKit.Toolbar") or null. Then use this constant in the var $ variable definition at the top of the app_controller file. Well, Cake doesn't like to have zero in an array of components and barfs. I do not like the empty string.

It seems to me that I'm missing something, but I do not see the forest for the trees. Thanks in advance!

+1
source share
3 answers

Firstly, thanks to Adam Giles for the excellent answer. I did not think to look at the __construct () callback. It may be better than I found. And Daniel Wright, sir. I will most likely change my production server to 0 debugs in the near future and start looking at the error logs.

I found my answer shortly after posting this question. DebugKit has an “autoRun" parameter that turns it on and off. So, I first set the global constant in bootstrap.php as follows:

define( 'IS_DEV', ($_SERVER['SERVER_NAME'] == 'staging.example.com') ); 

Then in app_controller.php I use it to set the autoRun parameter in the $ components expression.

 var $components = array( 'DebugKit.Toolbar'=>array('autoRun'=>IS_DEV) ); 

It seems to be working so far.

+2
source

I am doing something similar in my applications: I would use the __construct method to detect the presence of DEBUG_KIT and add it to the $ components array. This function is called before processing the $ components array, so you can add / remove components transparently.

In your app_controller

 function __construct(){ if(DEBUG_KIT){ $this->components[] = 'DebugKit.Toolbar' } parent::__construct(); } 

If you have a _construct function in any or your individual controllers, be sure to include parent :: _ construct (); otherwise you will break the chain.

Hope this helps

+3
source

I think that the main goal of DebugKit is that it is in debug mode, so I understand that the tools do not provide the ability to disable without disabling debug mode.

However, if you absolutely decide this, I think it is best to change app/plugins/debugkit/controllers/components/toolbar.php by adding an existing debug mode check to ToolbarComponent::initialize with a check for your constant.

(For what it's worth, I think you'd better turn off debugging mode on your production server and use the errors / warnings logged in /app/tmp/logs/error.log to identify problems that passed testing).

+2
source

All Articles