Slim 4 PHP 中的默认路由 / notFound 错误处理 / HttpNotFoundException

问题描述 投票:0回答:1

我想创建一个与 Slim 4 兼容的自定义错误页面/JSON 回复,当请求不存在的路由时返回。

默认路线(Slim 3)

我最近从 Slim 3 升级到 Slim 4。使用 Slim 3,我有一个完美完成这项工作的默认路由:

   $app->any('/[{path:.*}]', function (Request $request, Response $response, array $args) {
      // catching any other requests...
      /* ... creating JSON error object and write it to $slimresponse ... */
      return ($slimresponse);
   });

但是,当我在 Slim 4 中执行此操作时,出现错误

Type: FastRoute\BadRouteException
Code: 0
Message: Cannot register two routes matching "/" for method "GET"

这显然意味着 Slim 将其识别为 GET / 的双重输入,这在 Slim 4 中是不允许的。

不幸的是,这篇

文章也没有为 Slim 4 提供任何帮助。

未找到

此外,根据

https://www.javaer101.com/en/article/13830039.html,我尝试添加

$app->notFound(function () use ($app) { $app->response->setStatus(403); echo "Forbidden"; //output 'access denied', redirect to login page or whatever you want to do. });
到我的routes.php,但它不起作用:

Call to undefined method Slim\App::notFound()
HttpNotFoundException

最后,我还尝试创建一个错误处理方法(专门针对 HttpNotFoundException,尽管我不知道如何分离 HttpNotImplementedException)

https://www.slimframework.com/docs/v4/middleware/error-handling .html,没有任何成功。

非常感谢任何帮助。

php error-handling http-status-code-404 slim slim-4
1个回答
2
投票
我在搜索了两个或更多小时后才发布了这个问题。

提交问题后,我在这里找到了答案。

https://odan.github.io/2020/05/27/slim4-error-handling.html#捕捉-404-not-found-errors

这是我的新 middleware.php:

use Slim\App; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Slim\Exception\HttpNotFoundException; use Slim\Middleware\ErrorMiddleware; use Middlewares\TrailingSlash; use Slim\Psr7\Response; return function (App $app) { // Parse json, form data and xml $app->addBodyParsingMiddleware(); // Add the Slim built-in routing middleware $app->addRoutingMiddleware(); // always add a trailing slash $app->add(new TrailingSlash(true)); // Add BasePathMiddleware $app->add(BasePathMiddleware::class); // HttpNotFoundException $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; } }); // Catch exceptions and errors $app->add(ErrorMiddleware::class); };
对于每个正在寻找具有另一个错误类的扩展的人,请通过以下方式扩展上面的代码:

try { return $handler->handle($request); } catch (HttpNotFoundException $httpException) { $response = (new Response())->withStatus(404); $response->getBody()->write('404 Not found'); return $response; } catch (HttpMethodNotAllowedException $httpException) { $response = (new Response())->withStatus(405); $response->getBody()->write('405 Method not allowed. Please use one of the following: '.implode(',',$httpException->getAllowedMethods())); return $response; }
其他错误类可以在 

https://github.com/slimphp/Slim/tree/4.x/Slim/Exception 找到

这是您获取和显示异常类以用于开发/调试目的的方法:

} catch (Exception $e) { $response = (new Response())->withStatus(500); $response->getBody()->write(get_class($e)); return $response; }
    
© www.soinside.com 2019 - 2024. All rights reserved.