Alex
Alex

Reputation: 7688

Laravel send default variables to layout

Having the following class that is extended by other controllers

class Admin_Controller extends Base_Controller 
{
    static $admin_layout = 'admin.layouts.default';

    public function __construct()
    {
        parent::__construct();

        $role_object = User::find(Auth::user()->id)->roles()->get();
        $role = $role_object[0]->attributes['name'];

such as:

class Admin_Draws_Controller extends Admin_Controller
{
    public $restful = true;

    public function __construct()
    {
                $this->layout = parent::$admin_layout;
        parent::__construct();
    }

    public function get_index()
    {
        $view = View::make('admin.templates.draws');
        $this->layout->content = $view;
    }
}

How can I send the $role variable to admin.layouts.default so I can have it when ever the view is loaded?

The point of "global" $role variable is to avoid to have to call it in all of my View::make() like the following:

$view = View::make('admin.templates.articles',
    array(
        'fields' => $fields,
        'data' => $results,
        'links' => $links,
                    'role' => 'role here'. // I don't want to add this where ever I call the View::make
    )
);
$this->layout->content = $view;

and just do an echo $role like, in my header.blade.php

Upvotes: 2

Views: 2814

Answers (1)

Alex
Alex

Reputation: 7688

I ended up doing the following, which works.

Controller

// Example of variable to set
$this->layout->body_class = 'user-register';

$view = View::make('modules.user.register', array(
    'success' => true,
));

$this->layout->content = $view;

My default.layout.php view

<body class="<?php echo isset($body_class) ? $body_class : '' ;?>">

    @include('partials.header')

    {{ $content }}

    @include('partials.footer')

The variable, can be easily used in any other context within the view.

Upvotes: 1

Related Questions