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

Router string parameter for search controller

I am creating a generic search for my app and am trying to use "router" to generate something like "/search/anything+here" I'm doing it this way:

$di->set('router', function(){
  $router = new \Phalcon\Mvc\Router();
  $router->add(
    "/search/{term:[a-zA-Z_]+}",
    array(
      "controller" => "search",
      "action"  => "search",
    )
  );

  return $router;
});

In SearchController I try this

    public function searchAction()
    {
      $term = $this->dispatcher->getParam("term");
      $this->log->log($term);
    }

Thus I can only get the parameter {term} if only with a simple word like "anything" if I use "anything+here" or "anything here" the router is ignored.

I do not know if I was clear on the issue.

Your route definition is not accepting white spaces, so it will not be matched.

Repair your regular expression for this route to include characters you want to put in search term.



93.7k
Accepted
answer

Little addition to David's comment.

More info and list of examples(regular expressions) here: https://docs.phalcon.io/en/latest/reference/routing.html

Or you can just omit the regular expression in the route and allow the user to search freely :

$router->add("/search/{term}"