简体   繁体   English

Laravel 5配置文件中的自定义配置文件访问

[英]Custom config file access within Laravel 5 config files

I created custom config file in Laravel 5 and try to use settings from it in other files ( session.php , cache.php ) by calling config('myconfigfile.value') , but there are no values returned from my config. 我在Laravel 5中创建了自定义配置文件,并尝试通过调用config('myconfigfile.value')在其他文件( session.phpcache.php )中使用它的设置,但是我的配置没有返回任何值。 It seems that config files have predefined loading order and custom configs is loading in the end or it didn't initialized else by other cause. 似乎配置文件具有预定义的加载顺序,并且自定义配置最终加载或者其他原因未初始化。

How can I access to my settings from laravel config files? 如何从laravel配置文件访问我的设置?

First you need add the file in the config folder for example: laravel\\config\\Test.php 首先,您需要在config文件夹中添加文件,例如:laravel \\ config \\ Test.php

<?php
use Illuminate\Support\Facades\Config;


return [

    'name' => 'Ali Mohammed',
    'age' => 26

];

then you need to call the config 然后你需要调用配置

get('test', function(){

   return  Config::get('Test.name');

});

Why not just use the env() helper within every configuration file you need? 为什么不在您需要的每个配置文件中使用env()帮助器?

You would just have to set the value in your .env file 您只需在.env文件中设置值.env

CUSTOM_SETTING="some value"

and get it on each configuration file 并在每个配置文件上获取它

<?php
return [
    // ...
    'custom_value' => env('CUSTOM_SETTING', 'some default value'),
    // ...
];

Have a look at the helper code . 看一下帮助代码

We should also check the cache for custom config files added. 我们还应该检查缓存中添加的自定义配置文件。

php artisan config:cache

Check this link 检查此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM