简体   繁体   English

Laravel PHP:使用nest()有麻烦

[英]Laravel PHP: Having trouble using nest()

I'm trying to use 'nest()' in Laravel PHP by taking 2 views, 1) The layout (layouts\\master.blade.php) and 2) The form (forms\\new-video.php). 我正在尝试通过2个视图在Laravel PHP中使用'nest()',1)布局(layouts \\ master.blade.php)和2)表单(forms \\ new-video.php)。

However when I use the 'nest()' function, all I am seeing is the output from 'master.blade.php' and not the form from 'forms.new-video.blade.php'. 但是,当我使用'nest()'函数时,我看到的只是'master.blade.php'的输出,而不是'forms.new-video.blade.php'的表单。

Controller: 控制器:

class VideosController extends BaseController {

/**
 * The video model
 *
 * @var \Models\Video
 **/
protected $video;


//  Use this variable for the repository

protected $videoRepo;

protected $layout = 'layouts.master';

  public function create()
  {
    $data = array('type' => 'video');
    $this->layout->content = \View::make('forms.new-video', $data)
        ->nest('form', 'forms.new-video');
  }
}

EDIT: Here is my 'master.blade.php' layout(template): 编辑:这是我的“ master.blade.php”布局(模板):

<!doctype html>
<html>
<head>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href=" <?php echo asset('css/style.css')?>" type="text/css">
    <style type="text/css">
        form ul {list-style: none}
        .navbar {border-radius: 0px;}
    </style>
</head>

<body>
    <nav class="navbar navbar-inverse" role="navigation">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
              <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
              </button>
              <a class="navbar-brand" href="#">Pictures and Videos</a>
            </div>
            <div class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li {{ (Request::is('pics_vids') ? 'class="active"' : '') }}>{{ link_to_route('overview', 'Home') }}</li>
                    <li {{ (Request::is('pics_vids') ? 'class="active"' : '') }}>{{ link_to_route('overview', 'Home') }}</li>
                    <li {{ (Request::is('pics_vids/new_video') ? 'class="active"' : '') }}>{{ link_to_route('new_video', 'New Video') }}</li>
                </ul>
            </div>
        </div>
    </nav>

/* Content is being called in my 'container' <div> */

    <div class="container">
        @if (Session::has('message'))
            <div class="flash alert">
                <p>{{ Session::get('message') }}</p>
            </div>
        @endif

/* Call up the content here  */

        @yield('content')
    </div>

    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
</body>

I know I'm not using the 'nest()' method properly since Laravel is still new to me. 我知道我没有正确使用'nest()'方法,因为Laravel对我来说还是新手。 Any help is appreciated! 任何帮助表示赞赏!

You can use layouts in 2 main ways in Laravel: 您可以在Laravel中以2种主要方式使用布局:

1. Controller Templates 1.控制器模板

In your controller you have: 在您的控制器中,您具有:

$this->layout->content = \View::make('forms.new-video', $data);

In your master.blade.php you have this where you want the content to be placed: 在您的master.blade.php您可以将其放置在以下位置:

{{ $content }} // When using controller templates you don't use @yield

And in your new-video view you have whatever html you need. new-video视图中,您将拥有所需的任何html。

2. Template Inheritance 2.模板继承

You controller doesn't need to extend BaseController for this to work. 您的控制器不需要扩展BaseController即可工作。 And in your action you return the view not assign it, like so: 在执行操作时,您将返回视图而不分配视图,如下所示:

return \View::make('forms.new-video', $data);

In your master.blade.php you use @yield where you want to include your content: 在您的master.blade.php @yield ,您要使用@yield包含您的内容:

@yield('content');

In your new-video view you need to specify that the view @extents your master layout and define a section named content that will be included in your layout: new-video视图中,您需要指定视图@extents主布局并定义一个名为content的部分,该部分将包含在布局中:

@extends('layouts.master')

@section('content')
    // Your form html goes here
@stop

In your case you can just use $this->layout->content = \\View::make('forms.new-video', $data); 在您的情况下,您可以只使用$this->layout->content = \\View::make('forms.new-video', $data); in your controller and {{ $content }} instead of @yield in your master layout. 在您的控制器和{{ $content }}而不是在主布局中使用@yield

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM