【问题标题】:Handle Method not allowed in slim 3slim 3 中不允许处理方法
【发布时间】:2018-05-10 04:07:43
【问题描述】:

需要您帮助解决这个问题。我在 Slim 3 框架中开发了我的网站。我想处理当我使用浏览器后退和前进按钮时收到的“Method not allowed。必须是以下之一:POST”消息。

I want to redirect to a different page when if the route is post and when user clicks on browser back or forward page.

当调用 post 路由时,有一种方法可以让我检测到它是 post 方法调用并将他重定向到不同的 get 路由。

【问题讨论】:

    标签: php slim-3


    【解决方案1】:

    您可以为特定错误添加自己的处理程序:

    $container['notAllowedHandler'] = function (ServerRequestInterface $request, ResponseInterface $response, array $methods) {
        // you can return a redirect response
    };
    

    查看更多here

    【讨论】:

      【解决方案2】:

      另一种语法解决方案

      $notAllowedHandler = function ($c) {
          return function ($request, $response) use ($c) {
              return $response
                  ->withJson(
                      [
                          "status" => false,
                          "message" => "Your custom message",
                          "data" => [],
                      ]
                  )
                  ->withStatus(400);
          };
      };
      
      $app = new App(
          [
              'notAllowedHandler' => $notAllowedHandler,
          ]
      );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-18
        • 1970-01-01
        • 2018-09-02
        • 2016-04-21
        • 2018-11-05
        • 1970-01-01
        • 2019-01-22
        • 2013-12-07
        相关资源
        最近更新 更多