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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9462231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:52:48  来源:igfitidea点击:

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

phpcakephp

提问by Rinto George

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

我想在 cakephp 中打开特定控制器的调试模式。现在我在 config/core.php 中这样做,它工作正常。但是在控制器中启用/禁用很容易,我们可以避免在现场工作时出现问题,否则日志会弄乱用户

回答by mark

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);

回答by Sarah King

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.

这确实意味着对所有控制器进行调试,但这不是问题。

回答by kantsverma

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