如何在Laravel中为URL参数创建模式

问题描述 投票:1回答:1

我在RouteServiceProvider.php目录中的app/Providers文件中编写了这段代码。

在方法启动中:

$this->pattern('{id}', '[0-9]+');

然后根据我读到的,我想如果我写这段代码:

Route::get('/user/{id}', function ($id){
    return $id;
});

在route目录中的web.php文件中,id参数只接受int值,但是当我测试这个url时:http://127.0.0.1:8000/user/a我看到a被返回了。

问题出在哪里,我该如何制作模式?

php laravel laravel-5 laravel-routing
1个回答
1
投票

根据the docs你应该使用Route:: facade而不是$this->id而不是{id}

public function boot()
{
    Route::pattern('id', '[0-9]+');

    parent::boot();
}
© www.soinside.com 2019 - 2024. All rights reserved.