laravel5 routing parameter rules problem
習慣沉默
習慣沉默 2017-05-16 16:56:21
0
2
726

The following routing rules limit the id to numbers. If it is not a number, an error will be reported. How to set it to prompt an incorrect parameter, a non-existent article, or jump to the homepage when it is not a number?

Route::get('article/{id}', function($id) {
    return 'Article:' . $id;
})->where('id', '[0-9]+');
習慣沉默
習慣沉默

reply all(2)
我想大声告诉你

Method 1

Route::get('article/{id}', function($id) {
    if(is_numeric($id)) {
        return 'Article:' . $id;
    } else {
        return 'Index'
    }
});

Method 2

Route::group(['prefix' => 'article/'], function() {
    Route::get('{id}', function($id) {
        return 'Article:' . $id;
    })->where('id', '[0-9]+');
    Route::get('{id}', function($id) {
        return 'Index:'.$id;
    });
});
洪涛

Create fileresourcesviewserrors404.blade.php Non-existing routes will jump to this file, the specific processing is written in this file

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template