Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

yagrasdemonde's avatar

How can I share routes between users and members ?

I have an application for users and members. I’m using spatie permission package to define roles. Each have a specific space after login defined with prefix on routes (see below), Members can access only to their dashboard, whereas users can access to their dashboard and members routes.

To best explain here is my environment

//LoginController

protected function redirectTo()
{
    if ( Auth::user()->hasExactRoles(‘members’) ) {
        return route('dashboard.members);
    }

    return route('dashboard.users);
}
//RedirectIfAuthenticated middleware

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        if(auth()->user()->hasExactRoles(‘members’)){
            return redirect()->route('dashboard.members);
        }
        return redirect()->route('dashboard.users);
;
    }

    return $next($request);
}
//MembersMiddleware

public function handle($request, Closure $next)
{
    if (Auth::user()->hasExactRoles(‘members’)) {
           abort(401, 'Access denied');
    }
    return $next($request);
}

Routes

//Users

Route::middleware(['auth', ‘Members’])->prefix(‘users)->group(function () {
	Route::controller(HomeController::class)->group(function () {
    		Route::get(‘/dashboard’, 'index')->name('home');
    	});

});

//Members

Route::middleware(['auth'])->prefix(‘members’)->group( function () {
	Route::controller(HomeMemberController::class)->group(function () {
    		Route::get(‘/dashboard’, 'index')->name('home.members’);
    	});
});

//Wanted Shared routes

Route::middleware(['auth'])->group( function () {
	Route::controller(HelpController::class)->group(function () {
    		Route::get(‘/help’, 'index')->name('help.index');
    		Route::get(‘/help/thanks’, 'show')->name('help.thanks’);
   		 Route::post(‘/help’, 'send')->name('help.send');
	});
});

So if I connect with user (with ‘User’ role) I have a corresponding dashboard, and url is localhost/users/dashboard

And same if I connect with user (with ‘Member’ role) I have a corresponding dashboard, and url is localhost/members/dashboard

And Members can’t access to users dashboard with MembersMiddleware

In these dashboards views, I want to show a link to help route (help.index) But now how can I do to have these urls :

localhost/users/help
localhost/members/help

with same views, form etc because users and members can ask some help by submitting a form. What is the trick to put users or members prefix in appropriate space before help in url ?

Thanks for your tips and advice.

0 likes
2 replies
yagrasdemonde's avatar

I would like to avoid repeating the declarations of these routes

Please or to participate in this conversation.