我可以使用protected $layout = 'layouts.mylayout';
来定义Laravel在使用$this->layout->content = View::make('myview');
时应该使用哪种布局,但是如果我需要在同一控制器中使用多个布局,该怎么办?
这个解决方案呢?您可以在控制器方法中覆盖layout
属性,向其添加内容等...响应会自动返回。
请注意,请确保您的控制器扩展BaseController
其中包含setupLayout
方法。如果未扩展,请在控制器内部实现setupLayout
。
<?php
class UsersController extends BaseController
{
protected $layout = 'users.layout.main';
public function getList()
{
$this->layout->content = View::make('users.list');
}
public function getDetail()
{
$this->layout = View::make('users.layout.detail');
$this->layout->content = View::make('users.detail');
}
}
看起来你不能用protected $layout
来做。但是你有很多选择。
一种是将布局名称传递给视图:
class TestController extends BaseController {
public function index()
{
return View::make('myview', ['layout' => 'layouts.mylayout']);
}
public function show()
{
return View::make('myview', ['layout' => 'layouts.mySecondLayout']);
}
public function create()
{
/// this one will use your default layout
return View::make('myview');
}
}
并在您的myview.blade.php
中@extends
您的布局:
@extends( isset($layout) ? $layout : Config::get('app.layout') )
@section('content')
Here goes your content
@stop
你的布局应该是这样的
<html><body>
THIS IS YOUR LAYOUT 1
@yield('content')
</body></html>
此外,在您的应用程序/配置/应用程序中.php您必须配置默认布局:
return array(
'layout' => 'layouts.master',
...
);