Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adrielzxdf's avatar

Laravel 5 Pass Variables to Master Template

Hi,

In Laravel 4, I am able to pass variables to my master template via filters.php like this:

View::composer('layout.master', function ($view) {
    view->with('variable', 'myvariable');
}

How can I achieve the same thing in Laravel 5? What would be the best practice for placing variables that can be accessed by all pages. Thanks.

0 likes
2 replies
JohnRivs's avatar
Level 37

https://laracasts.com/series/laravel-5-fundamentals/episodes/25

Register a service provider or use AppServiceProvider.

public function boot()
{
    view()->composer('layouts.master', function($view)
    {
        $view->with('variable', 'myvariable');
    });
}

Or use a dedicated class:

// app/Providers/AppServiceProvider.php
public function boot()
{
    view()->composer('layouts.master', 'App\Http\Composers\MasterComposer');
}

// app/Http/Composers/MasterComposer.php
use Illuminate\Contracts\View\View;

class MasterComposer {

    public function compose(View $view)
    {
        $view->with('variable', 'myvariable');
    }

}
6 likes
adrielzxdf's avatar

Welp, problem solved. Thanks, I missed that one!

1 like

Please or to participate in this conversation.