简体   繁体   中英

Laravel 5.4 Routes

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.

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

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

Or just with a foreach loop :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM