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

How to add multiple parameters to route matching

I'm essentially trying to duplicate what happens when you use [] in a url, something like: page.php?type[]=1&type[]=2, etc.

Right now, I have this:

$routes->add("/read/(([0-9],?)+)", array(
  'controller'  => 'read',
  'action'      => 'index',
  'type'        => 1
));

Then I can explode the types on the ',', but that just seems clunky to me. Is there a better way to do this? I want users to be able to bookmark searches.



2.2k
Accepted
answer

Why don't you use just same as ?type[]=1&type[]=2 ? Then it is not related with routing.

You can get them like below in controllers.

$this->request->getQuery('type')


14.9k

Yeah -- I suppose that would work -- just seems so inelegant when compared to the nice urls the rest of the app has :)

Thanks! -Seth

edited Jan '15

Well page.php?type[]=1&type[]=2 is sending query strings after the ? and you are using variables that are arrays at that point

brackets in a URL otherwise is non standard, ie page/type[]/type[] will break

if you are sending multiple types as "pretty url" you would send /page/1 AND/OR /page/1/2/3/4 and have to create a matching number

$routes->add("/page/([0-9]+)", array(
  'controller'    => 'page',
  'action'        => 'index',
  'type1'      => 1
));

$routes->add("/page/([0-9]+)/([0-9]+))/([0-9]+))/([0-9]+))", array(
  'controller'    => 'page',
  'action'        => 'index',
  'type1'      => 1,
  'type2'      => 2,
  'type3'      => 3,
  'type4'      => 4
));