Laravel 5 - 配置子文件夹

8
在Laravel 5中,我们可以通过以下方式读取配置值:
config('app.url')

但是,我们如何在子文件夹中读取配置文件的值?我尝试使用config('subfolder/app.url'),但它没有起作用。


1
我从未见过那种结构,请尝试仍使用点符号表示法。 - manix
4个回答

9
在Laravel 5中,您可以这样做:config('subfolder.myfile.var') ;subfolder是文件夹名称,myfile是您的文件名,var是您的变量。

3
我已经创建了一个文件 config/local/app.php 包含以下内容: <?php return [ "key" => "value" ]; 使用 Artisan 命令,我得到了如下输出结果: enter image description here

2
config目录下创建名为subfolder的文件夹,其中包含一个名为app.php的文件。
在app.php中放置所有的常量。
return array(
        'APP_URL'  => 'http://www.test.com/xyz',
        'APP_MAX_FOLDER_SIZE' => 1000000000,
);

并且可以像访问一样进行访问

config('subfolder.app.APP_URL');

建议使用大写字母创建常量,并创建有意义的文件名,例如constans.phpappconstants.php等。


2
数组键不是常量。 - Klaaz

0
针对Lumen开发人员
如果有需要的话,这是我能够做到的方法:
步骤1 - 在config/下创建子目录,例如validator 步骤2 - 创建配置文件:messages.php、rules.php
config
└── validator
    ├── messages.php
    └── rules.php

步骤3 - 在bootsrap/app.php下启用配置文件

/*
|--------------------------------------------------------------------------
| Register Custom Configuration
|--------------------------------------------------------------------------
|
| Register custom configuration files that are under config/
|
*/
$app->configure('validator/messages');
$app->configure('validator/rules');

/**/
$app->router->group([
    'namespace' => 'App\Http\Controllers',
], function ($router) {
    require __DIR__.'/../routes/web.php';
});

return $app;

步骤4 - 获取配置

config('validator/rules.key');

参考:https://laracasts.com/discuss/channels/lumen/config-in-subdirectory


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接