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

How to get url parameters

Hi guys,

How can I get id value in this URL example

https://localhost/phalcon-crud/index/update/id/3

the id value is 3 and i'm using a router

$router->add("/delete/id/:params", array( 'controller' => 'index', 'action' => 'delete', 'id' => 1, 'params' => 1 ));

to generate that URL, and also how can i access that value in a update controller

Many thanks,



5.2k

I think it's something like this:

in router:

$router->add("/delete/id/([0-9])", array( 'controller' => 'index', 'action' => 'delete', 'id' => 1 ));

in Controller:

$id = $this->dispatcher->getParam("id");


8.4k
edited Sep '15

this is like

      $router->add(
        "/index/update/([0-9]{2})/:params",
        array(
            "controller" => "index",
            "action"     => "update",
            "id"       => 1, // ([0-9]{4})
            rest of it..
      )

you dont need to have \id\ you can either remove it or getting it as first parameter and the value as second parameter

    $this->dispatcher->getParam("id");

https://docs.phalcon.io/en/latest/reference/routing.html#parameters-with-names