一个控制器中有2个布局

时间:2014-09-03 19:25:51

标签: 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 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但是我收到了一个错误:

  

ErrorException(E_UNKNOWN)   尝试分配非对象的属性

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您需要对BaseController正在扩展的HomeController进行更改。 在您的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
}
相关问题