Skip to content
Advertisement

Laravel application URL with directory and route group

The laravel application url will be something like app.laravel.com{clientName}. All the routes will be following the client_name, for example app.laravel.com{clientName}home, app.laravel.com{clientName}profile. Will load/ render the application depends on the clientName.

In routes/web.php I declare two routes

Route::get('/{clientName}', 'ClientController@index');
Route::get('/{clientName}/home', 'HomeController@index');

And the ClientController will look for the clientName in the database and load all the client properties and will share it. If the client name doesn’t exists it will abort(404).

The issue is HomeController currently doesn’t do this check, so whatever clientName I use everything go through & show the home page.

My question is how can I group all my routes to follow after clientName and all the routes will go through the clientName validation?

I thought about middleware, If I’m using a common middleware how can I access the clientName in the route, in the middleware?

Advertisement

Answer

According to Laravel official documentation, You may also use the prefix parameter to specify common parameters for your grouped routes:

Route::group(['prefix' => 'accounts/{account_id}'], function () {
    Route::get('detail', function ($account_id)    {
        // Matches The accounts/{account_id}/detail URL
    });
});

Hope it helps.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement