简体   繁体   中英

Laravel 4.* to 5.*, nesting views in Controller

I've updated my Laravel running environment to the latest release as of this date. Back in Laravel 4.*, I was able to do something like:

$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. The other error I've received is "Attempt to assign property of non-object" when I add the use 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). 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. https://laravel.io/forum/03-19-2014-simply-loading-multiple-views

Trying Laravel 4: Nest view inside layout with data but with

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.

How to include a sub-view in Blade templates? 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:

$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. See https://laravel.com/docs/5.4/blade#template-inheritance

In your layouts/main.blade.php file:

@yield('body')

In your users/login.blade.php file:

@extends('layouts.main')

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

Finally, to render a view in a Laravel controller, you can use the view() helper function.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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