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

engrlaravel's avatar

Add dynamic prefix in laravel routes web.php

I have below code example in my web.php

Route::group(['prefix'=>'admin'],function (){

Route::group(['prefix' => 'admin','middleware' => 'auth'], function () {

Route::group(['prefix' => 'candidate','middleware' => 'auth'], function () {


Route::get('login', 'frontend\LoginController@login');

Route::get('/home', 'DashboardController@index')->name('home');

I have some group & some with out groups.

Questions:

1- How to get dynamic value from DB

2- How i can add that value prefix before all above?

Example

example.com/company_name/admin

example.com/company_name/home
0 likes
5 replies
MikeMacDowell's avatar

Are you trying to create a multi-tenanted application?

Functionally doing what you ask is pretty simple, but I'm not sure you actually want to do this as what you're asking for is to create a pre-defined set of routes for every company_name, which is just going to duplicate the entire routes file across however many companies in your database (which means that the routes can't be cached etc...)

I think what you instead want, is for all routes to require a company_name as a wildcard which then triggers a tenant to be loaded for that request?

You'd do that like this:

Route::group([
    'prefix'     => '/{tenant}',
    'middleware' => \App\Http\Middleware\IdentifyTenant::class,
    'as'         => 'tenant:',
], function () {
    // Tenant routes here
});

Taken from https://ollieread.com/articles/laravel-multi-tenancy-avoiding-over-engineering which I've used to implement multi-tenancy on an application previously

engrlaravel's avatar

@mikemacdowell : Simply, I just want to create url having each company name who is using my application( who is registered with me).

For example, If you are login(assume your company name is mikemacdowell) so the route will be example.com/mikemacdowell/.....

and if i am login(company name is laravel) so example.com/laravel/......

MikeMacDowell's avatar

That's a multi-tenanted application if you're then segmenting all the data for each company based upon the URL.

IF you still want to just pre-generate all the URLs, you can do it like this, but be warned, for each company you add to the database you're duplicating all your routes again for that extra company, so if you have 100 routes, and 10 companies then you'll have 1000 routes.

Company::all()->each(function($company) {

    Route::group([
        'prefix' => $company->name, 
        'name' => $companyName . '.',
    ], function() {
    // routes go here
    });

});


MikeMacDowell's avatar
Level 25

Like my previous reply, with a wildcard as a route prefix which you then check in a middleware if a valid Customer exists (or load a tenant based upon the prefix).

Route::group([
    'prefix'     => '/{customer_name}',
    'middleware' => \App\Http\Middleware\IdentifyCustomer::class,
    'as'         => 'customer:',
], function () {
    // routes here
});

Check out the link I posted for how you'd do this and load only the information for that customer on every other database request.

Please or to participate in this conversation.