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

查看:30
本文介绍了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 服务器上提供我的 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-fpm www.conf 文件中的pool.d php5-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天全站免登陆