当你想只接受路由的一个特定参数时,你可以这样做:
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;
});