Optimus
Optimus

Reputation: 1835

Custom route and form get parameters

I have the following route set up

routes.jobSearch.type = "Zend_Controller_Router_Route"
routes.jobSearch.route = "job/:sector/search/:sectorID/:subSectorID/:regionID/:jobTypeID/:ignore/:keywords/:page"
routes.jobSearch.defaults.module = "site"
routes.jobSearch.defaults.controller = "job"
routes.jobSearch.defaults.action = "search"
routes.jobSearch.defaults.keywords = ""
routes.jobSearch.regs.sector = "\w+"

And aslo I have this Zend_Form which provides search interface. But when I submit the form, the url becomes

http://localhost/job/all/search?search_sector=1&search_sub_sector=0&world_area=0&job_type=0&search_submit=Submit+Query&keywords=

and because of this my custom route is not matched. So is there a way to do this?

[EDIT] Here is the new route which didn't work either

routes.jobSearch.type = "Zend_Controller_Router_Route"
routes.jobSearch.route = "job/:sector/search/:search_sector/:search_sub_sector/:world_area/:job_type/:search_submit/:keywords"
routes.jobSearch.defaults.module = "site"
routes.jobSearch.defaults.controller = "job"
routes.jobSearch.defaults.action = "search"

[EDIT ref:@Jani] This is my route now

routes.jobSearch.route = "job/:sector/search/:subSectorID/:regionID/:jobTypeID/:search_submit/:keywords/:page"
routes.jobSearch.defaults.module = "site"
routes.jobSearch.defaults.controller = "job"
routes.jobSearch.defaults.action = "search"
routes.jobSearch.defaults.sector = "all"
routes.jobSearch.defaults.subSectorID = "0"
routes.jobSearch.defaults.regionID = "0"
routes.jobSearch.defaults.jobTypeID = "0"
routes.jobSearch.defaults.search_submit = "Submit+Query"
routes.jobSearch.defaults.keywords = ""
routes.jobSearch.defaults.page = "1"

and it matches correctly. At least I get the search result page. But all passes parameters are set to their defaults. So if I hit this url http://localhost/job/pharmaceutical/search?subSectorID=1&regionID=1&jobTypeID=1&search_submit=Submit%20Query&keywords=drugs and if I do var_dump($this->getRequest()->getParams()) I expect to see

array
  'sector' => string 'pharmaceutical' (length=14)
  'module' => string 'site' (length=4)
  'controller' => string 'job' (length=3)
  'action' => string 'search' (length=6)
  'subSectorID' => string '1' (length=0)
  'regionID' => string '1' (length=1)
  'jobTypeID' => string '1' (length=1)
  'search_submit' => string 'Submit+Query' (length=12)
  'keywords' => string 'drugs' (length=0)
  'page' => string '1' (length=1)

but what I get is

array
  'sector' => string 'pharmaceutical' (length=14)
  'module' => string 'site' (length=4)
  'controller' => string 'job' (length=3)
  'action' => string 'search' (length=6)
  'subSectorID' => string '0' (length=0)
  'regionID' => string '0' (length=1)
  'jobTypeID' => string '0' (length=1)
  'search_submit' => string 'Submit+Query' (length=12)
  'keywords' => string '' (length=0)
  'page' => string '1' (length=1)

Upvotes: 0

Views: 1335

Answers (1)

Jani Hartikainen
Jani Hartikainen

Reputation: 43253

The problem is that your route does not match at all.

The routes in ZF only match the path - not GET params / query string.

The path you're using is: /job/all/search

Your route only matches a path like: job/:sector/search/:sectorID/:subSectorID/:regionID/:jobTypeID/:ignore/:keywords/:page

In order for your route to match just /job/all/search, you need to provide default values for all the other parameters in the route, or create a route which match that path.

Match the path:

routes.jobSearch.route = "/job/:sector/search"
routes.jobSearch.defaults.module = "site"
routes.jobSearch.defaults.controller = "job"
routes.jobSearch.defaults.action = "search"

Or, give defaults:

routes.jobSearch.route = "job/:sector/search/:sectorID/:subSectorID/:regionID/:jobTypeID/:ignore/:keywords/:page"
routes.jobSearch.defaults.module = "site"
routes.jobSearch.defaults.controller = "job"
routes.jobSearch.defaults.action = "search"
routes.jobSearch.defaults.keywords = ""
routes.jobSearch.defaults.sectorId = ""
routes.jobSearch.defaults.subSectorID = ""
routes.jobSearch.defaults.regionID = ""
routes.jobSearch.defaults.jobTypeID = ""
routes.jobSearch.defaults.ignore = ""
routes.jobSearch.defaults.page = ""
routes.jobSearch.regs.sector = "\w+"

In order for the form to work as you described, you need to use getQuery() (or getPost() if you use POST form) from the request, not getParam().

getParam() defaults to router parameters first, then get, then post.

If you want to use the route params as defaults, you could do something along the lines of...

$foo = $this->_request->getQuery('foo', $this->_getParam('foo'));

This way if 'foo' exists in GET, you get that first, and if not, the result of _getParam becomes the value.

(_getParam is an alias for request->getParam())

Upvotes: 1

Related Questions