Last active
February 10, 2017 08:08
-
-
Save SabrinaMarkon/db921d31e871c1852d6bb3f1a0eb0ce4 to your computer and use it in GitHub Desktop.
Sabrina Notes - Laravel 5.3 - Make site settings from database model available to all controllers and views using a custom service provider.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Use in your blade templates like: | |
{{ $your_variable_name }} | |
OR: | |
{{ $settings['your_variable_name'] }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Use like the below in your controller classes: | |
config('your_variable_name') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace App\Providers; | |
use Illuminate\Support\ServiceProvider; | |
// change to your settings model path below: | |
use App\Models\Setting; | |
use View; | |
class SiteSettingsServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap the application services. | |
* | |
* Provides site settings to all views and controllers | |
* Need to add to config/app.php in the Application Service Providers section: | |
* App\Providers\SiteSettingsServiceProvider::class, | |
* | |
* @author Sabrina Markon, PHPSiteScripts.com 2017 | |
* @return void | |
*/ | |
public function boot() | |
{ | |
// change to your settings model name below (instead of "Setting" if yours is different) | |
$settings = Setting::pluck('setting', 'name'); | |
// provides site settings to all views: | |
// if we need to use all settings variables ie. passing just $settings in parameter | |
// instead of individual variables like $sitename, $domain, etc. | |
View::share('settings', $settings); | |
// provides site settings to all controllers: | |
config()->set('settings', $settings); | |
foreach ($settings as $key => $val) { | |
// provides individual site settings variables to all views: | |
View::share( $key, $val ); | |
// provides individual site settings variables to all controllers: | |
config()->set($key, $val); | |
} | |
} | |
/** | |
* Register the application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
// | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment