How to group multiple routes using the same controller method?

I have the following in routes.php:

Route::get('test/foo', 'TestController@index');

Route::get('test/bar', 'TestController@index');

Route::get('test/baz', 'TestController@index');

and I try to reduce it to the following:

Route::get(either 'test/foo' or 'test/bar' or 'test/baz', 'TestController@index');

One documented approach that is applicable here is to bind a regular expression to a route:

Route::get('test/{uri}','TestController@index')->where('uri', 'regex for foo, bar, and baz...');

However, this decision would be ugly. Isn’t there an elegant way to simply express

{uri} in foo,bar,baz

in Laravel? Otherwise, what does the regex look like?

Thank!

PS I read this one , but this does not apply to my case with three routes.

+4
source share
1 answer

, , RegExr . , .

, :

Route::get('user/{name}', function ($name) { // }) ->where('name', '(foo|bar|baz)');

(foo|bar|baz) RegExr : 'foo', 'bar', 'baz'. , , .

+6

Source: https://habr.com/ru/post/1650860/


All Articles