Daniel Del Core
Daniel Del Core

Reputation: 3221

Laravel Master Layout is being evaluated when using another layout

I've been trying to make a set of variables available within blade layout files e.i.

The variable i'm using is called $sitesettings->sitetitle.

I'm outputting this variable like so:

    <title>{{ $sitesettings->sitetitle }}</title>

This variable is populated within the BaseController:

class BaseController extends Controller 
{

    public function __construct()
    {   
        View::share('sitesettings', SiteSetting::find(1));
    }

    /**
     * Setup the layout used by the controller.
     *
     * @return void
     */
    protected function setupLayout()
    {
        if ( !is_null($this->layout))
        {
            $this->layout = View::make($this->layout);
        }
    }

}

The problem is when i access the admin layout. The master seems to be getting evaluated before hand and the following error occurs.

ErrorException
Undefined variable: sitesettings (View: /home/vagrant/Code/DanielDelCore/laravel/app/views/layouts/master.blade.php) (View: /home/vagrant/Code/DanielDelCore/laravel/app/views/layouts/master.blade.php)

Thank you in advance for any help.

Daniel.

Upvotes: 1

Views: 125

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 152860

I'm not sure why exactly this problem occurs, but I suggest you use a different approach. View composers.

View::composer(['layouts.master', 'layouts.admin'], function($view){
    $view->with('sitesettings', SiteSetting::find(1));
});

You can put this code in app/filters.php or create a new app/composers.php and include it at the end of app/start/global.php with:

require app_path().'/composers.php';

Upvotes: 1

Related Questions