Laravel 4访问环境变量 [英] Laravel 4 accessing environment variables

查看:93
本文介绍了Laravel 4访问环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问Laravel 4项目中的系统变量.类似于使用ENV['VARIABLE_NAME']访问RoR项目中的系统变量.

I am trying to access an system variable within my Laravel 4 project. Similar to using ENV['VARIABLE_NAME'] to access a system variable in an RoR project.

在我的Laravel代码中,getenv('VARIABLE_NAME')返回一个空字符串.但是,我可以在命令提示符下使用php -r "echo getenv('VARIABLE_NAME')"访问此变量并将其打印到屏幕上.

Within my Laravel code getenv('VARIABLE_NAME') returns an empty string. However, I can access and print this variable to the screen using php -r "echo getenv('VARIABLE_NAME')" at the command prompt.

php -i确认此变量也存储在php的$_SERVER超全局变量中.但是,尝试从我的项目的database.php文件访问$_SERVER['VARIABLE_NAME']会导致Undefined index: VARIABLE_NAME错误.

php -i confirmed that this variable is also stored in php's $_SERVER superglobal. However, attempting to access $_SERVER['VARIABLE_NAME'] from the database.php file of my project results in an Undefined index: VARIABLE_NAME error.

出于某种原因(例如,可能存在潜在的安全问题),我不能从php访问任意系统变量吗?如果是这种情况,如何将所需的系统变量公开给Laravel 4项目?

Can I not access arbitrary system variables from php for some reason (e.g., potential security issue, perhaps) ? If this is the case, how can I expose the system variable I need to my Laravel 4 project?

如果配置很重要,我将使用php5-fpm和nginx在Ubuntu 13.04 Server上安装我的PHP. PHP版本是5.5.

If configuration matters, I'm using php5-fpm and nginx to serve up my PHP on Ubuntu 13.04 Server. PHP version is 5.5.

推荐答案

在与之抗争的时间超过了必要的时间之后,正确的方法是使用php5的pool.d目录中的php5-fpm www.conf文件-fpm安装.对我来说是在/etc/php5/fpm/pool.d/www.conf.

After fighting with this for much longer than necessary, the proper approach is to use the php5-fpm www.conf file in the pool.d directory of your php5-fpm install. For me this was at /etc/php5/fpm/pool.d/www.conf.

www.conf中,文件的特定部分列出了使用以下语法的几个环境变量:

In www.conf there is a specific section of the file that lists several environment variables with the following syntax:

env[VARNAME] = $ENV_VAR_NAME

因此,只需添加自己的变量,然后您就可以使用以下命令在Laravel应用中访问这些变量

So just add your own and then you can then access these variables in your Laravel app with

getenv('VARNAME')

像冠军一样工作.

这篇关于Laravel 4访问环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆