bashleigh
bashleigh

Reputation: 9314

Laravel, same URI, different route name, different middleware causes over loop

In laravel I've simply done this:

Route::group(["middleware"    =>    "admin"], function() {

    Route::get("/", "UserController@index")->name("user_index");

});


Route::group(["middleware"    =>    "user", "as"    =>    "User::"], function() {

    Route::get("/", "DocumentController@index")->name("user_index");

});

The problem is when I am logged in as my Admin auth middleware, when going to "/" my browser returns too many redirects and stops. I'm guessing because the second route is removing this as when I print out php artisan route:list there is only one result for "/" and that's with the user middle's parameters so it's defo overriding the previous route.

What I don't understand is why would it do this is both have a separate middleware?

Both middlewares are extremely simple. Below is my admin

public function handle($request, Closure $next)
{

    if ( Auth::check() && Auth::user()->hasRole("customer_service") )
    {
        return $next($request);
    }

    return redirect("/");
}

And my user's middleware is exactly alike except the role is different

Upvotes: 0

Views: 890

Answers (1)

bashleigh
bashleigh

Reputation: 9314

This is probably wrong but this is what I did to fix this particular issue with the above.

public function index() {

    return \Auth::user()->hasRole("trainer") ? \App::call("App\Http\Controllers\Trainer\UserController@index")
    : \App::call("App\Http\Controllers\User\UserController@index");
}

Upvotes: 0

Related Questions