【问题标题】:Can i access env variables in Exception Handler我可以在异常处理程序中访问环境变量吗
【发布时间】:2019-10-17 06:04:34
【问题描述】:

我需要在Exception/Handler.php 中获取.env 文件中的常量,

我已经更改了我的 Handle.php

    public function render($request, Exception $exception)
{
    if (env('APP_DEBUG')) {
        return parent::render($request, $exception);
    } else {
        return response(view('error_custom')->render(),200);
    }
}

env('APP_DEBUG') 返回 null,有什么想法吗?

【问题讨论】:

  • 应该可以的。但是,建议使用 config 值,因为它们会被缓存。 config('app.debug')
  • 你使用的是哪个 laravel 版本?那应该可以正常工作。

标签: php laravel null environment-variables


【解决方案1】:
【解决方案2】:
    As you know you have debug variable in env something like the below:
    'debug' => env('APP_DEBUG', false);
    Laravel 5+:
    then you can access it like $debug = config('app.debug');
    Laravel 4.2:
    $debug = Config::get('app.debug');
    but before going further just check you get the value for it, if you have set value of APP_DEBUG as false in env then you will get 0 but you can't see it while printing so create a condition such as
    if (config('app.debug')) {
      echo "Yes";
    } else {
      echo "No";
    }

once you get the output you are ready to go further.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-04
    • 2013-09-22
    • 2015-03-22
    • 1970-01-01
    • 2011-05-25
    • 2018-02-05
    • 2021-12-30
    • 1970-01-01
    相关资源
    最近更新 更多