1

I want to group the Laravel 5 routs based on the logged users and guest users. Is there any inbuilt framework methods in Laravel 5 to do this?

sschale
  • 5,168
  • 3
  • 29
  • 36

2 Answers2

8

Yes, there are some: https://laravel.com/docs/master/middleware#assigning-middleware-to-routes auth for authorized and guest for guests.

Route::group(['middleware' => ['auth']], function () {
    //only authorized users can access these routes
});

Route::group(['middleware' => ['guest']], function () {
    //only guests can access these routes
});
Giedrius Kiršys
  • 5,154
  • 2
  • 20
  • 28
3

Yes, you can do this by updating following method in Authenticate.php

public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->guest()) {

            if ($request->ajax() || $request->wantsJson()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('login');
            }
        }

        return $next($request);
    }

If you are using Sentinel you can check the the logged user from

Sentinel::check() instead of Auth::guard($guard)->guest()

Then you can group the routs as follows.

Route::group(['middleware' => ['auth']], function () {
    // Authorized routs
});

Route::group(['middleware' => ['guest']], function () {
    // Guest routs
});
Manula Thantriwatte
  • 316
  • 1
  • 4
  • 18