See improved answer in Update 2
I would add for this special service provider. It will read all your settings stored in the database and add them to the Laravels configuration. Thus, for parameters, there is only one database query, and you can access the configuration in all controllers and views as follows:
config('settings.facebook');
Step 1. Create a service provider.
You can create a service provider using the wizard:
php artisan make:provider SettingsServiceProvider
This will create the app/Providers/SettingsServiceProvider.php file.
Step 2. Add this to the download method of the just created provider:
public function boot() {
From the Laravel docs:
[Download method] is called after registration of all other service providers, which means that you have access to all other services that have been registered by the infrastructure.
http://laravel.com/docs/5.1/providers#the-boot-method
Step 3. Register the provider in your application.
Add this line to the providers array in config/app.php :
App\Providers\SettingsServiceProvider::class,
What is it. Happy coding!
Update: I want to add that the boot method supports dependency injection. Therefore, instead of hard coding \App\Setting you can add a repository / interface bound to the repository, which is great for testing.
Update 2: As indicated by Jeemusu in his comment , the application will query the database for each request. To prevent this, you can cache the settings. There are basically two ways you can do this.
To make it more fault tolerant, I would use the second option. Caches may be deleted unintentionally. The first option will not work with new installations unless the administrator has set the settings or reinstalled after a server failure.
For the second option, change the method of loading Service providers:
public function boot(Factory $cache, Setting $settings) { $settings = $cache->remember('settings', 60, function() use ($settings) {
Now you only need to make a cache to forget the settings key after the administrator updates the settings:
public function update($id, Factory $cache) {