There is Laravel Middleware, which should redirect if the user is not logged in with the next. content:

public function handle($request, Closure $next) { if (Auth::guest()) { redirect('/login'); } return $next($request); } 

And the route:

 Route::get('/solditems', ['middleware' => 'unauthorised', function(){}], 'PostController@sold'); 

But middleware does not work and instead of redirection, I get to where the route leads. What is the problem?

  • and everything is registered in $routeMiddleware ? - maxkrasnov
  • $routeMiddleware spelled 'unauthorised' => Middleware\IfUnAuthorized::class, - RedScreed

1 answer 1

Try to register like this

 public function handle($request, Closure $next) { if (Auth::guest()) { return redirect('/login'); } return $next($request); }