user3150060
user3150060

Reputation: 1745

2 layouts in one controller

I have in my HomeController.php

   // other pages layout
protected $layout = "layout";

protected $layout2 = "layout2";
    // empty user object
protected $user;

// constructor
public function __construct() 
{ 
}


public function getAbout()
{

// share main home page only
 $this->layout->content = View::make('about');

}

// THIS IS NOT WORKING >>>>
public function getAboutnew()
{

// share main home page only
 $this->layout2->content = View::make('about');

}

so the getAboutNew I am trying to use layout2 but I am getting an error:

ErrorException (E_UNKNOWN) Attempt to assign property of non-object

How to fix this?

Upvotes: 0

Views: 52

Answers (1)

Nein
Nein

Reputation: 419

You need to make changes to your BaseController from which your HomeController is extending. In your BaseController you have:

protected function setupLayout()
{
    if ( ! is_null($this->layout))
    {
        $this->layout = View::make($this->layout);
    }
}

This converts the $this->layout (string) into the view object that you need.

Fix:

// BaseController, returning respective views for layouts
protected function setupLayout()
{
    if ( ! is_null($this->layout))
    {
        $this->layout = View::make($this->layout);
    }

    if ( ! is_null($this->layout2))
    {
        $this->layout2 = View::make($this->layout2);
    }
}



// HomeController
public function getAbout()
{
    $this->layout->content = View::make('about');
}


public function getAboutnew()
{
     $this->layout2->content = View::make('about');
     return $this->layout2;
     // Note the additional return above.
     //You need to do this whenever your layout is not the default $this->layout
}

Upvotes: 1

Related Questions