laravel 4 - 如何为特定方法加载不同的布局?

ajr*_*eal 3 php laravel laravel-4

我有这样的事情:

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

在方法alt中,我希望使用与默认"templates.layouts.master"不同的布局.

我的laravel 4知识非常有限.这可能很容易实现,但我不知道.

我认为可能的解决方案:

  1. 定义构造方法,并检测当前方法是什么,并为$ layout设置不同的值(但是,我不知道如何获取当前方法名称).
  2. 做一个像我上面所说的任务.

哪种方法正确?

fid*_*per 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果你看看BaseController,你会发现它只是调用View :: make()来设置布局视图.您可以执行相同操作以覆盖其默认值.