【问题标题】:Replacement for notFoundHandler setting替换 notFoundHandler 设置
【发布时间】:2021-11-02 00:57:42
【问题描述】:

我正在从 Slim/3 迁移到 Slim/4。除了404 Not Found Handler(现已消失的App::$settings 的一部分)之外,我已经找到或想出了我正在使用的所有已删除功能的替代品:

Slim App::$settings 已被移除,已实现多个中间件来替换每个单独设置的功能。

notFoundHandler 有中间件吗?如果没有,我该如何实现?

我以前的样子是这样的:

use Slim\Container;
$config = new Container(); 
$config['notFoundHandler'] = function (Container $c) {
    return function (Request $request, Response $response) use ($c): Response {
        $page = new Alvaro\Pages\Error($c);
        return $page->notFound404($request, $response);
    };
};

【问题讨论】:

    标签: php slim slim-4


    【解决方案1】:

    根据Slim 4 documentation on error handling

    每个 Slim Framework 应用程序都有一个错误处理程序,用于接收所有 未捕获的 PHP 异常

    您可以设置自定义错误处理程序来处理引发的每种类型的异常。同一页面上提供了预定义异常类的列表。

    这是一个非常基本的示例,说明如何将 closure 注册为错误处理程序,以仅处理 HttpNotFoundException 异常。您还可以将处理程序放在扩展 Slim\Handlers\ErrorHandler 的类中。另外,我实际上并没有使用您的Alvaro\Pages\Error 来生成响应,但是更改它应该很简单:

    <?php
    
    require '../vendor/autoload.php';
    
    $app = Slim\Factory\AppFactory::create();
    
    // Define Custom Error Handler
    $customErrorHandler = function (
        Psr\Http\Message\ServerRequestInterface $request,
        \Throwable $exception,
        bool $displayErrorDetails,
        bool $logErrors,
        bool $logErrorDetails
    ) use ($app) {
        $response = $app->getResponseFactory()->createResponse();
        // seems the followin can be replaced by your custom response
        // $page = new Alvaro\Pages\Error($c);
        // return $page->notFound404($request, $response);
        $response->getBody()->write('not found');
        return $response->withStatus(404);
    };
    
    // Add Error Middleware
    $errorMiddleware = $app->addErrorMiddleware(true, true, true);
    // Register the handler to handle only  HttpNotFoundException
    // Changing the first parameter registers the error handler for other types of exceptions
    $errorMiddleware->setErrorHandler(Slim\Exception\HttpNotFoundException::class, $customErrorHandler);
    
    
    $app->get('/', function ($request, $response) {
        $response->getBody()->write('Hello Slim 4');
        return $response;
    });
    
    $app->run();
    

    另一种方法是创建一个通用错误处理程序并将其注册为默认处理程序,并在该处理程序内部根据抛出的异常类型决定应发送什么响应。比如:

    $customErrorHandler = function (
        Psr\Http\Message\ServerRequestInterface $request,
        \Throwable $exception,
        bool $displayErrorDetails,
        bool $logErrors,
        bool $logErrorDetails
    ) use ($app) {
        $response = $app->getResponseFactory()->createResponse();
    
            if ($exception instanceof HttpNotFoundException) {
                $message = 'not found';
                $code = 404;
            } elseif ($exception instanceof HttpMethodNotAllowedException) {
                $message = 'not allowed';
                $code = 403;
            }
            // ...other status codes, messages, or generally other responses for other types of exceptions
    
        $response->getBody()->write($message);
        return $response->withStatus($code);
    };
    

    然后您可以将其设置为默认错误处理程序:

    $errorMiddleware = $app->addErrorMiddleware(true, true, true);
    $errorMiddleware->setDefaultErrorHandler($customErrorHandler);
    

    【讨论】:

      猜你喜欢
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 2013-08-05
      • 1970-01-01
      相关资源
      最近更新 更多