How to use a variable in all views?

I want the locale information of the system to be available in each view, so I could highlight any language that the user currently selected. After some searching on Google, I found the cost-sharing issue discussed in the official documentation . However, after entering the code in boot() as follows:

 class AppServiceProvider extends ServiceProvider{ public function boot(){ view()->share('locale', \Lang::getLocale()); } } 

the $locale variable, when accessing views, always contains the default $locale , not the currently selected one. Why?

+9
php laravel-5
Jan 27 '16 at
source share
2 answers

I usually use View Composers to make it more understandable and understandable.

For example, if I want to share a variable with the main navigation bar to all my views, I follow the rules below:

1. Create a new service provider

You can create a service provider using artisan cli:

php artisan make:provider ViewComposerServiceProvider

In the ViewComposerServiceProvider file, create the composeNavigation method, which has the main menu of the blade template main.nav, which represents the navigation menu with common variables.

ViewComposerServiceProvider looks like this:

 <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class ViewComposerServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { $this->composeNavigation(); } /** * Register the application services. * * @return void */ public function register() { // } private function composeNavigation() { view()->composer('main.nav-menu', 'App\Http\ViewComposers\NavComposer'); } } 

2. Create a composer

As you saw in the above file, we have App \ Http \ ViewComposers \ NavComposer.php, so let's create this file. Create the ViewComposers folder in App \ Http, and then create the NavComposer.php file.

File NavComposer.php:

 <?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 a repository

As you saw above in the NavComposer.php file, we have a repository. Usually I create a repository in the application directory, so I create the repository directory in the application, and then create the NavMenuRepository.php file.

This file is the heart of this design pattern. In this file, we must take the value of our variables, which we want to share with all our representations.

Take a look at the file below:

 <?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; } } 
+16
Jan 27 '16 at 16:08
source share

For people with a small project:

First, the accepted answer is awesome!

For Laravel 5.2 users:

Just use the new @inject blade @inject in your views like this

@inject('shared','App\Utilities\SharedWithView')

then you can use it: {{ $shared->functionName() }}

And SharedWithView is a simple class like this:

 namespace App\Utilities; use App\Repositories\SomeRepositoryLikeArticlesRepository; class SharedWithView { public function functionName() { $properNameHere = new SomeRepositoryLikeArticlesRepository(); return $properNameHere->forEaxmpleGetMostViewedArticles( 10 ); } } 
+2
Mar 03 '16 at 9:04 on
source share



All Articles