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).
I'm having the same issue. Did you figure something out?
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.
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.
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';
}
}
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?
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:
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community