在 Laravel 4 中在运行时设置 debug => true 时出现问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19204539/
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-09-14 08:31:31  来源:igfitidea点击:

Problems setting debug => true at runtime in Laravel 4

phplaravellaravel-4

提问by Holly

In Laravel 4, I want to set the configuration variable "debug" to "false" by default, but to "true" for requests from my own IP address (the test will eventually be more sophisticated than that).

在 Laravel 4 中,我想将配置变量“debug”默认设置为“false”,但对于来自我自己的 IP 地址的请求,我想设置为“true”(测试最终会比这更复杂)。

Based on the documentation at http://four.laravel.com/docs/configuration, I've tried the following:

根据http://four.laravel.com/docs/configuration上的文档,我尝试了以下操作:

config/app.php:

配置/app.php:

'debug' => false

filters.app - App::before (I also tried putting the code at the top of routes.php, with the same effect):

filters.app - App::before(我也试过把代码放在routes.php的顶部,效果一样):

if(Request::getClientIp() == '[my ip address]') {
  echo 'hello world';
  Config::set('app.debug', true);
}
echo Config::get('app.debug');

When I visit a bad URL I see "hello world" and "1", so that's good, but then just the public (non-debug) error message displays below that.

当我访问错误的 URL 时,我会看到“hello world”和“1”,这很好,但是只有公共(非调试)错误消息显示在其下方。

I understand config variables set at run-time are only for a single request, but since my "hello world" is displaying, it seems like this is a single request.

我知道在运行时设置的配置变量仅适用于单个请求,但是由于我的“hello world”正在显示,因此这似乎是单个请求。

Is there a better place to put my code, or is what I'm doing not actually possible?

有没有更好的地方来放置我的代码,或者我所做的实际上是不可能的?

回答by Adrien Delessert

This seems like something you could accomplish with Laravel's environment detection (from the page you linked: http://four.laravel.com/docs/configuration#environment-configuration).

这似乎是您可以使用 Laravel 的环境检测完成的事情(来自您链接的页面:http://four.laravel.com/docs/configuration#environment-configuration )。

Using environments, you decide on a name—you could call yours debugging—then you put config files for that environment in a subdirectory of your config directory. So in the case you gave above, you'd copy the app.phpconfig file to /app/config/debugging/app.php(and any others you wanted to), and make the necessary configuration changes.

使用环境,你决定一个名字——你可以叫你的名字debugging——然后你把那个环境的配置文件放在你的 config 目录的子目录中。因此,在您上面给出的情况下,您可以将app.php配置文件复制到/app/config/debugging/app.php(以及您想要的任何其他文件),并进行必要的配置更改。

Then the important step is to detect the environment so that your app will use the debuggingconfig files. This is specified in /bootstrap/start.php. You can pass a closure instead of an array to Laravel's environment detection, and return the environment name you want to use, so you could use your approach from above like so:

然后重要的一步是检测环境,以便您的应用程序将使用debugging配置文件。这在/bootstrap/start.php. 您可以将闭包而不是数组传递给 Laravel 的环境检测,并返回您想要使用的环境名称,因此您可以使用上面的方法,如下所示:

$env = $app->detectEnvironment(function()
{
    if ($_SERVER['REMOTE_ADDR'] === '[my ip address]') {
        return 'debugging';
    }
});

I haven't tested it, but it should work. Hope that helps!

我还没有测试过,但它应该可以工作。希望有帮助!

回答by Sleavely

After some digging into how the exception handler I've found this to be reliable:

在深入研究异常处理程序的方式后,我发现这是可靠的:

  Config::set('app.debug', true);
  app()->startExceptionHandling();

Why this approach? Because Laravel binds the exception handler at startup (to avoid PHPs internal one ruining things) you'll need to fake a reboot of that part.

为什么采用这种方法?因为 Laravel 在启动时绑定异常处理程序(为了避免 PHP 内部的破坏),您需要假装重新启动该部分。

PS. I'm reviving this old question because I don't think the accepted answeris accurate. It does not allow you to enable debugging at runtime, and I cannot detect whether the user is an employee by looking at system variables.

附注。我正在重温这个老问题,因为我认为接受的答案不准确。它不允许您在运行时启用调试,而且我无法通过查看系统变量来检测用户是否是员工。