search

Home  >  Q&A  >  body text

laravel5 routing parameter rules problem

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]+');
習慣沉默習慣沉默2752 days ago734

reply all(2)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-16 16:58:21

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

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 16:58:21

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

    reply
    0
  • Cancelreply