Support the ongoing development of Laravel.io →
posted 11 years ago
Views

The view composer doesn't seem to fire on the master layout that all other views extend. The code works fine on views that get called through View::make but not on the master view they extend. Is that the expected behavior?

Basically the same problem as this guy, but his solution doesn't seem to work: http://forumsarchive.laravel.io/viewtopic.php?pid=60680

UPDATE: it seems like the variables do get assigned and are available in the master layout (and any included view) but not in the requested view (the one that extends the master layout).

Last updated 2 years ago.
0

I'm having the same issue. Did you figure something out?

Last updated 2 years ago.
0

Same issue. Did any of you solve this?

Last updated 2 years ago.
0

i use view composer but i use it in a different way at the basecontoller.php i have public function __construct() { if(Auth::check()}{ $name = Auth::user()->fname.' '.lname; } }

This works for me. I can access $name in Master layout as well as child layouts.

Last updated 2 years ago.
0

Also, the mentioned post http://forumsarchive.laravel.io/viewtopic.php?pid=60680 would not work because the variable is available to Layout.master not any other layouts.

Last updated 2 years ago.
0

Try this and see if it works

class HomeController extends BaseController {

	public $restful = true;
	public $layout = 'templates.example';

public function showWelcome() {
  $view = View::make('home.view');

  //set variable available in view
  $view->var_available_in_view = 'hello view';

  //set variable available in  template
  $this->layout->var_available_in_template = 'Hello template';

}

}
Last updated 2 years ago.
0

joy014 said:

i use view composer but i use it in a different way at the basecontoller.php i have public function __construct() { if(Auth::check()}{ $name = Auth::user()->fname.' '.lname; } }

This works for me. I can access $name in Master layout as well as child layouts.

I am not sure that I understand this approach, how does the $name variable get passed to the view?

Last updated 2 years ago.
0

I found that it is possible to use wildcards when registering view composers. This is still a suboptimal solution as it requires you to structure your views folder in a certain way.

Say your views folder is structured like this:

  • views
    • public
      • home.blade.php
      • blog.blade.php
    • admin
      • posts.blade.php
      • users.blade.php

It is then possible to register a view composer on all views in a folder like this:

View::composer('admin.*', function($view) {
    $view->with('user', Auth::user());
});

Like I said, suboptimal, but this does it for me at the moment.

Last updated 2 years ago.
0

You can also use View::share

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.