5

I'm trying to pass to all templates current user object like this:

class Controller extends BaseController { public function __construct() { view()->share('usr', Auth::guard('user')); } } 

Every controller is extended by Controller. But if i try to dump Auth::guard('user')->user() Laravel returns null, although I am logged in. Moreover when i pass this variable into template, {{ $usr->user() }} returns current user. What I've done wrong?

my config/auth.php

'defaults' => [ 'guard' => 'user', 'passwords' => 'users', ], 'guards' => [ 'user' => [ 'driver' => 'session', 'provider' => 'user', ], ], 'providers' => [ 'user' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], ], 

Kernel.php

 protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, ]; /** * The application's route middleware groups. * * @var array */ protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, //\App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, ], 'api' => [ 'throttle:60,1', 'bindings', ], ]; /** * The application's route middleware. * * These middleware may be assigned to groups or used individually. * * @var array */ protected $routeMiddleware = [ 'auth' => \Illuminate\Auth\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, ]; 

my own function to log in:

public function authorizes(Request $request) { $this->validate($request, [ 'login' => 'required|max:50', 'password' => 'required|max:50' ]); $credentials = $request->only(['login', 'password' ]); $remember = $request->get('remember', false) == 1 ? true : false; if ($this->guard->attempt( $credentials, $remember)) { $user = $this->guard->user(); $user->last_login = date('Y-m-d H:i:s'); $user->save(); return redirect()->route( 'homepage' )->withSuccess(trans('app.login.success')); } return redirect()->back()->withErrors(trans('app.wrong.credentials')); } 

4 Answers 4

3

In Laravel 5.3 you should change your controller constructor like so to make this work (assuming you use at least Laravel 5.3.4):

public function __construct() { $this->middleware(function ($request, $next) { view()->share('usr', Auth::guard('user')); return $next($request); }); } 

You can see this change described in Upgrade guide:

In previous versions of Laravel, you could access session variables or the authenticated user in your controller's constructor. This was never intended to be an explicit feature of the framework. In Laravel 5.3, you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet.

As an alternative, you may define a Closure based middleware directly in your controller's constructor. Before using this feature, make sure that your application is running Laravel 5.3.4

Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct answer, literally ran into this yesterday!
2

Try to do this:

view()->share('usr', Auth::user()); 

Or this:

view()->share('usr', auth()->user()); 

3 Comments

null in both cases
Are you sure you're logged in? Please double check this. Did you change sessions related functionality or something? Did you add web middleware to your routes manually?
in other controllers, which are extended by this Controller it returns current user... 100% that im logged in! Route::group(['middleware' => 'auth'], function () { // all my routes } Route::get('/login', [ 'as' => 'login', 'uses' => 'AuthController@login' ] ); Route::get('/logout', [ 'as' => 'logout', 'uses' => 'AuthController@logout' ] ); Route::post('/login', [ 'as' => 'authorize', 'uses' => 'AuthController@authorizes' ] );
0

Frist check if user is logged or not, then share the current user as:

public function __construct() { if (auth()->check()) { $this->currentUser = auth()->user(); View::share('currentUser', $this->currentUser); } else { // you can redirect the user to login page. } } 

1 Comment

but in Controller it always returns false. In other controllers which are extended by Controller, this returns true
0

In your case there are two things to consider

  1. To get actual user model you should do Auth::guard('user')->user()
  2. Auth::user() is actually not yet initialized when view()->share() is called, see https://github.com/laravel/framework/issues/6130

Therefore you could rather use view composer. In boot method of your AppServiceProvider add:

\View::composer('*', function ($view) { $view->with('usr', \Auth::guard('user')->user()); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.