2

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]);
Community
  • 1
  • 1
Gyhth
  • 1,155
  • 9
  • 21

1 Answers1

4

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');
}
fubar
  • 16,918
  • 4
  • 37
  • 43
  • I'm using the view function, in the working version, so that's not an issue. The mentions in the post are the old ways, and the tried ways, of getting the functionality to work as it used to. As for the main file, what used to be done via the controller is now done entirely though the blade interface...? – Gyhth Mar 07 '17 at 01:11
  • Welp, doing it all through the blade scripts seems to do the trick, although that's something I had hoped to get around and only change the controllers... oh well. +1 to you! – Gyhth Mar 07 '17 at 01:17
  • Well, that's the documented way. Obviously blade eventually compiles down to PHP, so you could look into the source and see if you can do it via the View object in your controller instead. – fubar Mar 07 '17 at 01:19