简体   繁体   English

Laravel 4. *至5. *,将视图嵌套在Controller中

[英]Laravel 4.* to 5.*, nesting views in Controller

I've updated my Laravel running environment to the latest release as of this date. 截止到目前,我已经将Laravel运行环境更新为最新版本。 Back in Laravel 4.*, I was able to do something like: 回到Laravel 4. *,我能够执行以下操作:

$this->layout->content = View::Make('users.login');

But it seems like now, that fails. 但似乎现在失败了。 I've gone through a lot of posts, trying everything that seemed reasonable, and I've come up with absolutely nothing that works for my case. 我已经阅读了很多文章,尝试了所有看起来合理的事情,但我没有发现任何适合我的情况的东西。 Links to the tried efforts can be found at the bottom of this post. 链接的尝试的努力可以在这篇文章的底部找到。 Below is the code which I am trying to get to work. 以下是我尝试使用的代码。

Errors I've received range from "Class 'App\\Http\\Controllers\\View' not found" - which is resolved when I add use View to the controller, but that results in another error. 我收到的错误来自“找不到类'App \\ Http \\ Controllers \\ View'”的错误-当我在控制器中添加Use View时可以解决此错误,但这会导致另一个错误。 The other error I've received is "Attempt to assign property of non-object" when I add the use View; 添加使用View时,我收到的另一个错误是“试图分配非对象的属性”。 bit. 位。

The basic idea is this: 基本思想是这样的:

I have two views, one that's the basic page layout (layouts.main), and one that contains the user login that I am wishing to embed inside my main layout (user.login). 我有两个视图,一个是基本页面布局(layouts.main),另一个包含希望嵌入到主布局中的用户登录名(user.login)。 They're both blade format files, they both load independent of each other if I try that, I simply can't get the two files to nest under the controller. 它们都是刀片格式文件,如果尝试这样做,它们都彼此独立加载,我根本无法将两个文件嵌套在控制器下。 I'm attempting to do this, so that the main file can be just that, and only this content section changes between page views. 我正在尝试执行此操作,以便主文件可以就是该文件,并且仅此内容部分在页面视图之间进行更改。

Controller function: 控制器功能:

public function getLogin() {
     $this->layout->content = View::Make('users.login');
}

Section of Blade file: 刀片文件部分:

    <div class="content roundBorder wrapper">
        @yield($content)
    </div>

Attempted solutions: Anything using View::make seems to fail due to deprecation(?) ex. 尝试的解决方案:使用View :: make的所有操作似乎都由于deprecation(?)而失败。 https://laravel.io/forum/03-19-2014-simply-loading-multiple-views https://laravel.io/forum/03-19-2014-simply-loading-multiple-views

Trying Laravel 4: Nest view inside layout with data but with 尝试Laravel 4:在布局内部嵌套视图,但要包含数据

return $layout->nest('content','user.login');

fails each time. 每次都失败。

Laravel define default layout from controller in good way didn't work, so I assume that's deprecated as well. Laravel不能很好地从控制器定义默认布局 ,因此我认为也已弃用。

How to include a sub-view in Blade templates? 如何在Blade模板中包括子视图? won't work, because I'm looking to do this on the fly 将无法正常工作,因为我正在寻求即时执行此操作

https://laracasts.com/discuss/channels/general-discussion/laravel-5-this-layout-content-not-working?page=1 didn't work for me, when I attempted the following: 当我尝试以下操作时, https://laracasts.com/discuss/channels/general-discussion/laravel-5-this-layout-content-not-working?page = 1不适用于我:

$content = view('user.login');    
return view($this->layout, ['content' => $content]);

Okay, there are a couple of things you're doing wrong. 好的,您做错了几件事。

Firstly, if you want to render a view inside a layout, you would typically use the @extends Blade directive. 首先,如果要在布局内渲染视图,则通常使用@extends Blade指令。 See https://laravel.com/docs/5.4/blade#template-inheritance 参见https://laravel.com/docs/5.4/blade#template-inheritance

In your layouts/main.blade.php file: 在您的layouts/main.blade.php文件中:

@yield('body')

In your users/login.blade.php file: 在您的users/login.blade.php文件中:

@extends('layouts.main')

@section('body')
    <!-- your markup -->
@stop

Finally, to render a view in a Laravel controller, you can use the view() helper function. 最后,要在Laravel控制器中呈现视图,可以使用view()帮助器函数。

public function getLogin()
{
    return view('users.login');
}

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

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