Mahaveer's avatar

How to set dynamic route prefix in Laravel?

How to set dynamic route prefix in Laravel?

Can i change prefix name according to user role. Eg. admin or seller.

Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function() {	
	
	Route::get('import', 'Admin\ImportController@index')->middleware('can:isSeller')->name('import');
	
});	
0 likes
2 replies
automica's avatar

@mahaveer are you looking at the following?

Route::group(['middleware' => 'auth'], function() {	
	
	Route::get('import', 'ImportController@index')->name('import');
	
});

and

Route::group(['prefix' => 'admin', 'middleware' => ['auth','can:isAdmin']], function() {	
	
	Route::get('import', 'Admin\ImportController@index')->name('import');
	
});

so your first route is available to everyone, and uses ImportController and second route is only available to admin and uses Admin\ImportController?

if you want to use multiple middlewares for a group you need to specify as an array as above

martinbean's avatar
Level 80

@mahaveer Just register the routes you need:

Route::prefix('admin')->group(function () {
    // Routes for admins
});

Route::prefix('seller')->group(function () {
    // Routes for sellers
});

Please or to participate in this conversation.