Php 一个控制器中有2个布局

Php 一个控制器中有2个布局,php,laravel-4,Php,Laravel 4,我的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

我的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');

}
因此,
getAboutNew
我试图使用layout2,但我得到一个错误:

错误异常(E_未知) 尝试分配非对象的属性


如何解决这个问题

您需要对您的
家庭控制器
所扩展的
基本控制器
进行更改。 在
BaseController
中,您有:

protected function setupLayout()
{
    if ( ! is_null($this->layout))
    {
        $this->layout = View::make($this->layout);
    }
}
这将
$This->layout
(字符串)转换为所需的视图对象

修复:

// 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
}

$this->layout2是一个字符串,而不是一个对象。为什么$this->layout2是一个对象,而那个可以工作?