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

murtaza1904's avatar

Is their any way to add a region name as a prefix to all routes

I am working on the multi region website and all the authenticated routes contains the region name as a prefix expect the guest routes. As the user login to the portal, he will be redirect to its desire region dashboard.

For example a user with region oc logged in then he will be redirect to

http://127.0.0.1:8000/oc/dashboard

I can just group the route and apply the prefix function to add region to all routes like this

Route::prefix('{reg}')->group(function ()
{
	// all the routes will come here
});

But the issue is that then I need to pass the region name to all the links like this

<a href="{{ route('dashboard', ['reg' => 'oc']) }}">Dashboard</a>

But as the region will be same for all the links then why need to pass the region for all the links

So is their any other way that I can add prefix without needed to pass the region name to all the links

Note: I can't hardcode the region name to the prefix function because user can have different region, and for some user roles, user can change the region from dashboard

0 likes
3 replies
dualklip's avatar

Hi @murtaza1904

Maybe I need more info about the project, for example how the regions are stored or linked with the user, but a way could be to set it in the session or authentication guard (after login) and then retrieve it from there when needed. So, in your login controller, you could do something like:

// After validation and checking credentials
auth()->login($user);
session(['region_prefix' => $user->region]);
return redirect()->route('dashboard');

And to create URLs, you could make a helper function like this:

function region_route($name, $parameters = [], $absolute = true)
{
    return app('url')->route($name, array_merge(['reg' => session('region_prefix')], $parameters), $absolute);
}

Then in your views, instead of route('dashboard'), you can use region_route('dashboard') and it will append the region automatically without having to pass it manually. The group routes should be like this:

Route::prefix('{reg}')->group(function ()
{
    // all the routes will come here
});

This should work with your current {reg} prefix, but remember to replace all hard-coded region prefixes {reg} with session('region_prefix') to ensure user can change the region from dashboard.

(no tested)

murtaza1904's avatar

@dualklip But the issue is that the project is almost at the end stage and their are so many routes. I need to do it do it for each and then test them one by one. I their any other way that I not needed to change the current route functions

dualklip's avatar

Taking into account two assumptions you impose on the problem, which are:

  • user with region oc logged in then he will be redirect to http://127.0.0.1:8000/oc/dashboard
  • I can't hardcode the region name to the prefix function because user can have different region

The only option I can think of (but it seems super dirty to me) is to do a redirect within the Route function.

Route::get('/dashboard', function (Request $request) {
    if($request->user()->region == 'OC'){
        redirect('/oc/dashboard');
    }
});

If you could sacrifice the need for the region to appear in the url then you could make a conditional within the Route to send to the Dashboard of the region without the need to redirect.

Route::get('/dashboard', function (Request $request) {
    if($request->user()->region == 'OC'){
        return Inertia::render('Dashboard-OC', []);
    } else {
        return Inertia::render('Dashboard-OTHER', []);
    }
});

Please or to participate in this conversation.