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

Route based on query string

Hi,

I would like to make a route for https://mysite.com/?s=cap&searchfor=pages to another controller than index. I've tried this, but is not working

 $router->add(
    '/\?s=([a-zA-Z0-9\_\-]+)&searchfor=([a-zA-Z0-9\_\-]+)',
    [
        'controller'    => 'page',
        'action'        => 'search'
    ]
);


77.7k
Accepted
answer
edited Sep '17

Afaik routes only operate on the path component of the URI

You can handle it with dispatching:

class IndexController extends \Phalcon\Mvc\Controller {
    public function indexAction()
    {
        if($this->request->hasQuery('s') && $this->request->hasQuery('searchfor')) {
          $this->dispatcher->forward([
              'controller' => 'page',
              'action' => 'search'
          ]);
          return;
        }
    }
}
edited Sep '17

Query parameters are part of $_GET in php. Use request->hasQuery

Thanks Lajos. Worth to remember your solution. I've made a 301 redirect from htaccess in the end, because my main concern was the old searches indexed in google.