Introduction
Laravel is a powerful PHP framework that has gained widespread popularity for its elegant syntax and useful features. Blade is Laravel’s templating engine, which is intuitive and flexible. One common requirement while working with Blade templates is the use of global variables. In this tutorial, we will explore various methods of defining and using global variables across Blade views in your Laravel application.
What Are Global Variables?
In the context of Laravel and Blade templates, a global variable is one that can be accessed across all views without needing to be explicitly passed every time a view is returned by a controller.
Method 1: Using the view
Composer
The view
composer can be used to bind data to all views.
View::composer('*', function ($view) {
$view->with('globalVariable', 'This is a global variable');
});
Now, in any Blade template, you can access $globalVariable
directly:
{{ $globalVariable }}
Method 2: Sharing Data Using Service Provider
Another way to define a global variable is by using a service provider.
public function boot() {
view()->share('globalVariable', 'This is a global variable');
}
In the views, you access it just like any other variable:
{{ $globalVariable }}
Method 3: Environmental Configuration
Environmental variables can be set in the .env
file and accessed in Blade templates using the env
function.
APP_GLOBAL_VARIABLE="A global value from .env"
In your Blade template, use:
{{ env('APP_GLOBAL_VARIABLE') }}
Method 4: Creating a Blade Directive
Custom Blade directives can provide a reusable piece of code that you can use in any template.
Blade::directive('globalVariable', function () {
return 'This is a global variable';
});
And then use your custom directive in your templates:
@globalVariable
Method 5: Using a Base View
A base Blade template can be used to define variables that can be extended by other views.
// resources/views/layouts/app.blade.php
@section('globalVariableSection')
This is the content of the global variable section.
@endsection
And in child views:
@extends('layouts.app')
@section('globalVariableSection')
@parent
Additional content for the section.
@endsection
Advanced Methods
Using Middleware
Middleware can be used to bind data to views on every HTTP request.
public function handle($request, Closure $next)
{
view()->share('globalVariable', 'This is set by middleware');
return $next($request);
}
Then, register your middleware in the web
group within the app/Http/Kernel.php
file.
Global Variables for Specific Views
You can bind data to specific views using patterns.
View::composer(['profile', 'dashboard'], function ($view) {
$view->with('globalVariable', 'Specific for some views');
});
In view profile.blade.php
and dashboard.blade.php
use as normal:
{{ $globalVariable }}
Using a Singleton
You can set a global variable via Laravel’s container binding as a singleton.
App::singleton('globalVariable', function () {
return 'This is a global variable as a singleton';
});
Access it in your Blade templates by:
{{ App::make('globalVariable') }}
Conclusion
In conclusion, global variables offer a handy solution for sharing common data among different views within a Laravel application. From something as simple as using the view
composer and environmental variables, to creating robust and reusable code with service providers and custom directives, you have a range of options at your disposal depending on your project’s needs.