什么是 Laravel 中的“在控制器上定义布局”


what is "Defining A Layout On A Controller" in Laravel?

Laravel中的"在控制器上定义布局"是什么?例如,此代码:

$this->layout->content = View::make('user.profile');

这是什么意思?

$this->layout->content

我已经阅读了文档,但我不明白。

Laravel为我们提供了两种不同的方法来使用Layout,在Controller中,并通过扩展master/main布局在View中。因此,在控制器中定义布局如下所示:

class UserController extends BaseController {
    // The master layout in the layouts folder
    protected $layout = 'layouts.master';
    public function showProfile()
    {
        // Set the content to the master layout to display
        $this->layout->content = View::make('user.profile');
    }
}

因此,布局是一种template,其中视图生成的内容将显示在该布局中。另一方面,您可以直接从视图中使用layout,在这种情况下,您无需在控制器中定义布局。视图将扩展主布局,如下所示:

@extends('layouts.master')
@section('sidebar')
    <p>This is appended to the master sidebar.</p>
@stop

主布局可能如下所示:

<html>
    <body>
        @section('sidebar')
            This is the master sidebar.
        @show
        <div class="container">
            @yield('content')
        </div>
    </body>
</html>

在Laravel网站上阅读更多信息。