php - How to share a variable across all views? -


i want information system locale available in every view, highlight whatever language selected user. after googling around, i've found value-sharing issue addressed @ official documentation. however, after putting code boot() this:

class appserviceprovider extends serviceprovider{     public function boot(){         view()->share('locale', \lang::getlocale());     } } 

the $locale variable, when accessed in views, holds default system locale, not currently selected one. why?

i use view composers it's more clear , readable.

for example if want share variable main navbar of views follow below rules:

1. create new service provider

you can create service provider artisan cli:

php artisan make:provider viewcomposerserviceprovider

in viewcomposerserviceprovider file create composenavigation method in has blade template main.nav-menu represents navmenu shared variables.

the viewcomposerserviceprovider looks like:

<?php  namespace app\providers;  use illuminate\support\serviceprovider;  class viewcomposerserviceprovider extends serviceprovider {     /**      * bootstrap application services.      *      * @return void      */     public function boot()     {         $this->composenavigation();     }      /**      * register application services.      *      * @return void      */     public function register()     {         //     }      private function composenavigation()     {         view()->composer('main.nav-menu', 'app\http\viewcomposers\navcomposer');     } } 

2. create composer

as saw in file above have app\http\viewcomposers\navcomposer.php let's create file. create folder viewcomposers in app\http , inside create navcomposer.php file.

the navcomposer.php file:

<?php  namespace app\http\viewcomposers;  use app\repositories\navmenurepository; use illuminate\view\view;  class navcomposer {     protected $menu;      public function __construct(navmenurepository $menu)     {         $this->menu = $menu;     }      public function compose(view $view)     {         $thing= $this->menu->thing();         $somethingelse = $this->menu->somethingelseformydatabase();          $view->with(compact('thing', 'somethingelse'));     } } 

3. create repository

as saw above in navcomposer.php file have repository. usually, create repository in app directory, create repositories directory in app , then, create inside navmenurepository.php file.

this file heart of design pattern. in file have take value of our variables want share of our views.

take @ file bellow:

<?php  namespace app\repositories;  use app\thing; use db;  class navmenurepository {      public function thing()     {         $getvarforsharewithallviews = thing::where('name','something')->firstorfail();         return $getvarforsharewithallviews;     }      public function somethingelseformydatabase()     {         $getsomethingtomyviews = db::table('table')->select('name', 'something')->get();          return $getsomethingtomyviews;     }  } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -