1

我有一个没有控制器的应用程序,并在laravel 4 文档其他文章中阅读了有关控制器布局的信息,但我不知道从哪里开始在路由(版本 4)中实现它,我该怎么做?

收到错误:InvalidArgumentException,未找到视图 [master]。

应用程序/routes.php

<?php
View::name('layouts.master', 'layout');
$layout = View::of('layout');
Route::get('users/create', array('as' => 'users.create', function() use($layout) {
  //@TODO: load view using 'layouts.master',
  //       desirable: append 'users.create' and 'users.menu' views to sidebar and content sections.
  //return View::make('users.create');
  return $layout->nest('content', 'master');
  }));
?>

应用程序/视图/布局/master.blade.php

<html>
  <body>
    @section('sidebar')
      This is the master sidebar.
    @show

    <div class="container">
      @yield('content')
    </div>
  </body>
</html>

应用程序/视图/用户/create.blade.php

{{ Form::open() }}

{{ Form::text('name') }}
{{ Form::submit('submit') }}

{{ Form::close() }}

应用程序/视图/用户/menu.blade.php

<!-- This is appended to the master sidebar -->
<p><a href="users/create">Create user</a></p>

更新:我修改了示例代码以阐明我想要做什么。检查app/routes.php及其评论

4

4 回答 4

4

您的路线文件中的代码试图将主布局嵌套在其自身中,这并不是您真正想要的。您收到错误是因为'master'会寻找app/views/master.blade.php. 这很容易通过将其更改为 来解决'layouts.master',但我不想考虑会发生什么......

您遇到问题的根本原因是从 Blade 模板“生成”视图与从路由嵌套它们之间的差异。当你嵌套一个路由时,你需要echo它而不是使用@yield标签。

// File: app/routes.php

View::name('layouts.master', 'layout');
$layout = View::of('layout');

Route::get('users/create', array('as' => 'users.create', function() use ($layout)
{
    return $layout
            ->nest('content', 'users.create')
            ->nest('sidebar', 'users.menu');
}));


/*
|--------------------------------------------------------------------------
| View Composer
|--------------------------------------------------------------------------
|
| Code in this method will be applied to all views that use the master
| layout. We use that to our advantage by injecting an "empty" sidebar
| when none is set when returning the view. It will error otherwise.
|
*/

View::composer('layouts.master', function($view)
{
    if (!array_key_exists('sidebar', $view->getData()))
    {
        $view->with('sidebar', '');
    }
});


// File: app/views/layouts/master.blade.php

<html>
<body>
    @section('sidebar')
        This is the master sidebar
        {{ $sidebar }}
    @show

    <div class="container">
        {{ $content }}
    </div>
</body>
</html>

Laravel 的View composers是一个强大的工具。如果您有共享相同模板的所有视图使用的任何数据(例如登录的用户信息),您可以使用作曲家在每次加载视图时保存注入数据。

于 2013-06-22T02:54:35.517 回答
0

假设您正在使用刀片进行模板化,您还可以使用 @parent 标记附加内容。例如(在视图中)

@section('sidebar')
    @parent
    <p>This is appended to the master sidebar.</p>
@stop
于 2013-06-14T18:31:05.170 回答
0

如果您使用刀片,则不需要使用嵌套视图。

应用程序/视图/用户/create.blade.php

你需要扩展master.blade

@extends('layouts.master')
@section('content')
// form stuff here
@stop

现在,您需要做的就是打电话create.blade

return View::make('users.create')

于 2013-06-25T00:15:33.063 回答
0

只需使用控制器路由将其作为可能的解决方案扔出去(而您可以从控制器内设置模板)。

应用程序/routes.php

Route::controller('something', 'SomethingController');

应用程序/控制器/SomethingController.php

class SomethingController extends BaseController {
  protected $layout = "templates.main"; // denotes views/templates/main.blade.php

  public function getIndex() { // the "landing" page for "/something" or "/something/index"
    $this->layout->content = View::make('something.index')->with("myVar", "Hello, world!"); // load in views/something/index.blade.php INTO main.blade.php
  }

  public function getTest() { // for "/something/test"
    $this->layout->content = View::make('something.index')->nest("widget", "something.widget", array("myVar" => "Hello, World!"));
  }
}

应用程序/视图/模板/main.blade.php

@include('templates.partials.header')
@yield('something')
@yield('content')
@include('templates.partials.footer')

app/views/something/widget.blade.php

I'm a widget. {{ $myVar }}

app/views/something/index.blade.php

@section('something')
  I will go in the 'something' yield in main.blade.php
@stop

@section('content')
  I will go in the 'content' yield in main.blade.php.

  {{ $myVar }}

  {{ $widget }}
@stop

?>

现在您可以测试http://myserver/somethinghttp://myserver/something/test查看差异。注意:未经测试,但作为一个粗略的例子。

于 2013-06-25T21:10:26.840 回答