【问题标题】:Route fallback for POST methods in Laravel?Laravel中POST方法的路由回退?
【发布时间】:2019-08-23 10:44:02
【问题描述】:

有没有办法检查 post 方法的路由回退?此代码适用于我的路由文件中的任何获取 url,即如果我输入错误的 GET url,我会收到此响应(“找不到页面。”)。

有没有办法检查 POST url?

Route::fallback(function(){
    return response()->json([
        'status'    => false,
        'message'   => 'Page Not Found.',
    ], 404);
});

【问题讨论】:

    标签: laravel routes


    【解决方案1】:
    use Request;
    use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
    use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
    

    在 app/Exceptions/Handler.php 中替换渲染函数

    public function render($request, Exception $exception)
        {
            if (Request::isMethod('post') && $exception instanceof MethodNotAllowedHttpException) {
                return response()->json([
                    'message' => 'Page Not Found',
                    'status' => false
                    ], 500
                );
            }
            return parent::render($request, $exception);
        }
    

    或者如果你想要 NotFound 和 MethodNotAllowed 那么

    public function render($request, Exception $exception)
        {
            if ((Request::isMethod('post') && $exception instanceof MethodNotAllowedHttpException) || (Request::isMethod('post') && $exception instanceof NotFoundHttpException)) {
                return response()->json([
                    'message' => 'Page Not Found',
                    'status' => false
                    ], 500
                );
            }
            return parent::render($request, $exception);
        }
    

    【讨论】:

    • 嗨 Akash,我相应地更改了代码,我收到了这个错误 Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
    • 将实例更改为 MethodNotAllowedHttpException。请检查更新的答案
    • 它有效。谢谢。只需要添加使用 Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
    【解决方案2】:

    您可以使用给定的方法来检查 POST url。

    Route::fallback(function(){
        return \Response::view('errors.404');
    });
    

    【讨论】:

      【解决方案3】:

      在您的 routes/api.php 末尾定义自定义后备路由

      Route::any('{any}', function(){
          return response()->json([
              'status'    => false,
              'message'   => 'Page Not Found.',
          ], 404);
      })->where('any', '.*');
      

      【讨论】:

        【解决方案4】:

        将此脚本放在路由文件的末尾。

        Route::any('{url?}/{sub_url?}', function(){
            return response()->json([
                'status'    => false,
                'message'   => 'Page Not Found.',
            ], 404);
        })
        

        它会自动检测是否有人尝试点击以下任何其他路线。

        laravel_project_path/public/any_string
        laravel_project_path/public/any_string/any_string
        

        【讨论】:

        • 它不工作。并且给定的代码现在也不适用于 GET url。我需要在 {url} 中传递一些东西吗?
        • 这只能做到 2 深度。将正则表达式与 where() 一起使用。
        猜你喜欢
        • 2021-09-03
        • 2020-01-21
        • 2020-12-05
        • 2021-01-14
        • 2018-08-05
        • 2015-01-02
        • 2021-03-09
        • 2020-10-06
        • 1970-01-01
        相关资源
        最近更新 更多