1 回答
TA贡献1851条经验 获得超5个赞
在 Slim 4 中,您可以将自定义错误处理程序添加到 ErrorMiddleware。您还可以在 ErrorMiddleware 之前添加自己的中间件以捕获和映射您自己的异常:
例子
<?php
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Exception\HttpNotFoundException;
use Slim\Middleware\ErrorMiddleware;
use Slim\Psr7\Response;
// ...
// HttpNotFound Middleware
$app->add(function (
ServerRequestInterface $request,
RequestHandlerInterface $handler
) {
try {
return $handler->handle($request);
} catch (HttpNotFoundException $httpException) {
$response = (new Response())->withStatus(404);
$response->getBody()->write('404 Not found');
return $response;
}
});
$app->add(ErrorMiddleware::class);
- 1 回答
- 0 关注
- 135 浏览
添加回答
举报