Laravel 4 控制器模板/刀片 - 正确的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16951010/
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 07:53:50  来源:igfitidea点击:

Laravel 4 Controller Templating / Blade - Correct method?

viewslaravellaravel-4conventionsblade

提问by Mediabeastnz

I've been reading through the Laravel 4 documentation and have been making a demo application to help with learning.

我一直在阅读 Laravel 4 文档并制作了一个演示应用程序来帮助学习。

I couldn't find much documentation on the templating of views with blade and controllers. Which is the correct method or does it come down to personal preference?

我找不到很多关于刀片和控制器视图模板的文档。哪个是正确的方法还是归结为个人喜好?

E.g. 1

例如 1

Controllers/HomeController.php

控制器/HomeController.php

protected $layout = 'layouts.main';

public function showWelcome()
{
    $this->layout->title = "Page Title";
    $this->layout->content = View::make('welcome');
}

Views/layouts/main.blade.php

视图/布局/main.blade.php

<html>
<head>
    <title>{{ $title }}</title>
</head>
<body>
    {{ $content }}
</body>
</html>

Views/welcome.blade.php

视图/welcome.blade.php

<p>Welcome.</p>

E.g. 2

例如 2

Controllers/HomeController.php

控制器/HomeController.php

protected $layout = 'layouts.main';

public function showWelcome()
{
    $this->layout->content = View::make('welcome');
}

Views/layouts/main.blade.php

视图/布局/main.blade.php

<html>
<head>
    <title>@yield('title')</title>
</head>
<body>
    @yield('content')
</body>
</html>

Views/welcome.blade.php

视图/welcome.blade.php

@section('title', 'Welcome')
@section('content')
// content
@stop

What is the best convention and/or advantages of the the above?

以上的最佳约定和/或优点是什么?

回答by SamV

I don't store any layout information in the controller, I store it in the view via

我不在控制器中存储任何布局信息,而是通过以下方式将其存储在视图中

@extends('layouts.master')

When I need to return a view in the controller I use:

当我需要在控制器中返回一个视图时,我使用:

return \View::make('examples.foo')->with('foo', $bar);

I prefer this approach as the view determines what layout to use and not the controller - which is subject to re-factoring.

我更喜欢这种方法,因为视图决定了要使用的布局,而不是控制器——这取决于重构。

回答by Farzher

I don't like either of them. Layouts are probably the weirdest part of Laravel. The controller version doesn't really make sense; all methods of the controller then require that view. The @yield version is a mess of boilerplate. I made up this "method specific layouts":

我不喜欢他们中的任何一个。布局可能是 Laravel 中最奇怪的部分。控制器版本没有意义;然后控制器的所有方法都需要该视图。@yield 版本是一堆样板。我编造了这个“特定于方法的布局”:

public function index()
{
    return View::make('layouts.main', [
        'layout_data' => 'sup'
    ])->nest('content', 'welcome', [
        'view_data' => 'sup'
    ]);
}

I think it should be mentioned in the docs that this is an option.

我认为应该在文档中提到这是一个选项。

回答by Aloys

I prefere the second one, because it shows a more clear separation between your view and controller code. It seems more logical to me that title would be a property of a content view instead of combining your welcome view with your welcome title each time.

我更喜欢第二个,因为它显示了您的视图和控制器代码之间更清晰的分离。对我来说,标题应该是内容视图的属性,而不是每次都将欢迎视图与欢迎标题相结合,这似乎更合乎逻辑。

In the end both are correct and will work, but the second alternative is more maintainable.

最后,两者都是正确的并且可以工作,但第二种选择更易于维护。

回答by thestepafter

I prefer the 1st method since some sites have a dynamically generated title from the database. It's easy to pass the title in using the first method.

我更喜欢第一种方法,因为某些站点具有从数据库动态生成的标题。使用第一种方法很容易传递标题。