我一直在阅读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

Controllers/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

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

Views/welcome.blade.php

<p>Welcome.</p>

E.g. 2

Controllers/HomeController.php

protected $layout = 'layouts.main';

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

Views/layouts/main.blade.php

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

Views/welcome.blade.php

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

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

推荐答案

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

@extends('layouts.master')

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

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

我更喜欢这种方法,因为视图决定要使用什么布局,而不是控制器——控制器需要重新分解.

Laravel相关问答推荐

LARAVEL 从多个表中获取重复的行

Npm run dev 卡在 APP_URL

给定的角色或权限应该使用 guard `` 而不是 `web`. -Laravel

如何在 Laravel 中显示渲染时间/页面加载时间?

路由模型绑定不起作用

这个上下文的流程应该是怎样的? (根据 Select 的付款方式,将处理付款的代码放在哪里?)

Homebrew PHP 似乎没有链接

Eloquent - 更新集合中的所有模型

日期验证 - 如何本地化/翻译字符串今天和明天

Laravel:验证 json 对象

在 Laravel 5.4 中向多个抄送收件人发送Electron邮件

Laravel 搜索关系

使用 Laravel 使用 2 个磁盘复制文件

Elastic search全文与mysql全文?

Laravel 4:将什么作为参数传递给 Url 类?

Laravel 验证存在于不存在的地方

laravel如何访问具有表编号名称的列?

Laravel 5 默认注册后如何发送邮件?

如何在 Laravel 5 中验证 RESTful API?

Laravel 将附加参数传递给函数