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

MartelliEnrico's avatar

Laravel 4.3 and View::share

Today I started porting my app to the Laravel 4.3 structure, and a problem came out... In my BaseController, in the setupLayout method, I have a couple of View::share declaration, but once I try to open the website, I get an error that say Undefined variable: currentUser (this is one of the variable shared from the BaseController).

Was there any change to View::share with the 4.3 update, or maybe with the setupLayout on the controller... I can't find the problem anywhere...

Here's the setupLayout method and the stack trace:

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

        View::share([
            'currentUser' => Auth::user(),
            'errorsTemplate' => '<span class="glyphicon glyphicon-remove form-control-feedback"></span> <span class="help-block">:message</span>'
        ]);
    }
Next exception 'ErrorException' with message 'Undefined variable: currentUser (View: /home/vagrant/Code/cookbook/resources/views/layouts/partials/nav.blade.php) (View: /home/vagrant/Code/cookbook/resources/views/layouts/partials/nav.blade.php) (View: /home/vagrant/Code/cookbook/resources/views/layouts/partials/nav.blade.php)' in /home/vagrant/Code/cookbook/storage/views/f1f2308e03e0c0d48b16b8ba7f6f02fb:21
Stack trace:
#0 /home/vagrant/Code/cookbook/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php(39): Illuminate\View\Engines\CompilerEngine->handleViewException(Object(ErrorException))
#1 /home/vagrant/Code/cookbook/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(56): Illuminate\View\Engines\PhpEngine->evaluatePath('/home/vagrant/C...', Array)
#2 /home/vagrant/Code/cookbook/vendor/laravel/framework/src/Illuminate/View/View.php(135): Illuminate\View\Engines\CompilerEngine->get('/home/vagrant/C...', Array)
#3 /home/vagrant/Code/cookbook/vendor/laravel/framework/src/Illuminate/View/View.php(103): Illuminate\View\View->getContents()
#4 /home/vagrant/Code/cookbook/vendor/laravel/framework/src/Illuminate/View/View.php(77): Illuminate\View\View->renderContents()
#5 /home/vagrant/Code/cookbook/vendor/laravel/framework/src/Illuminate/Http/Response.php(43): Illuminate\View\View->render()
#6 /home/vagrant/Code/cookbook/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Response.php(202): Illuminate\Http\Response->setContent(Object(Illuminate\View\View))
#7 /home/vagrant/Code/cookbook/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1500): Symfony\Component\HttpFoundation\Response->__construct(Object(Illuminate\View\View))
#8 /home/vagrant/Code/cookbook/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1031): Illuminate\Routing\Router->prepareResponse(Object(Illuminate\Http\Request), Object(Illuminate\View\View))
#9 /home/vagrant/Code/cookbook/vendor/laravel/framework/src/Illuminate/Routing/Router.php(994): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#10 /home/vagrant/Code/cookbook/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(827): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#11 /home/vagrant/Code/cookbook/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(804): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#12 /home/vagrant/Code/cookbook/vendor/laravel/framework/src/Illuminate/Session/Middleware.php(72): Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request), 1, true)
#13 /home/vagrant/Code/cookbook/vendor/laravel/framework/src/Illuminate/Cookie/Queue.php(47): Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request), 1, true)
#14 /home/vagrant/Code/cookbook/vendor/laravel/framework/src/Illuminate/Cookie/Guard.php(51): Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1, true)
#15 /home/vagrant/Code/cookbook/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1, true)
#16 /home/vagrant/Code/cookbook/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(660): Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request))
#17 /home/vagrant/Code/cookbook/public/index.php(49): Illuminate\Foundation\Application->run()
#18 {main} [] []
0 likes
15 replies
stevemo's avatar
stevemo
Best Answer
Level 1

Looking at Illuminate\Routing\Controller the callAction method do not call setupLayout method anymore.

in your baseController you can do


function __contruct() { $this->setupLayout(); } protected function setupLayout() { if ( ! is_null($this->layout)) { $this->layout = View::make($this->layout); } View::share([ 'currentUser' => Auth::user(), 'errorsTemplate' => '<span class="glyphicon glyphicon-remove form-control-feedback"></span> <span class="help-block">:message</span>' ]); }
isimmons's avatar

Ran into that issue yesterday. While the constructor works just as well, another option is to place those View::share lines in the boot method of the existing AppServiceProvider or create another service provider specifically for view stuff.

2 likes
lkoenigsberger's avatar

Hi at all, I've tried to put the View::share into the boot method, but it only works when I`m not logged in!! If I´m logged out it displays null which I think is correct, but if I log in it show this error

Call to a member function connection() on null
lazychaser's avatar

So I guess that layout-based controllers aren't now in favor?

MartelliEnrico's avatar

@wiggerl3000 you have to put the app service providers (the first block) after the illuminate service providers (the second block), because in the boot() method, if you are logged in you call an Eloquent query, but the DatabaseServiceProvider isn't event called yet.

1 like
brnlbs's avatar

I have the same issue with View::share but none of the solutions here seems to work for me.

If I put the View::share() in my BaseController and extend the BaseController the View::share() seems to get ignored. If I put the View::share() directly into a function before the View gets rendered everything works fine.

I also tried it with a ServiceProvider (which I load after all Illuminate Providers) and the Provider gets executed but the View::share() from there also gets ignored.

Here is some related code of the solutions I tried. Are there any issues I don't see? https://gist.github.com/brnlbs/4b8f79fc47a6f6021521

MartelliEnrico's avatar

@brnlbs if you extend this class, and you put some code in the constructor, like:

public function __construct(FooBar $foo)
{
$this->foo = $foo;
}

you will have to call the parent constructor, so the construct will be something like this:

public function __construct(FooBar $foo)
{
parent::__construct();
$this->foo = $foo;
}
1 like
joselara's avatar
App::booted(function()
{
    View::share('currentUser', Auth::user());
});

I did this the AppServiceProvider under the register function. Works for me.

chuuke's avatar

@joselara, this always returns null for me in the view. The variable comes in, but it is null. Calls to \Auth->user(); within the template return the correct user object.

Any ideas?

AppServiceProvider.php

<?php namespace App\Providers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider {

 /**
  * Bootstrap any application services.
  *
  * @return void
  */
 public function boot()
 {

 }

 /**
  * Register any application services.
  *
  * @return void
  */
 public function register()
 {
  // This service provider is a great spot to register your various container
  // bindings with the application. As you can see, we are registering our
  // "Registrar" implementation here. You can add your own bindings too!

  $this->app->bind(
   'Illuminate\Contracts\Auth\Registrar',
   'App\Services\Registrar'
  );


  $this->app->booted(function()
  {
   View::share('currentUser', Auth::user());
  });

 }

}

davemcl's avatar

Use a View Composer, its well documented in the "Views" section.

Please or to participate in this conversation.