我有这样的东西:
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"不同的布局。
我的知识非常有限。这也许很容易实现,但超出了我的知识范围。
我放弃的可能解决方案:
- 定义一个构造方法,检测当前方法,并为$layout设置一个不同的值(但是,我不知道如何获取当前方法名称)
- 做一个像我上面写的作业
哪种方式是正确的?
您可以将布局设置为每个方法的另一个视图:
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似乎是可能的,但我认为很模糊:
class AController extends BaseController
{
public function __construct()
{
if (Request::is("..."))
{
$this->layout = "alternative layout";
}
}
}