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

Params mismatch

Here is my route:

$router->addGet("/rest/:controller/{id}", array(
    "controller" => 1,
    "action" => "read"
));

and here is my action:

public function readAction(){
    $this->view->disable();
    echo $this->dispatcher->getParam('id');
}

When is go to rest/test/1 url it returns test instead of 1. Is it a bug?



43.9k

Hi

I think this should do the trick

public function readAction($id){....



43.9k

but you're right, you get here a curious behavior ....

edited Dec '14

@Reza

this behavoir is because you mixing placeholder and named parameter. named parameter are parsed in an extra function with own counter i guess.

in your case you get a compilied path like that:

array(
    "controller" => 1,
    "action" => "read",
    "id" => 1
)

however you should create a new issue on github.

use instead of your implementation:

// placeholder
$router->addGet("/rest/:controller/:id", array(
    "controller" => 1,
    "action" => "read",
    "id" => 2
));

or

// named parameter
$router->addGet("/rest/{controller}/{id}", array(
    "action" => "read"
));