【问题标题】:Manage 'MethodNotAllowedHttpException ' in RESTful APIs using Laravel 5.3使用 Laravel 5.3 在 RESTful API 中管理“MethodNotAllowedHttpException”
【发布时间】:2017-02-02 04:59:45
【问题描述】:

我是 laravel 的新手,如果用户使用错误的 HTTP 方法点击 url,我想处理异常。我想以 json 格式发送用户响应,但代码不起作用,有时它会给出空白页。以下是我的代码:

处理程序.php

    public function render($request, Exception $exception)
    {

        if ($exception instanceof MethodNotAllowedHttpException) {
            return response()->json(['error' => 'Bad Request.'], 404);
        }

    }   

提前致谢

【问题讨论】:

    标签: php exception-handling laravel-5.3


    【解决方案1】:

    试试这个:

    public function render($request, Exception $exception)
        {
    
            if (basename(str_replace('\\', '/', get_class($exception))) == 'MethodNotAllowedHttpException') {
                return response()->json(['error' => 'Bad Request.'], 404);        
            }
            return parent::render($request, $exception);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-16
      • 1970-01-01
      • 2023-03-19
      • 2017-02-15
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多