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

Route does not work as expected

Route is recognized but parameters aren't.

In a route definition such as this one:

$router->add('/raw/brand/{brand}', array( 'module' => 'raw', 'controller' => 'brand', 'action' => 'index' ));

The correct module, controller and action fire, but getParams of dispatcher only gives:

array (size=1) '_url' => string '/raw/brand/bmw' (length=14)

Same thing if I use:

$router->add('/raw/brand/([a-zA-Z0-9]*)', array( 'module' => 'raw', 'controller' => 'brand', 'action' => 'index', 'brand' => 1 ));

Am I doing something wrong?



98.9k
Accepted
answer

Can you try running this gist on your machine?

https://gist.github.com/phalcon/6083917

edited Oct '14

So is there going to be a fix for this? Or should I do this?

$router = new Phalcon\Mvc\Router(false);

$router->add('/raw/brand/{brand}', array(
  'module' => 'raw',
    'controller' => 'brand',
    'action' => 'index'
));

$router->handle($this->getDi()->get('dispatcher')->getParam('_url'));
edited Oct '14

@swader

Using Andres's example I got this:

Array ( [brand] => bmw )

What does the getParams() of the router give you?

Sorry I am not 100% clear of what you are expecting to see.

Well, using my first approach, the route is triggered but I get no params. This is counter intuitive. If I call $router->handle(), then the route is triggered AND I get the params. I'm wondering why I have to call handle() explicitly, if the route is already being handled (and obviously triggers) without it (just the param is missing)? Andres' example works, but it's counter intuitive - calling handle() on a router that's already handling said route is confusing.