Support the ongoing development of Laravel.io →
posted 10 years ago
Views
Last updated 1 year ago.
0

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

Last updated 1 year ago.
0

Same issue. Did any of you solve this?

Last updated 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year ago.
0

You can also use View::share

Last updated 1 year 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.

© 2024 Laravel.io - All rights reserved.