laravel / lumen 访问中间件中的 .env 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36580497/
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
laravel / lumen access .env values in middleware
提问by Aaron
Is there any way to access .env vals from inside of a middleware script?
有没有办法从中间件脚本内部访问 .env vals?
I have tried to do so by env('KEY') but this seems to return null most of the time.
我曾尝试通过 env('KEY') 这样做,但这似乎在大多数情况下都返回 null。
Does any one know of a better way to do this inside of middleware or a way to insure the .env file has been loaded before the middleware runs?
有没有人知道在中间件内部执行此操作的更好方法或确保在中间件运行之前加载 .env 文件的方法?
回答by Alexey Mezenin
You can use config()
to access .env
variables. For example, if you want to get MySQL port, use this:
您可以使用config()
来访问.env
变量。例如,如果要获取 MySQL 端口,请使用以下命令:
$mysqlPort = config()['database']['connections']['mysql']['port'];
To get all available variables, you can do dd(config());
要获取所有可用变量,您可以执行 dd(config());
If you want to use custom variables in .env
, you also can do this:
如果要在 中使用自定义变量.env
,也可以这样做:
CUSTOM=hello
And to get this variable, use env()
helper:
要获取此变量,请使用env()
helper:
echo env('CUSTOM'); // Will output 'hello'