Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 - global Blade view variable available in all templates

How can I in Laravel 5 make global variable which will be available in all Blade templates?

like image 768
Limon Monte Avatar asked Apr 18 '15 10:04

Limon Monte


People also ask

How to define global variables for Blade templates in Laravel 9?

This tutorial shows how to define global variables for Blade templates in Laravel 9 application. The View facade has share method which allows to inject a variable to each Blade template. Create a new service provider and call the share method within boot method. Register the service provider in the config/app.php file.

What is Laravel blade?

Blade is the simple, yet powerful templating engine that is included with Laravel. Unlike some PHP templating engines, Blade does not restrict you from using plain PHP code in your templates.

Should the same variable be passed to all blade templates?

There might be a case that the same variable should be passed to all Blade templates. It can done on each controller. However, it is not perfect solution. This tutorial shows how to define global variables for Blade templates in Laravel 9 application.

Where are blade template files stored?

Blade template files use the .blade.phpfile extension and are typically stored in the resources/viewsdirectory. Blade views may be returned from routes or controller using the global viewhelper. Of course, as mentioned in the documentation on views, data may be passed to the Blade view using the viewhelper's second argument:


1 Answers

Option 1:

You can use view::share() like so:

<?php namespace App\Http\Controllers;  use View;  //You can create a BaseController:  class BaseController extends Controller {      public $variable1 = "I am Data";      public function __construct() {         $variable2 = "I am Data 2";          View::share ( 'variable1', $this->variable1 );        View::share ( 'variable2', $variable2 );        View::share ( 'variable3', 'I am Data 3' );        View::share ( 'variable4', ['name'=>'Franky','address'=>'Mars'] );     }    }  class HomeController extends BaseController {      //if you have a constructor in other controllers you need call constructor of parent controller (i.e. BaseController) like so:      public function __construct(){        parent::__construct();     }      public function Index(){       //All variable will be available in views       return view('home');     }  } 

Option 2: Use a composer:

  1. Create a composer file at app\Composers\HomeComposer.php

NB: create app\Composers if it does not exists

<?php namespace App\Composers;  class HomeComposer {      public function compose($view)     {         //Add your variables         $view->with('variable1',      'I am Data')              ->with('variable2',      'I am Data 2');     } } 

Then you can attached the composer to any view by doing this

<?php namespace App\Http\Controllers;  use View;  class HomeController extends Controller{       public function __construct(){          View::composers([             'App\Composers\HomeComposer'  => ['home'] //attaches HomeComposer to home.blade.php         ]);      }      public function Index(){         return view('home');     }  } 

Option 3: Add Composer to a Service Provider, In Laravel 5 I prefer having my composer in App\Providers\ViewServiceProvider

  1. Create a composer file at app\Composers\HomeComposer.php

  2. Add HomeComposer to App\Providers\ViewServiceProvider

<?php  namespace App\Providers;  use Illuminate\Support\ServiceProvider; use View; use App\Composers\HomeComposer; use Illuminate\Support\Facades\Blade;  class ViewServiceProvider extends ServiceProvider {     /**      * Register any application services.  *      * @return void      */     public function register()     {         //     }      /**      * Bootstrap any application services.      *      * @return void      */     public function boot()     {         //add to all views         view()->composer('*', HomeComposer::class);         //add to only home view          //view()->composer('home', HomeComposer::class);     } } 
like image 54
Emeka Mbah Avatar answered Sep 30 '22 01:09

Emeka Mbah