Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 812
korokoro Avatar asked May 10 '15 13:05

korokoro


3 Answers

First you need add the file in the config folder for example: 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');

});
like image 128
Ali Mohammed Avatar answered Sep 30 '22 23:09

Ali Mohammed


Why not just use the env() helper within every configuration file you need?

You would just have to set the value in your .env file

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.

like image 33
Quetzy Garcia Avatar answered Oct 01 '22 00:10

Quetzy Garcia


We should also check the cache for custom config files added.

php artisan config:cache

Check this link

like image 24
Captain Sparrow Avatar answered Sep 30 '22 22:09

Captain Sparrow