3

While using following route with 2 optional parameters,

Route::get('/abc-{abc_id?}/xyz-{xyz_id?}', function($abc_id=0, $xyz_id=0)
    {
        return "\n hello ... World";
    });

For requests

/abc-1/xyz-15           - Hello World
/abc-1/xyz              - Hello World

But for

/abc-/xyz-15           - 404
/abc/xyz-15            - 404

Why first optional parameter not working properly? Is there any alternate solutions?

Please note both parameters are in url not as a get attribute

2
  • 1
    Here's an older question about the same thing: stackoverflow.com/questions/23627864/… Commented May 26, 2015 at 6:23
  • Please note both parameters are in url not as a get attribute Commented May 26, 2015 at 8:42

1 Answer 1

4

Everything after the first optional parameter must be optional. If part of the route after an optional parameter is required, then the parameter becomes required.

In your case, since the /xyz- portion of your route is required, and it comes after the first optional parameter, that first optional parameter becomes required.

One option you have would be to include the id prefix as part of the parameter and use pattern matching to enforce the route format. You would then need to parse the actual id out from the parameter values.

Route::get('/{abc_id?}/{xyz_id?}', function($abc_id = 0, $xyz_id = 0) {
    $abc_id = substr($abc_id, 4) ?: 0;
    $xyz_id = substr($xyz_id, 4) ?: 0;

    return "\n hello ... World";
})->where([
    'abc_id' => 'abc(-[^/]*)?',
    'xyz_id' => 'xyz(-[^/]*)?'
]);
Sign up to request clarification or add additional context in comments.

2 Comments

This resolve route as expected, Also don't know why laravel have such weird rules.
@JithinJose This is actually a restriction from Symfony. Laravel's routing is built on top of Symfony's routing. In this case, the Laravel uses the RouteCompiler from Symfony. According to the Symfony documentation here: "Of course, you can have more than one optional placeholder (e.g. /blog/{slug}/{page}), but everything after an optional placeholder must be optional. For example, /{page}/blog is a valid path, but page will always be required (i.e. simply /blog will not match this route)."

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.