Reputation: 152
I currently have several routes in Laravel 5 but I want to combine them so instead of them being separate, I want them grouped, how can I do this?
Route::get('url1', function() {
return Redirect::to('/');
}
Route::get('url2', function() {
return Redirect::to('/');
}
Route::get('url3', function() {
return Redirect::to('/');
}
How can I make just a single route so I dont have to repeat it like maybe:
Route::get('url1','url2','url3', function(){
return Redirect::to('/');
});
Thank You.
Upvotes: 3
Views: 116
Reputation: 313
Or just with a foreach
loop :)
foreach(['url1', 'url2', 'url3'] as $url)
{
Route::get($url, function() {
return Redirect::to('/');
}
}
Upvotes: 0
Reputation: 13562
You can use a regular expression for your routes:
Route::get('{url}', function ($name) {
Redirect::to('/');
})->where('url', 'url[0-9]+');
This will redirect all routes with a number behind url
If they are different you can use the same logic:
Route::get('{url}', function ($name) {
Redirect::to('/');
})->where('url', 'url1|url2|url3');
Upvotes: 1
Reputation: 924
What about this code.you can use a dynamic route parameter. Let me know your thoughts?
Route::get('{slug?}', function($slug = 'index')
{
return Redirect::to('/');
})->where('slug', '(url1|url2|url3)');
Upvotes: 0