我正在使用 Laravel 5 并在本地工作。 我创建了一个参数为 {id} 的路由和另一个具有特定名称的路由,如下所示:
Route::get('contacts/{id}', 'ContactController@get_contact');
Route::get('contacts/new', 'ContactController@new_contact');
我的问题是,如果我尝试访问 localhost/contacts/new,它将自动访问 get_contact 方法。我知道我已经制作了一个 {id} 参数,但是如果我只想在我的参数是整数时调用 get_contact 怎么办?如果不是,请检查它是否是“新的”并访问 new_contact 方法。然后,如果它不是整数且不是"new",则错误页面 404。
我如何在 Laravel 5 中做到这一点?
感谢您的帮助!
最佳答案
只需添加 ->where('id', '[0-9]+')
到您想要接受纯数字参数的路由:
Route::get('contacts/{id}', 'ContactController@get_contact')->where('id', '[0-9]+');
Route::get('contacts/new', 'ContactController@new_contact');
阅读更多:http://laravel.com/docs/master/routing#route-parameters
关于php - 如果参数不是整数,如何以不同方式定义路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30414860/