Php 路由正则表达式检查数据库中是否存在id
如果只希望接受管线的特定参数,请执行以下操作:Php 路由正则表达式检查数据库中是否存在id,php,laravel,laravel-4,Php,Laravel,Laravel 4,如果只希望接受管线的特定参数,请执行以下操作: Route::get('users/{id}', 'UsersController@show')->where('id', '\d+'); 但是如果我想要像这样的东西呢: Route::get('users/{id}', 'UsersController@show')->where('id', '\d+')->where(this id exists in table column id); laravel是如何做到这一点的?
Route::get('users/{id}', 'UsersController@show')->where('id', '\d+');
但是如果我想要像这样的东西呢:
Route::get('users/{id}', 'UsersController@show')->where('id', '\d+')->where(this id exists in table column id);
laravel是如何做到这一点的?如果数据库中还没有id,我想返回未找到的页面
更新 我想创建一个过滤器:
Route::filter('user.exists', function()
{
//get the id then check the database
});
但是如何将$id传递给过滤器
注 我知道我可以做一些事情,比如检查查询是否返回了控制器中的任何数据,如果现在这样,则抛出404:
$user = User::find($id);
if(!$user){
App::abort(404);
}
但我认为如果我能在路线上做到这一点,它会更干净。因为我不仅要过滤用户,还要过滤更多内容。这将帮助您开始:
Route::get('users/{id}', 'UsersController@show', array('before' => 'userExists'))->where('id', '[\d]+');
Route::filter('userExists', function($id) {
echo $id; exit;
});