Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bencarter78@hotmail.com's avatar

Laravel 5: $this->layout->content not working?

Hi all,

I'm just playing around with Laravel 5 and it's a fresh install but I've added the example in the docs to my controller and for the line

$this->layout->content

it gives me the error 'Attempt to assign property of non-object'.

Anyone any ideas?

0 likes
7 replies
danharper's avatar

Looks like Laravel 5 no longer includes the templating logic in controllers which Laravel 4 had.

See: callAction in Laravel 4 vs callAction in Laravel 5.

No idea if it'll still be like this in the final release, or if this is being done to push Blade templates more?

Either way, if you want to do templates the old way you can:

  1. Make a BaseController class which all your controllers will extend from
  2. Copy in the callAction method from Laravel 4's Controller
  3. Copy in the setupLayout method (<-- that's a link) which was in Laravel 4's BaseController.

I haven't tested this, but that should do it.

ratiw's avatar

Use this instead

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

What I did was create a function to replace this functionality, like so:

protected function setPageContent($content)
{
    return view($this->layout, ['content' => $content]);
}
2 likes
hexhex's avatar

I wondered why Taylor removed Controller Layouts, but I think it's a better architecture to put the reference to the master layout in the view itself - templating logic should not be included in Controllers.

So if you want to stay with best practices, I would use yield and section instead.

brainbox's avatar

Hey @ratiw

Could you please provide a working example? I've tried your code with every combination that make sense and nothing is working. What am I missing?

Thank you, Kris

nolros's avatar

@ratiw like the approach, however, it forces me to call the method in the Base Controller from within a child Controller i.e. setPageContent feels more like a helper method, unless I'm missing something. Would prefer all my controllers to inherit from Base as per with setupLayout. I suppose your could call the method from from a bootstrap file. Also, with setupLayout I was able to init other global vars and view::share with the rest of my app e.g. Auth::user.

I apologize if I'm missing something here and talking about a different issue.

I've tried to do this in my own ViewShareServiceProvider, but I'm having issues with the service provider approach for some reason.

ryun's avatar

I personally I like having the control of choosing the layout from a controller, bad design or not it's handy.. This is what I have been playing with.

<?php namespace App\Http\Controllers;

use Illuminate\Routing\Controller;

class BaseController extends Controller {

    protected $layout = 'core::layouts.default';

    /**
     * Show the user profile.
     */
    public function setContent($view, $data = [])
    {

        if ( ! is_null($this->layout))
        {
            return $this->layout->nest('child', $view, $data);
        }

        return view($view, $data);

    }

    /**
     * Set the layout used by the controller.
     *
     * @param $name
     * @return void
     */
    protected function setLayout($name)
    {
        $this->layout = $name;
    }

    /**
     * Setup the layout used by the controller.
     *
     * @return void
     */
    protected function setupLayout()
    {
        if ( ! is_null($this->layout))
        {
            $this->layout = view($this->layout);
        }
    }


    public function callAction($method, $parameters)
    {
        $this->setupLayout();

        $response = call_user_func_array(array($this, $method), $parameters);


        if (is_null($response) && ! is_null($this->layout))
        {
            $response = $this->layout;
        }

        return $response;
    }
}
2 likes

Please or to participate in this conversation.