laravel Global Variable For all Views | websolutioncode.com
laravel Global Variable For all Views | websolutioncode.com

laravel Global Variable For all Views

Laravel, a powerful PHP framework, offers a convenient way to share data across all your views using global variables. This feature proves handy when you have data that needs to be accessible throughout your application, eliminating the need to pass it explicitly to each view. In this article, we’ll explore how to create and use global variables in Laravel with easy-to-understand code examples.

Understanding Global Variables in Laravel

Global variables in Laravel are accessible from any view within your application. These variables are typically set in a central location and automatically made available to all views, ensuring consistency and ease of data management.

Setting Up a Global Variable

Let’s start by creating a global variable in Laravel. We’ll use the View::share method in Laravel, which allows us to share data with all views.

// File: app/Providers/AppServiceProvider.php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // Define a global variable named 'siteName'
        View::share('siteName', 'My Awesome Website');
    }
}

In this example, we’ve created a global variable named siteName and set its value to “My Awesome Website.” The AppServiceProvider is an ideal place to define global variables as it is loaded during the application’s bootstrapping process.

Don’t forget to register the AppServiceProvider in the providers array within the config/app.php file:

// File: config/app.php

'providers' => [
    // Other service providers...
    App\Providers\AppServiceProvider::class,
],

Using the Global Variable in Views

Now that we’ve set up a global variable, let’s see how we can access it in our views.

<!-- File: resources/views/welcome.blade.php -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome to {{$siteName}}</title>
</head>
<body>
    <h1>Welcome to {{$siteName}}</h1>
    <p>This is a sample Laravel application with a global variable.</p>
</body>
</html>

In this example, we use the Blade templating engine to insert the value of the siteName global variable into the HTML title and heading. Laravel automatically makes this variable available to all views, so you don’t need to pass it explicitly.

Adding Multiple Global Variables

You can add as many global variables as needed by chaining more View::share calls in the boot method of the AppServiceProvider.

// File: app/Providers/AppServiceProvider.php

public function boot()
{
    View::share('siteName', 'My Awesome Website');
    View::share('companyName', 'TechCo');
    View::share('yearFounded', 2020);
}

Now, the siteName, companyName, and yearFounded global variables are available in all your views.

Conclusion

Implementing global variables in Laravel provides a convenient way to share data across all views, promoting code consistency and reducing the need for repetitive variable passing. By following the steps outlined in this article, you can easily create and utilize global variables in your Laravel application. Happy coding!

Leave a Reply