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.

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.



62.7k
Accepted
answer

Little addition to David's comment.

More info and list of examples(regular expressions) here: https://docs.phalconphp.com/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}"