2015-08-24 14 views
10

如何从我的控制器访问app/config/app.php中的调试变量以查看我是否处于调试模式?laravel如何阅读app/config/app.php调试变量

<?php                             
                                | 
    /*                             |  if ($this->user->id) 
    |--------------------------------------------------------------------------          |  { 
    | Application Debug Mode                       |   // Save roles. Handles updating. 
    |--------------------------------------------------------------------------          |   $this->user->saveRoles(Input::get('roles')); 
    |                             | 
    | When your application is in debug mode, detailed error messages with            |   // Redirect to the new user page 
    | stack traces will be shown on every error that occurs within your            |   return Redirect::to('admin/users/'.$this->user->id)->with('success', Lang::get('admin/users/messages.cre 
    | application. If disabled, a simple generic error page is shown.             |ate.success')); 
    |                             |  } 
    */                             | else 
                                | { 
    'debug' => true,  

回答

21

您可以使用辅助函数config(Laravel 5+)。

$debug = config('app.debug'); 

Laravel 4.2:

$debug = Config::get('app.debug'); 
+0

我得到undefined fu nction config()...我在Laravel 4.2上。 – Phil

+0

哦,谢谢!我现在在这里看到它http://laravel.com/docs/4.2/configuration – Phil

3

这个问题已经有了正确的答案,我只是想补充一点,与环境变量做是一个更好的选择:

'debug' => env('APP_DEBUG', false), 

在.ENV file:

APP_ENV=local 
+0

我看到了一些建议来做到这一点,但我不知道为什么这会更好。它看起来比使用现有变量更加复杂和混乱。你能解释一下吗? – Phil

+1

主要是因为你可以将.env文件添加到.gitignore文件,这样你就可以拥有你的机器特​​定的用户名和密码,而不必将它分享给其他版本控制系统,而需要提交app.php文件,公开分享。 – Shota

+1

@Phil它也更好,因为你应该在你的.env文件中存储所有的证书和其他常量。您会注意到,您的所有配置设置都像app.php或database.php中的... => env('whatever','')。 – baao