Php 拉拉维尔5号空气污染指数与野狗

Php 拉拉维尔5号空气污染指数与野狗,php,laravel,dingo-api,Php,Laravel,Dingo Api,我正在使用Laravel5和Dingo构建一个API。如何捕获未定义路由的任何请求?我希望我的API总是以特定格式的JSON响应进行响应 例如,如果我有一条路线: $api->get('somepage','mycontroller@mymethod'); 假设没有定义路由,我如何处理有人向同一uri创建帖子的情况 实际上,Laravel正在抛出MethodNotAllowedHttpException 我试过这个: Route::any('/{all}', function ($all

我正在使用Laravel5和Dingo构建一个API。如何捕获未定义路由的任何请求?我希望我的API总是以特定格式的JSON响应进行响应

例如,如果我有一条路线: $api->get('somepage','mycontroller@mymethod');

假设没有定义路由,我如何处理有人向同一uri创建帖子的情况

实际上,Laravel正在抛出MethodNotAllowedHttpException

我试过这个:

    Route::any('/{all}', function ($all) 
    {
        $errorResponse = [
            'Message' => 'Error',
            'Error' => ['data' => 'Sorry, that resource is not found or the method is not allowed.' ]
        ];
        return Response::json($errorResponse, 400);     //400 = Bad Request
    })->where(['all' => '.*']);
但我一直在抛出MethodNotAllowedHttpException

我有办法做到这一点吗?使用中间件?什么其他形式的通吃路线

编辑:

已尝试将此添加到app\Exceptions\Handler.php

public function render($request, Exception $e)
{
    dd($e);
    if ($e instanceof MethodNotAllowedHttpException) {
        $errorResponse = [
            'Message' => 'Error',
            'Error' => ['data' => 'Sorry, that resource is not found or the method is not allowed.' ]
        ];
        return Response::json($errorResponse, 400);
    }
    return parent::render($request, $e);        
}
没有效果。我做了自动装填之类的。我甚至添加了dd($e),但没有效果。我觉得这很奇怪

编辑-解决方案

我明白了。当James answer让我朝着正确的方向思考时,所发生的事情是Dingo凌驾于错误处理之上。要自定义此错误的响应,必须修改app\Providers\AppServiceProvider.php。使引导功能如下(默认为空)

接受詹姆斯的回答,因为这让我走上了正确的方向


希望这对某人有所帮助:)这占据了我晚上的大部分时间……呃

你可以在
app/Exceptions/Handler.php中通过捕获异常并检查它是否是
MethodNotAllowedHttpException
的实例来实现这一点

如果是,则可以执行逻辑以返回自定义错误响应

在同一地点,如果您想捕获
NotFoundHttpException
的实例,还可以自定义检查

// app/Exceptions/Handler.php

public function render($request, Exception $e)
    {
        if ($e instanceof MethodNotAllowedHttpException) {
            $errorResponse = [
                'Message' => 'Error',
                'Error' => ['data' => 'Sorry, that resource is not found or the method is not allowed.' ]
            ];
            return Response::json($errorResponse, 400);
        }

        if($e instanceof NotFoundHttpException)
        {
            $errorResponse = [
                'Message' => 'Error',
                'Error' => ['data' => 'Sorry, that resource is not found or the method is not allowed.' ]
            ];
            return Response::json($errorResponse, 400);
        }

        return parent::render($request, $e);
    }
// app/Exceptions/Handler.php

public function render($request, Exception $e)
    {
        if ($e instanceof MethodNotAllowedHttpException) {
            $errorResponse = [
                'Message' => 'Error',
                'Error' => ['data' => 'Sorry, that resource is not found or the method is not allowed.' ]
            ];
            return Response::json($errorResponse, 400);
        }

        if($e instanceof NotFoundHttpException)
        {
            $errorResponse = [
                'Message' => 'Error',
                'Error' => ['data' => 'Sorry, that resource is not found or the method is not allowed.' ]
            ];
            return Response::json($errorResponse, 400);
        }

        return parent::render($request, $e);
    }