Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Optional Query String in URI

https://docs.phalconphp.com/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.3k
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.