我正在制作一个关于苗条框架的API,我有一些像
这样的路线$app->group('/api', function() use ($app){
$app->post('/login', '\App\Controllers\Api\Auth\AuthController:postSignIn');
$app->post('/register', '\App\Controllers\Api\Auth\RegisterController:postRegister');
});
如果我对任何链接使用get请求,我得到了“不允许的方法”例外。
我想在中间件中处理这个异常。是否有可能,如果我该怎么办?
我想学习编写和使用中间件。
请帮帮我......
答案 0 :(得分:0)
您可以为此路线添加middellare函数,例如此函数
$GetHandeler=function($req,$res,$next){
if($res->isGet()){
return $res->getBody()->write("Get Method not allowed");
}
if($res->isPost())
$res=$next($req,$res);
return $res;
};
之后,您可以将此功能添加到您的路线中,如此
$app->group('/api', function() use ($app){
$app->post('/login', '\App\Controllers\Api\Auth\AuthController:postSignIn');
$app->post('/register', '\App\Controllers\Api\Auth\RegisterController:postRegister');
})->add($GetHandeler);