Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

php - Laravel 4 accessing environment variables

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.

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 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.

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?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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

getenv('VARNAME')

Works like a champ.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...