slim framework方法不允许使用中间件进行异常处理

时间:2017-10-07 13:10:38

标签: php exception exception-handling request slim

我正在制作一个关于苗条框架的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请求,我得到了“不允许的方法”例外。

我想在中间件中处理这个异常。是否有可能,如果我该怎么办?

我想学习编写和使用中间件。

请帮帮我......

1 个答案:

答案 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);