自定义路由和表单获取参数

时间:2012-02-05 12:31:08

标签: zend-framework zend-form

我设置了以下路线

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+"

我也有这个提供搜索界面的Zend_Form。但是当我提交表单时,网址就会变成

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

因此我的自定义路线不匹配。有没有办法做到这一点?

[编辑] 这是一条无效的新路线

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"

[编辑参考:@Jani] 这是我现在的路线

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"

并且匹配正确。至少我得到了搜索结果页面。但是所有传递参数都设置为默认值。所以如果我点击这个网址 http://localhost/job/pharmaceutical/search?subSectorID=1&regionID=1&jobTypeID=1&search_submit=Submit%20Query&keywords=drugs如果我var_dump($this->getRequest()->getParams()),我希望看到

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)

但我得到的是

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)

1 个答案:

答案 0 :(得分:1)

问题是您的路线根本不匹配。

ZF中的路由只匹配路径 - 而不是GET参数/查询字符串。

您使用的路径是:/job/all/search

您的路线仅匹配以下路径:job/:sector/search/:sectorID/:subSectorID/:regionID/:jobTypeID/:ignore/:keywords/:page

为了使您的路线仅匹配/job/all/search,您需要为路线中的所有其他参数提供默认值,或创建与该路径匹配的路线。

匹配路径:

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

或者,给出默认值:

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+"

为了使表单按照您的描述运行,您需要在请求中使用getQuery()(或getPost(),如果您使用POST表单),而不是getParam()

getParam()首先默认为路由器参数,然后是get,然后是post。

如果你想使用route params作为默认值,你可以做一些......

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

这样一来,如果GET中存在“foo”,你首先得到它,如果没有,_getParam的结果就成了值。

_getParamrequest->getParam())的别名