简体   繁体   中英

Laravel 5 API with Dingo

I'm building an API using Laravel 5 and Dingo. How do I catch any requests which do not have a route defined? I want my API to always respond with a specifically formatted JSON response.

So for example if I have a route: $api->get( 'somepage','mycontroller@mymethod');

how do I handle the case where someone creates a post to the same uri assuming that route is not defined?

Essentially what is happening is that Laravel is throwing a MethodNotAllowedHttpException.

I have tried this:

    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' => '.*']);

but I keep getting the MethodNotAllowedHttpException thrown.

Is there a way I can do this? Using Middleware? Some other form of a catch all route?

EDIT:

Tried adding this to 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);        
}

It had no effect. I did the dump-autoload and all that. I even added the dd($e) and it had no effect. This seems odd to me.

EDIT - SOLUTION

Figured it out. While James answer got me thinking in the right direction, what was happening is that Dingo was overriding the error handling. In order to customize the response for this error, you have to modify app\\Providers\\AppServiceProvider.php. Make the boot function like this (its empty by default)

public function boot()
{
    app('Dingo\Api\Exception\Handler')->register(function (MethodNotAllowedHttpException $exception) {
         $errorResponse = [
            'Message' => 'Error',
            'Error' => ['data' => 'Sorry, that resource is not found or the method is not allowed.' ]
        ];
        return Response::make($errorResponse, 400);
    });
}

Accepting James' answer because it got my going in the right direction.

Hope this helps someone :) This took up the better part of my night....ugh

You can do this inside app/Exceptions/Handler.php by catching the exception and checking if it is an instance of MethodNotAllowedHttpException .

If it is then you can execute the logic to return your custom error response.

In the same spot, you can also customise your check if you wanted to catch instances of 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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM