Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Router - Weird problem with default values

I have a simple defined route like this.

$router->add(
"/api/:controller/:action/:params",
array(
    'module' => "api",
    "controller" => 1,
    "action"     => 2,
    "id"     => 3,
    )
);

Using this function - print_r($this->dispatcher->getParams());

  • /api/cont/act/3 - Works
  • /api/cont/act/ - Works
  • /api/cont/act - makes the id = 3 which is not acceptable.

I'm just starting witth thi and I just wanted to know whats up with it ;)



9.0k
Accepted
answer

You have to create another route for /api/:controller/:action.

The difference between /api/cont/act/ and api/cont/act is that the first has param 3, just set to null, while the second has no third param. If you add the strip trailing slash option to your router dependancy injection, then only the first url will work without defining a second route.

Aw yeah, works without a problem, thanks :)

You have to create another route for /api/:controller/:action.

The difference between /api/cont/act/ and api/cont/act is that the first has param 3, just set to null, while the second has no third param. If you add the strip trailing slash option to your router dependancy injection, then only the first url will work without defining a second route.