We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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 ;)



15.1k
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.