控制器布局-如何成功返回具有修改内容的布局


Controller layout- how to successfully return layout with modified content

我正在尝试设置并返回一个附加到控制器的布局。我可以成功地为@yeild('content')设置内容,但我不能同时设置和返回模板。我可以返回没有内容集的模板,也可以返回没有布局模板的内容集。

masterblade.php

<html>
    <head>
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
        @yield('styles')
    </head>
    <body>
        <div class="container">
            <h1>Google Earth project!</h1>
            <a href="http://laravel.com/docs/quick">Saucey sauce for laravel</a>
            <hr>
            <h4>Content</h4>
            @yield('content')
        </div>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
        @yield('scripts')
    </body>
</html>

HotspotController.php

...
public function show($uid, $lid, $hid)
{
    // $this->layout->content = View::make('hotspot.profile');
    $this->layout->content = 'a string';
    return $this->layout;
}
....

@yield('content')和{{$content}}之间显然存在差异。

为了完成您想要完成的任务,您必须使用后一种方法在刀片模板上声明可变内容。

HotspotController.php

protected $layout = 'layouts.master';
...
public function show($uid, $lid, $hid)
{
    $this->layout->content = View::make('hotspot.profile');
}
...

热点.profile.blade.php

I am a hotspot profile

masterblade.php

<html>
    <head>
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
        @yield('styles')
    </head>
    <body>
        <div class="container">
            <h1>Google Earth project!</h1>
            <a href="http://laravel.com/docs/quick">Saucey sauce for laravel</a>
            <hr>
            <h4>Content</h4>
            {{ $content }}
        </div>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
        @yield('scripts')
    </body>
</html>

或者

您可以在主模板中保留@yield('content'),但您必须修改hotspot.profile.blade.php以包含@section('content']@stop。请参阅热点.blade.php

masterblade.php

<html>
    <head>
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
        @yield('styles')
    </head>
    <body>
        <div class="container">
            <h1>Google Earth project!</h1>
            <a href="http://laravel.com/docs/quick">Saucey sauce for laravel</a>
            <hr>
            <h4>Content</h4>
            @yield('content')
        </div>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
        @yield('scripts')
    </body>
</html>

热点.profile.blade.php

@section('content')
I am a hotspot profile
@stop