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 '123' form URL 'controller/123/param'

Phalcon is eating up the 123 part of the following URL through the dispatcher

// controller/123/param

echo $this->dispatcher->getControllerName(); // controller
echo $this->dispatcher->getActionName(); // index
echo $this->dispatcher->getParams(); // [0=>'param']

Where did 123 go? Is there a simple way to make this available to my applications by default so that for all URLs of the form /foo/123/slug the IndexAction gets params [123, slug] instead of just ['slug']?

Thanks

edited Mar '14

Try something along the lines of:


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


9.1k

I need a generic route definition and tried the following:

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

    $router->add(
        "/:controller/:params",
        array(
            "controller" => 1,
            "action"     => "default",
            "params"     => 2,
        )
    ); 

The second route always takes precedence so the default MVC convention of controller/action/params doesn't work.

Any suggestions?

edited Mar '14

/controller/123/slug is exactly the same form as a standard controller/action/params call, so unless you have a guaranteed format, there's no way to differentiate. If the first parameter is always going to be an integer, you could do:

$router->add(
    "/:controller/:int/:params",
    array(
      "controller" => 1,
      "action"     => "default",
      "id"         => 2,
      "slug"       => 3
    )
  );

Then in your action, rather than referencing the parameters via params, you could access them by name:

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