laravel 如何在同一个控制器中使用多个布局?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18185183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:15:54  来源:igfitidea点击:

How to use multiple layouts inside the same controller?

laravellaravel-4

提问by osos

I wanted to ask how can I define a multiple layouts for the same controller in Laravel. The scenario here is like the following:

我想问一下如何在 Laravel 中为同一个控制器定义多个布局。这里的场景如下所示:

I have a controller Home and i have two actions in this controller one called steps and the other called login.

我有一个控制器 Home,我在这个控制器中有两个操作,一个称为步骤,另一个称为登录。

I want the both of them load different layout.

我希望他们都加载不​​同的布局。

The way that I used to make this is as follow:

我用来做这个的方法如下:

protected $layout = "layouts.page";

public function index()
{
    // Get to the page of the website making steps
    $this->layout->content = View::make('steps');
}

Can I define multiple layouts? Maybe passing an array as follow:

我可以定义多个布局吗?也许传递一个数组如下:

protected $layout = array('first' => "layouts.page", 'second' => 'layouts.second');

回答by netvision73

Best solution is to create a method to generate your view, nesting your multiples layouts :

最好的解决方案是创建一种方法来生成您的视图,嵌套您的多个布局:

return View::make('layouts.master', array())
       ->nest('section_one', YOUR_SECOND_MASTER, array())
       ->nest...

and stop setting protected $layoutwith a layout.

并停止protected $layout使用布局进行设置。

回答by Js Lim

I achieve in this way

我以这种方式实现

$this->layout = View::make('layout.master');
$this->layout->content = View::make('step.demo')

回答by Abishek

Use View Composersor look at the section passing sub-views to views under http://laravel.com/docs/responses#views.

使用View Composers或查看将子视图传递到http://laravel.com/docs/responses#views下的视图的部分。

You can also specify multiple sections for the layout that is defined at http://laravel.com/docs/templates#blade-templating

您还可以为在http://laravel.com/docs/templates#blade-templating 中定义的布局指定多个部分

EDIT:

编辑:

If you want to define a master layout for different views from the same controller, then define the layout on the View it self. Take a look at the section Using A Blade Layout

如果要为来自同一控制器的不同视图定义主布局,请在视图本身上定义布局。查看使用刀片布局部分

The @extendsis used to define the layout on the view itself.

所述@extends用于定义上视图本身的布局。

Hope this helps for what you are looking for.

希望这对您正在寻找的东西有所帮助。

回答by fideloper

If you look at the BaseController, which your controller likely extends, you'll see the layout variable is ultimately used simply as th e result of any old View.

如果您查看您的控制器可能扩展的BaseController,您会看到布局变量最终仅用作任何旧视图的结果。

In other words, your $layoutvariable is just a View. You can create any $layoutvariable in your controller:

换句话说,您的$layout变量只是一个视图。您可以$layout在控制器中创建任何变量:

<?php

class MyController extends BaseController {

    protected $layout;

    protected $layout_alt;

    // Here we're over-riding setupLayout() from
    // the BaseController
    protected function setupLayout()
    {
        if ( ! is_null($this->layout))
        {
            $this->layout = View::make($this->layout);
        }

        if ( ! is_null($this->layout_alt))
        {
            $this->layout_alt = View::make($this->layout_alt);
        }

    }

}

Then in your view, you can return:

那么在你看来,你可以返回:

 $this->layout_alt->content = View::make('steps');

Of course, the possibilities are endless as Abishek R Srikaanth pointed out. You can do fancy things with Blade as well :D

当然,正如 Abishek R Srikaanth 指出的那样,可能性是无穷无尽的。你也可以用 Blade 做一些花哨的事情 :D

回答by iamjonesy

The way i do this is quite similar to @fideloper's answer.

我这样做的方式与@fideloper 的回答非常相似。

protected $layout;
private $_layout = null;

public function __construct()
{

}

private function _setupLayout()
{
    if ( ! is_null($this->_layout))
    {
        $this->layout = View::make($this->_layout);
    }
}

public function home() {
    $this->_layout = 'layouts.1col_public';
    $this->_setUpLayout();
    $this->layout->content = View::make('static/home');
}

public function about() {
    $this->_layout = 'layouts.2col_public';
    $this->_setUpLayout();
    $this->layout->active_menu = 'about';
    $this->layout->content = View::make('static/default');
}

回答by Mike Rockétt

This isn't common practise, and I haven't tested it yet, but it's worth a try.

这不是常见的做法,我还没有测试过,但值得一试。

In your controller's method:

在您的控制器方法中:

$this->layout = View::make('layouts.master1");