6 years later and i was having the same problem. thanks for sharing!
Feb 16, 2016
5
Level 7
Problem accessing $request->user() in my Middleware.
Hi, so I am following along with the video 'Ogres Are Like Middleware' in 'Laravel 5 Fundamentals' and I am having some issues with the 'RedirectIfNotAManager.php' middleware.
This is the code I have and the problem is that $request->user() returns null as opposed to my user so I can't use my isATeamManager() function.
RedirectIfNotAManager.php
<?php
namespace App\Http\Middleware;
use Closure;
class RedirectIfNotAManager
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
dd($request->user());
if (!$request->user()->isATeamManager()){
return redirect('articles');
}
return $next($request);
}
}
Kernel.php
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'manager' => \App\Http\Middleware\RedirectIfNotAManager::class,
];
Routes.php
Route::get('foo', ['middleware' => 'manager', function() {
return 'This page may only be viewed by managers.';
}]);
Update: I managed to fix it by putting the middleware route into the ['middleware' => 'web'] route group.
Please or to participate in this conversation.