Laravel 4 - 读取配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17132206/
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 4 - Read config files
提问by TheNiceGuy
How can i read the config files from laravel? For example for the database connection (app/config/database/.php)
如何从laravel读取配置文件?例如数据库连接(app/config/database/.php)
I want the mysql data from the config.
我想要配置中的 mysql 数据。
For a package you do it like that:
对于一个包,你这样做:
return Config::get('package::group.option');
But how to do it for the main files?
但是如何为主要文件做到这一点?
回答by gmaliar
I do Config::get('database.default')
for example.
我做Config::get('database.default')
的例子。
回答by Brendan White
To get the database connection parameters (server, username, password etc) if you're using MySQL, you can do this:
如果您使用 MySQL,要获取数据库连接参数(服务器、用户名、密码等),您可以执行以下操作:
echo "Driver: " . Config::get('database.connections.mysql.driver') . "<br/>\r\n";
echo "Host: " . Config::get('database.connections.mysql.host') . "<br/>\r\n";
echo "Database: " . Config::get('database.connections.mysql.database') . "<br/>\r\n";
echo "Username: " . Config::get('database.connections.mysql.username') . "<br/>\r\n";
echo "Password: " . Config::get('database.connections.mysql.password') . "<br/>\r\n";
On my local development machine this gives:
在我的本地开发机器上,这给出了:
Driver: mysql
Host: localhost
Database: local_dev_db
Username: root
Password: not-my-real-pwd
驱动程序:mysql
主机:本地主机
数据库:local_dev_db
用户名:root
密码:not-my-real-pwd
...obviously you should never show your password (or any of these other details) in your live app! But if it's just for your information on a local development machine you should be fine.
...显然,您永远不应该在您的实时应用程序中显示您的密码(或任何其他详细信息)!但如果只是为了您在本地开发机器上的信息,您应该没问题。
If you're not using MySQL, just replace mysql
with sqlite
or pgsql
or whatever.
如果您不使用 MySQL,只需替换mysql
为sqlite
或pgsql
或其他。