我有这样的事情:
class AController extends BaseController
{
protected $layout = "templates.layouts.master";
protected $data = "something";
public function alt()
{
// this is wrong
// i want to override "templates.layouts.master"
// missing something obviously here
$this->layout = ??? what should do?
$this->layout->content = View::make("content", $this->data);
}
}
在方法alt中,我希望使用与默认“templates.layouts.master”不同的布局。
我的laravel 4知识非常有限。这可能很容易实现,但我不知道。
我认为可能的解决方案:
哪种方法正确?
答案 0 :(得分:8)
您可以将布局设置为每个方法的另一个视图:
class AController extends BaseController
{
protected $layout = "templates.layouts.master";
protected $data = "something";
public function alt()
{
$this->layout = View::make('templates.layouts.alt');
$this->layout->content = View::make("content", $this->data);
}
}
如果您查看BaseController
,您会看到它所做的就是调用View::make()来设置布局视图。你也可以这样做来覆盖它的默认值。
答案 1 :(得分:1)
好的,解决方案1似乎是可能的,但我觉得很难看:
class AController extends BaseController
{
public function __construct()
{
if (Request::is("..."))
{
$this->layout = "alternative layout";
}
}
}