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

M4verick's avatar

Laravel Livewire dynamic route prefix

Hello everyone ,

I'm working on a web application in Laravel Livewire and i am struggling.

I have different user roles (admin, manager, doctor ect...) and want to redirect them into their dashboard within a prefix (e.g project.com/admin/dashboard | project.com/manager/dashboard).

I have tried in this way but i dont like it :

Route::group(['prefix' => 'manager'], function () {
    Route::get('/dashboard', ManagerDashboard::class)->name('manager-dashboard');
});
Route::group(['prefix' => 'admin'], function () {
    Route::get('/dashboard', AdminDashboard::class)->name('admin-dashboard');
});

ect...

Is there any method for a dynamic route prefix for example :

Route::get('/{$prefix}/dashboard', [Dashboard::class, 'dashboardRedirect'])->name('dashboard');

Thanks in advance!

0 likes
8 replies
Sinnbeck's avatar

Use this syntax

Route::get('/{prefix}/dashboard', [Dashboard::class, 'dashboardRedirect'])->name('dashboard');

It's not available as the first parameter in the method

1 like
M4verick's avatar

@Sinnbeck sorry it was my mistake , i just edited . But i want to know how to set that parameter , from dashboard controller or even from middleware ?

Sinnbeck's avatar

@M4verick you have a $ in your route. That won't work. Use the name

route('dashboard', ['prefix' => $user->role])
1 like
webrobert's avatar

@m4verick,

I think your first option is fine. I assume you are writing additional pages into those groups too? otherwise maybe just separate routes for each. I personal subscribe to the more controllers style where they are descriptive in name and function so being able to clearly see in your web file the routes and where they go avoids confusion later.

1 like
M4verick's avatar

@webrobert, yeah i have other pages into those groups.

When a user is logged in I redirect them to this route :

Route::get('/dashboard', [Dashboard::class, 'dashboardRedirect'])->name('dashboard');

And this method make the redirection in their relative dashboards

class Dashboard extends Component
{    
    public function dashboardRedirect(){
        switch(Auth::check()){
            case Auth::user()->isClient :
                return redirect()->route('client-dashboard');
                break;
            case Auth::user()->isCourier :
                return redirect()->route('courier-dashboard');
                break;            
            case Auth::user()->isBusiness :
                return redirect()->route('business-dashboard');
                break;            
            case Auth::user()->isManager :
                return redirect()->route('manager-dashboard');
                break;
            case Auth::user()->isAdmin :
                return redirect()->route('admin-dashboard');
                break;
            default :
                return abort(403);
        }
    }

    public function render(){
        return view('livewire.dashboard');
    }

}													

I don't know if this is the correct way to do this

webrobert's avatar

@M4verick,

well there are a bunch of ways to do things. This may sound contradictory from my first reply but my experience has been I try not to over think the url because it often clutters my head and the app, just my opinion. I don't do too many custom urls like this. It can make the route list monstrous. BUT if there is a valid reason to have a different dashboard, do it. That said, are they wildly different?

Sometimes its just a different query or a different menu that is changing.

1 like
M4verick's avatar

@webrobert Yeah , each dashboard is very different from each other , so i really need to seperate them . Thank you for suggestions.

Please or to participate in this conversation.