I created custom config file in Laravel 5 and try to use settings from it in other files (session.phpcache.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?

 

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');

});
 
   
I have no problem to get settings from my custom configs in any part of application except standard laravel config files. Try to access your 'Test.name' from 'config/cache.php'. – korokoro May 10 '15 at 17:22
   
that true sorry for misunderstanding the question I'am not sure but i think you can not call config inside another config file as you said It seems that config files have predefined loading order. – Ali Mohammed May 10 '15 at 17:41

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.

 
1 
Config file is the main place to store settings of application and .env file is for correct some settings if necessary. Application should work properly without any .env file. To guarantee app works without .envfile using your approach I should edit every occurrence of 'some default value' in configs instead edit only config/custom-config.php. Yes, it isn't hard, but I think it isnt't the best way. – korokoro May 11 '15 at 8:44
   
To achieve what you want, there's no other way I know of. Also, what's the point of assigning a value from a config file to a different config file value? You say my suggestion isn't the best way, yet what you ask is completely redundant! Care to explain what's the reasoning behind what you want? Otherwise, what I suggested is the most correct way you'll probably get. – Quetzy Garcia May 11 '15 at 9:27
   
No, it is not redundant for me :) For example, I have application name value in custom config and I want to generate session file name or cache prefix according this value. – korokoro May 11 '15 at 9:53
   
If the application name is gonna change all the time, you might as well use .env files, in which case you would just do something like 'files' => storage_path().'/framework/sessions/'.env('APP_NAME'),. If the application name isn't gonna change, just pass the value directly: 'files' => storage_path().'/framework/sessions/app_name',.– Quetzy Garcia May 11 '15 at 10:16

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

php artisan config:cache

Check this link

 

来自  https://stackoverflow.com/questions/30152032/custom-config-file-access-within-laravel-5-config-files


 

how to add custom config ?

PUBLISHED 2 YEARS AGO BY GANTODAY

i want to controller some configs in my site dashboard
such as how many items per page,name of site...
i wish could controller them in my site dashboard,not to modify files or change values in database manually.
how to realize it? please help me,thanks!

Best Answer(As Selected By gantoday)
bobbybouwmann

So you basically want a variable that you can always access? That's not what you asked in the beginning! Take a look at view composers: http://laravel.com/docs/5.0/views#view-composers

bobbybouwmann

You can create a CRUD environment for settings or use a settings while which you have to modify though.

What have you tried?

 
gantoday

@blackbird
sorry,i'm a newbie,and i don't know what you means.could you give me some example? many thanks!

 
bobbybouwmann

Well you want to change some settings right? Where do you want to change that? What have you tried? What do you have now that needs to be changed?

 
gantoday

@blackbird
i want to change something in my site dashboard. i mean controller some setting in my dashboard.
i hope could controller such as site name,how many items per page shows in my dashboard.
i have tried stored settings in my database,but every method use setting values should include
Setting::all();
and than compact it to views.
i hope could use my custom config as laravel's build in config.stored a custom config file in config directory and get setting values use
Config::get('xxx');
forgive me poor english.

 
bobbybouwmann

You can just create a new file in the config folder and use that

config/settings.php

<?php 

return  [
 
    'pages' => 5

];
Config::get('settings.pages');
 
gantoday

@blackbird
wow,many thanks.
and could you tell me what is the best way to realize site setting ?
does other users of laravel use this way to realize controller settings in site dashboard?
maybe sometimes files in config directory couldn't change for secure purpose.

 
bobbybouwmann

You can actually do this in your controller and output it on the page. Just a simple example:

class MyController extends Controller {

    public function index()
    {
        $settings = Config::all();
        return view('settings.index', compact('settings'));
    }

}

In your view

@foreach ($settings as $settingsarray)

    <ul>

    @foreach($settingsarray as $setting)
        
        <li>{{ var_dump($setting) }}</li>

    @endforeach

    </ul>

@endforeach
 
bobbybouwmann

Most developers would store the settings in the database. So you would create a new controller called SettingsController and a Setting model to do this. Then your controller would get all the data from the database and display it on the page ;)

 
gantoday

@blackbird
you are good man. └(^o^)┘

 
bobbybouwmann

If you need any help, you know where to find me ;)

 
zeeshan

Controller

namespace App\Http\Controllers;

use App\Http\Controllers\Controller; use App\Setting; use App\Http\Requests\SettingRequest;

class SettingsController extends Controller {

public function index() {

    $settings = Setting::oldest("name")->paginate($this->pagesize);
    return view("admin.settings.index")->with('settings', $settings);
}

public function create() {
    return view("admin.settings.create");
}

public function store(SettingRequest $request) {

    Setting::create($request->all());
    return redirect("admin/setup/settings")->with(['success' => 'New Setting created successfully.']);
}

public function edit($id) {

    $setting = Setting::findOrFail($id);

    return view("admin.settings.edit")
                    ->with('setting', $setting);
}

public function update($id, SettingRequest $request) {

    $setting = Setting::findOrFail($id);
    $setting->update($request->all());
    return redirect("admin/setup/settings")->with(['success' => 'Setting updated successfully.']);
}

public function destroy($id) {

    $setting = Setting::findOrFail($id);
    $setting->delete();
    return redirect("admin/setup/settings")->with(['success' => 'Setting deleted successfully.']);
}

}

Model
namespace App;

use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes;

class Setting extends Model {

use SoftDeletes;

protected $fillable = [
    'name',
    'value'
];
protected $dates = ['deleted_at'];

}

Request
namespace App\Http\Requests;

use App\Http\Requests\Request;

class SettingRequest extends Request {

public function authorize() {
    return true;
}

public function rules() {

    return [
        'name' => 'required',
        'value' => 'required'
    ];
}

}

Unfortunately I can not paste the blade templates here. I hope you would be able to create the form etc.

 
bobbybouwmann

How is this gonna help ganto? I bet he/she doesn't know what a Request object is.. You add paginate, but does he/she really need it?

 
gantoday

@zeeshan
thank you very much.
but if i use SettingsController to setting name of the site,i should add $settings=Setting::all(); and compact('$setting') every times i use views.
does there has easy way to do that?

 
zeeshan

@blackbird thats why I added whole classes which include the namespace (which may help him where to copy) and also has all the the use statements so he does not get confused.

Thats my way of sending help. If he does not understand he will come back to here and ask. if its not helpful to him he may not use it. Simple.

 
bobbybouwmann

@ganto you can also do it like this:

return view('settings.index')->with('settings' => Setting::all());

or

return view('settings.index')->withSettings(Setting::all());