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.

Route based on query string

Hi,

I would like to make a route for http://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'
    ]
);


42.4k
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.