简体   繁体   English

打开/关闭cakephp中特定控制器的调试模式

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

I want to turn on the debug mode for particualr controller in cakephp . 我想打开cakephp中的particualr控制器的调试模式。 Now I am doing this in config/core.php ,it working fine . 现在我在config / core.php中这样做,它工作正常。 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. 它的实际安全性至关重要,可以像在core.php中那样做任何疯狂的事情,它必须始终为所有用户前端站点保持0。

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. 我在异地工作,所以我是IP上的唯一用户,当路由器决定反弹时,不得不继续更新IP,但这是一个很小的代价。

It does mean debug is on for all controllers but that's not a problem. 它确实意味着所有控制器都打开了调试,但这不是问题。

It work for me in cakephp 3.4 . 它在cakephp 3.4中适合我

Use the below code in top of your controller in cakephp 3+: 在cakephp 3+中使用控制器顶部的以下代码:

use Cake\Core\Configure;

Then your beforeFilter() code should be something like below: 然后你的beforeFilter()代码应该如下所示:

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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