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

Optional Query String in URI

https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Router.html

I want to manage optional query param in URL

    $router->add(
        "/test/filter/?{name:param}",
        array(
                "controller" => "test",
                "action"     => "filter"
        )
    );

Not sure hot to really set an optional params in URI, by I want to make both endpoints valid

ENDPOINTS test/filter test/filter?name=johndoe



3.5k
Accepted
answer
// In config of router service
$router->add(
    "/test/filter",
    array(
        "controller" => "test",
        "action"     => "filter"
    )
);

// In Controller
...
$this->request->getQuery('name') // return name from $_GET if exist
// or
$this->request->getQuery() // return all in $_GET
...

Service "request" is for work with GET parameters, not "router". Service "router" not manage GET parameters except to read rewrited url.

Ok, thanks I tought there was a more cleaner way of handling query params

// In config of router service
$router->add(
  "/test/filter",
  array(
      "controller" => "test",
      "action"     => "filter"
  )
);

// In Controller
...
$this->request->getQuery('name') // return name from $_GET if exist
// or
$this->request->getQuery() // return all in $_GET
...

Service "request" is for work with GET parameters, not "router". Service "router" not manage GET parameters except to read rewrited url.