Php 在一个控制器中使用多个布局
我可以使用Php 在一个控制器中使用多个布局,php,laravel,Php,Laravel,我可以使用protected$layout='layouts.mylayout'定义在使用$this->layout->content=View::make('myview')时,Laravel应该使用哪个布局但是如果我需要在同一个控制器中使用多个布局,我应该怎么做?看起来您无法使用受保护的$layout来完成。但是你有很多选择 一种是将布局名称传递给视图: 类TestController扩展BaseController{ 公共职能指数() { 返回视图::make('myview',['lay
protected$layout='layouts.mylayout'
定义在使用$this->layout->content=View::make('myview')时,Laravel应该使用哪个布局
但是如果我需要在同一个控制器中使用多个布局,我应该怎么做?看起来您无法使用受保护的$layout
来完成。但是你有很多选择
一种是将布局名称传递给视图:
类TestController扩展BaseController{
公共职能指数()
{
返回视图::make('myview',['layout'=>'layouts.mylayout']);
}
公共活动展览(
{
返回视图::make('myview',['layout'=>'layouts.mySecondLayout']);
}
公共函数create()
{
///这个将使用您的默认布局
返回视图::make('myview');
}
}
并且@扩展了myview.blade.php
中的布局:
@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>
这个解决方案怎么样?
您可以覆盖控制器方法中的layout
属性,将内容分配给它,等等。。。
响应自动返回
请注意,确保控制器扩展了包含setupLayout
方法的BaseController
。
如果未扩展,请在控制器内部执行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');
}
}
调用该方法时,$layout已转换为视图对象。您需要执行$this->layout=View::make('layout2')代码>查看此函数在路由到的方法执行之前被调用。你说得太对了,William Cahill Manley。只是完全改变了我的答案,谢谢。
<?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');
}
}