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

Jithin Jose
  • 1,761
  • 2
  • 19
  • 35

1 Answers1

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(-[^/]*)?'
]);
patricus
  • 59,488
  • 15
  • 143
  • 145
  • This resolve route as expected, Also don't know why laravel have such weird rules. – Jithin Jose May 26 '15 at 08:48
  • 1
    @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](http://symfony.com/doc/current/book/routing.html): "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)." – patricus May 26 '15 at 08:54