1 回答

TA貢獻1851條經(jīng)驗 獲得超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 關(guān)注
- 157 瀏覽
添加回答
舉報