Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

automica's avatar

how to handle when route parameter is not provided

I am looking to handle the following case in my api.

Illuminate\Routing\Exceptions\UrlGenerationException: Missing required parameter for [Route: my.route] [URI: v1/posts/{post}/thing] [Missing parameter: post].

If the route doesnt have the correct parameters i'd like to return a 422 response rather than throwing an exception.

As this failure takes place prior to hitting the controller, where would be the best place to catch the above Exception?

0 likes
10 replies
OussamaMater's avatar

I don't think if I understood this correctly but bare with me

  1. If someone is using your API and skipped the post parameter (so an undefined route) that should be already handled by Laravel as a 404.
  2. Using Route Model Binding, you can make use of the missing() method and customize the response from 404 to 422 like so https://laravel.com/docs/9.x/routing#customizing-missing-model-behavior

Unless you are using the API as an internal service I don't really see where the issue will occur? that is generally because of route() method missing a parameter and it is YOU who is responsible to provide its parameters?

1 like
automica's avatar

@OussamaMater my use case is as follows:

Route: /billing/{clientCode}/manual maps to model: Client.clientCode and is an integer.

I'd like to bind it to the Client model but key needs to stay as clientName as thats whats defined in our api spec.

without the clientCode currently, it fails with:

Illuminate\Routing\Exceptions\UrlGenerationException: Missing required parameter for [Route: my.route] [URI: v1/billing/{clientCode}/manual] [Missing parameter: clientCode].
automica's avatar

@OussamaMater that works fine if there is an argument provided, but if its empty

eg /billing//manual

if I make the argument such /billing/{clientCode?}/manual then i can get the api to respond with a 404 now which is probably sufficient for now.

automica's avatar

@OussamaMater according to docs:

Typically, a 404 HTTP response will be generated if an implicitly bound model is not found.

I'm not implicitly binding the model at this point.

Its day job work so will have a think on best approach next week.

automica's avatar
automica
OP
Best Answer
Level 54

I did the following

Route::prefix('billing/{clientCode?}')->group(function () {
// routes
});

which allowed 404 response if clientCode wasnt present

emaia's avatar

Make the parameter optional with {post?} set a default value for it and also provide a fallback route .

Route::get('v1/posts/thing', function () {
    return response()->json(null, 422);
});
 
Route::get('v1/posts/{name?}/thing', function ($name = null) {
    return $post;
});

You can use regular expressions https://laravel.com/docs/9.x/routing#parameters-regular-expression-constraints or maybe check RouteServicePriovider to implement something that can help.

piljac1's avatar

Do you need to throw a 422 for this specific route, or for every routes that has a {post}?

Please or to participate in this conversation.