Lumen: print custom config return NULL

柔情痞子 提交于 2019-12-24 02:06:05

问题


I placed the configuration directory in the application root:

/config/app.php

Contents of /config/app.php:

<?php

return [
    'test' => 'this config should work',
];

In the /bootstrap/app.php:

$app->configure('app');

To test:

dd(config('app.test'));

Result is NULL :(. How can I fix it? I see that there is the same issue on github (https://github.com/laravel/lumen-framework/issues/107) but I have already updated my Lumen at the last version.


回答1:


I added following lines just before return $app; (this includes all files placed in config directory.

/*
|--------------------------------------------------------------------------
| Include all configs (by SK)
|--------------------------------------------------------------------------
|
| Here we include all php files that exists in config directory
|
*/
$files = scandir("config/");
if($files) {
    foreach($files as $file) {
        if(preg_match("@(\.php)$@",$file)) {
            // include app configs
            $app->configure($file);
        }
    }
}



回答2:


In your bootstrap.php file insert this

collect(scandir(__DIR__ . '/../config'))->each(function ($item) use 
($app) {
        $app->configure(basename($item, '.php'));
});


来源:https://stackoverflow.com/questions/35585897/lumen-print-custom-config-return-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!