1

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

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

config/app.php:

'debug' => false

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

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.

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.

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

2 Answers 2

4

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

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.php config file to /app/config/debugging/app.php (and any others you wanted to), and make the necessary configuration changes.

Then the important step is to detect the environment so that your app will use the debugging config 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:

$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!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Your code sample produces the error "Fatal error: Class 'Request' not found in /home/hp/bootstrap/start.php" but that was easily solved by replacing Request::getClientIp() with $_SERVER['REMOTE_ADDR']. So it's working well with the specific case in my question. But ultimately I was planning to use a Auth-based test, and the error indicates that won't work since the class isn't loaded yet. So if you happen to have any ideas for showing the debug errors only to certain logged-in users that would be great, but I recognize that wasn't my original question.
Yeah, if the Auth class hasn't loaded at that point you may need to get creative. What if you were to drop a cookie for authorized users who you want to be able to see the debugging information? The $_COOKIE superglobal will be available even if no Laravel classes have loaded yet. It'd be advisable to set & check the value of the cookie to something like a hash of the user's IP + user agent string + a salt, to help make sure that nobody else can snoop your debugging pages.
@Holly - You might also want to consider this answer to a similar requirement - stackoverflow.com/questions/22588266/…
2

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.

PS. I'm reviving this old question because I don't think the accepted answer is 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.

1 Comment

Config::set('app.debug', true); was enough for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.