I have the following in my routes.php
:
Route::get('test/foo', 'TestController@index');
Route::get('test/bar', 'TestController@index');
Route::get('test/baz', 'TestController@index');
and I am trying to reduce this to the following:
Route::get(either 'test/foo' or 'test/bar' or 'test/baz', 'TestController@index');
One documented approach that would sort of apply here, is to place a regex constraint to the route:
Route::get('test/{uri}','TestController@index')->where('uri', 'regex for foo, bar, and baz...');
However, this solution would be ugly. Isn't there an elegant way to just express
{uri} in foo,bar,baz
in Laravel's language? Otherwise, what would the regex look like?
Thanks!
P.S. I've read this, but it didn't apply to my case with 3 routes.
Route groups allow you to share route attributes, such as middleware, across a large number of routes without needing to define those attributes on each individual route.
I'm not sure why do you say that RegEx is ugly. I basically think RegEx is one of the most powerful tools.
In your case, I think the below snippet should do the job:
Route::get('user/{name}', function ($name) { // }) ->where('name', '(foo|bar|baz)');
The (foo|bar|baz)
RegExr will match any of these string: 'foo', 'bar', 'baz'. So, if you need more, just add pipe (|
) and add the needed string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With