php 如何在子文件夹中的laravel中获取配置数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17532449/
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
How to get config data in laravel in a subfolder
提问by Sergio Magoo Quintero
I have the Laravel Administrator Pluginand I set up a setting administrator page that is located in:
我有Laravel 管理员插件,我设置了一个位于以下位置的设置管理员页面:
app/config/administrator/settings/site.php
应用程序/配置/管理员/设置/site.php
The application can store the data, but when i try to get some data in one of my controllers like this:
该应用程序可以存储数据,但是当我尝试在我的一个控制器中获取一些数据时,如下所示:
Config::get('administrator.settings.site')
Config::get('administrator.settings.site')
I can get a returned value... Always null or array 0.
我可以得到一个返回值...始终为 null 或数组 0。
How I can get those configuration values?
我如何获得这些配置值?
回答by fideloper
Solution:
解决方案:
You can use a file path rather than dot notation for file paths:
您可以对文件路径使用文件路径而不是点符号:
Config::get('administrator/settings/site.variable_name');
Some Explanation
一些解释
Dot notation is used to get variables inside the config array within a file, rather than to denote file paths.
点符号用于获取文件中 config 数组内的变量,而不是表示文件路径。
Therefore you can use Config::get('administrator/settings/site.variable_name');
to get $variable_name from administrator/settings/site.php
, or ENV_DIR_NAME/administrator/settings/site.php
depending on your environment.
因此,您可以使用Config::get('administrator/settings/site.variable_name');
从administrator/settings/site.php
或ENV_DIR_NAME/administrator/settings/site.php
根据您的环境获取 $variable_name 。
Directories within in app/config
are read as environments. For example app/development/database.php
will work with Config::get('database.whatever')
when you're in your "development" environment while app/production/database.php
will be read with Config::get('database.whatever')
when you're in the "production" environment.
inapp/config
中的目录被视为环境。例如app/development/database.php
将与Config::get('database.whatever')
当你在你的“发展”的环境,同时app/production/database.php
将被读取Config::get('database.whatever')
,当你在“生产”环境。
Laravel falls back to the config/config/*.php
file when no environment-specific file is present.
config/config/*.php
当不存在特定于环境的文件时,Laravel 会回退到该文件。
Note
笔记
I believe that admin package has a config filein app/config/packages/frozennode/administrator/administrator.php
which the package would like you to use for it's settings. That should be available using this convention: Config::get('package::file.option');
我相信 admin 包有一个配置文件app/config/packages/frozennode/administrator/administrator.php
,该包希望您在其中进行设置。使用此约定应该可以使用:Config::get('package::file.option');
回答by Matthew
I ran into a very similar problem when using this Laravel-Excelpackage.
我在使用这个Laravel-Excel包时遇到了一个非常相似的问题。
The solution I needed was slightly different to the solution above. I needed to take advantage of Laravel's config overriding feature to have one config setting for normal execution and a different one for testing. Specifically, I normally wanted Excel files to be stored in storage/excel
and I wanted them to be put in storage/testing/excel
under testing.
我需要的解决方案与上面的解决方案略有不同。我需要利用 Laravel 的配置覆盖功能来拥有一个用于正常执行的配置设置和另一个用于测试的配置设置。具体来说,我通常希望将 Excel 文件存储在其中,storage/excel
并希望将它们放入storage/testing/excel
测试中。
The technique of using a slashed path didn't work because it points to a specific file so didn't respect the different environments:
使用斜线路径的技术不起作用,因为它指向特定文件,因此不尊重不同的环境:
$directory = Config::get('packages/maatwebsite/excel/export.store.path');
The solution was to use a package prefix so that the config loader would respect the environment:
解决方案是使用包前缀,以便配置加载器尊重环境:
$directory = Config::get('excel::export.store.path');
I'm not exactly sure where the excel
shorthand name comes from but I suspect it's something to do with the Excel facadein the package exposing itself as 'excel'
.
我不确定excel
速记名称的来源,但我怀疑这与包中的 Excel 外观有关,将自身暴露为'excel'
.