Laravel 4:在路由中使用灵活的布局

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

Laravel 4: Using flexible layouts within routes

phprouteslaravellaravel-4template-engine

提问by quantme

I have an application without controllers and read about controller layoutsin laravel 4 documentationand this other article too, but I don't know where to start for implement it within routes (version 4), how can I do that?

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

Error received: InvalidArgumentException, View [master] not found.

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

app/routes.php

应用程序/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');
  }));
?>

app/views/layouts/master.blade.php

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

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

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

app/views/users/create.blade.php

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

{{ Form::open() }}

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

{{ Form::close() }}

app/views/users/menu.blade.php

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

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

Update: I modified example code to clarify what I want to do. Check app/routes.phpand its comments

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

回答by Dave S

The code in your routes file is trying to nest the master layout within itself, which isn't really what you want. You're getting the error because 'master'would look for app/views/master.blade.php. That's easily fixed by changing it to 'layouts.master', but I wouldn't like to think what might happen...

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

The root cause of the issue you're having is the difference between "yielding" views from a Blade template, and nesting them from a route. When you nest a route, you need to echoit rather than using the @yieldtag.

您遇到的问题的根本原因是从 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's View composersare a powerful tool. If you have any data (eg logged-in user info) used by all views that share the same template(s), you can use the composers to save injecting the data every time you load the view.

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

回答by Rob Allport

You could also use the @parent tag to append content, assuming you;re using blade for templating. E.g. (in the view)

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

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

回答by Can Geli?

You don't need to use nesting views if you're using blade.

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

app/views/users/create.blade.php

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

You need to extend the master.blade

你需要扩展 master.blade

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

Now, all you need to do is call create.blade

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

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

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

回答by Half Crazed

Just throwing this out there as a possible solution using controller routing (whereas you can set the template from within the controller).

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

app/routes.php

应用程序/routes.php

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

app/controllers/SomethingController.php

应用程序/控制器/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!"));
  }
}

app/views/templates/main.blade.php

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

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

app/views/something/widget.blade.php

应用程序/视图/东西/widget.blade.php

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

app/views/something/index.blade.php

应用程序/视图/东西/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

?>

Now you can test http://myserver/somethingand http://myserver/something/testto see the differences. Note: not tested but as a rough example.

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