-1

I am trying to create a global exception handler for handling the exception for the api routes to catch the missing uri parameter.

routes/api.php

Route::resource('zones', ZoneController::class)->only(['index', 'store', 'edit']);
Route::patch('zones/{zone}', [ZoneController::class, 'update'])->name('zones.update');

As the code says, the zone parameter is required; therefore, if - for some reason - its not available or does not have any value then I am getting the following exception:

{
  "message": "The PATCH method is not supported for route api/zones. 
              Supported methods: GET, HEAD, POST.",
  "exception": "Symfony\\Component\\HttpKernel\\Exception\\MethodNotAllowedHttpException",
  ...
}

I tried following the documentation, but unfortunately it could not solve my problem.

UPDATE:

error

success

I am using the PATCH method for updating/modifying an existing record which requires an identifying parameter: zone. If the parameter is available then the request works as expected(the record is updated with the given values) without any error. However, if the parameter is not available(for some reason) the error pops up.

3
  • Try with reordering, e.x. reorder resource line and set it after, as last “zones/*” route(s). Commented May 28 at 15:10
  • Hi there. Can I see the Http Request you made when calling zone/{zone}? Thnaks. Commented May 29 at 1:10
  • @Tatachiblob, I have updated the question added the screenshot. Please have a look. Commented May 29 at 5:34

2 Answers 2

1

That's just not the way that Laravel routing works. Look at the handleMatchedRoute method in laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php:

    /**
     * Handle the matched route.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Routing\Route|null  $route
     * @return \Illuminate\Routing\Route
     *
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
     */
    protected function handleMatchedRoute(Request $request, $route)
    {
        if (! is_null($route)) {
            return $route->bind($request);
        }

        // If no route was found we will now check if a matching route is specified by
        // another HTTP verb. If it is we will need to throw a MethodNotAllowed and
        // inform the user agent of which HTTP verb it should use for this route.
        $others = $this->checkForAlternateVerbs($request);

        if (count($others) > 0) {
            return $this->getRouteForMethods($request, $others);
        }

        throw new NotFoundHttpException(sprintf(
            'The route %s could not be found.',
            $request->path()
        ));
    }

Laravel first looks to find a matching route, if it can't find a route which matches both the URI and verb, then it checks whether there are alternative verbs for that route and throws a MethodNotAllowedHttpException as you see in your example.

It's only if there are no routes with other verbs for the matching URI that the NotFoundHttpException is thrown.

If you want to modify this behaviour, you'll need to create your own router and extend this method.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to make a patch request, you would need to add _method key in your request body and send it as a POST request instead of PATCH.

POST: http://my-localhost.test/zones/zoneid

{
    "title": "testing",
    "_method": "PATCH"
}

This should avoid the error you are encountering.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.