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

mstdmstd's avatar

1 routes rule for 4 urls

Hello, In laravel 5.5 I want to write 1 routes rule to make all next urls:

http://host.com/admin/category/list
http://host.com/admin/category/index
http://host.com/admin/category/
http://host.com/admin/category

to work for 1 control action.

I tried to write in routes :

Route::get('/admin/category/{name}', 'Admin\CategoryController@index')->where('name','[\/|index|list]+');

But only 2 first urls works ok, but the last 2 urls do not work. Which is the correct way ?

Thanks!

0 likes
3 replies
tykus's avatar

name is optional, so:

Route::get('/admin/category/{name?}', 'Admin\CategoryController@index')
    ->where('name','[index|list]');
mstdmstd's avatar

Thank you, But in this case urls for editor are triggered by this route, like

/admin/category/edit?id=7&page=1

I tried to exclude “edit” from this route, like :

Route::get('/admin/category/{name?}', 'Admin\CategoryController@index')->where('name','[index|list|!edit]+');

But failed.Which is the right way ?

tykus's avatar
tykus
Best Answer
Level 104

Write a specific route for edit, an place it before optional name route, so that if is matched first:

Route::get('/admin/category/edit','Admin\CategoryController@edit')
    ->where('name','[index|list]');
Route::get('/admin/category/{name?}', 'Admin\CategoryController@index')
    ->where('name','[index|list]');
1 like

Please or to participate in this conversation.