简体   繁体   中英

Turn on /off the debug mode for particular controller in cakephp

I want to turn on the debug mode for particualr controller in cakephp . Now I am doing this in config/core.php ,it working fine . But it is easy to enable /disable in controller ,we can avoid probelms with working in live sites ,otherwise the log will messed up users

its actually security critical to do anything wild like that in the core.php, it has to be and stay always 0 for ALL user frontend sites.

If you want to enable it for some admin backend action, you can do that inside the action at the very beginning with

Configure::write('debug', 2);

I'm late to the party on this one but just in case anyone else needs this

$skdebug = 0;
if ($_SERVER["REMOTE_ADDR"]== '121.75.33.244') $skdebug = 2;
Configure::write('debug', $skdebug);

I work offsite so I'm the only user on the IP, can be a pain to have to keep updating the IP when the router decides to bounce but it's a small price to pay.

It does mean debug is on for all controllers but that's not a problem.

It work for me in cakephp 3.4 .

Use the below code in top of your controller in cakephp 3+:

use Cake\Core\Configure;

Then your beforeFilter() code should be something like below:

public function beforeFilter(\Cake\Event\Event $event){
    parent::beforeFilter($event);
    $this->loadComponent('RequestHandler'); 

    // allow the function to public access
    $this->Auth->allow(['index','logout','register','saveOrders']);

    $actions = [
       'saveOrders','save-orders',
    ];

    // change the debug mode for a particular action
    if (in_array($this->request->params['action'], $actions)) {
       Configure::write('debug', false); // off debug mode
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM