Laravel 5.4 Routes

I currently have multiple routes in Laravel 5, but I want to combine them, not separately, I want them to be grouped, how can I do that?

Route::get('url1', function() {
    return Redirect::to('/');
}

Route::get('url2', function() {
    return Redirect::to('/');
}

Route::get('url3', function() {
    return Redirect::to('/');
}

      

How can I only make one route so I don't have to repeat it like:

Route::get('url1','url2','url3', function(){
    return Redirect::to('/');
});

      

Thank.

+3


source to share


3 answers


You can use regex for your routes:

Route::get('{url}', function ($name) {
    Redirect::to('/');
})->where('url', 'url[0-9]+');

      

This will redirect all routes numbered per url



If they are different, you can use the same logic:

Route::get('{url}', function ($name) {
    Redirect::to('/');
})->where('url', 'url1|url2|url3');

      

+1


source


How 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)');

      

0


source


Or just using a loop foreach

:)

foreach(['url1', 'url2', 'url3'] as $url)
{
    Route::get($url, function() {
        return Redirect::to('/');
    }
}

      

0


source







All Articles