Domanda

My site lets you create projects, and there are multiple project types (like project templates).

What I want to do is to have one single action that handles all project types. Currently for each project type I have a separate action and view. I want to route them all to one single action.

Here's what I've come up with so far, that doesn't work.

resources.router.routes.new_project.route = "/project/new-:id"
resources.router.routes.new_project.defaults.controller = "project"
resources.router.routes.new_project.defaults.module = "site"
resources.router.routes.new_project.defaults.action = "new"
resources.router.routes.new_project.reqs.id = "([0-9]+)"

So I want my URLs to look like http://mysite.com/project/new-1 or http://mysite.com/project/new-2, where 1 and 2 are the project type IDs. Both these URLs should be mapped to the action called new in the project controller.

Is something like this possible in ZF 1.11?

È stato utile?

Soluzione

Your desired URL format can easily be achieved with a regular expression route:

resources.router.routes.new_project.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.new_project.route = "project/new-(\d+)"
resources.router.routes.new_project.defaults.controller = "project"
resources.router.routes.new_project.defaults.module = "site"
resources.router.routes.new_project.defaults.action = "new"
resources.router.routes.new_project.map.1 = "project_type_id"
resources.router.routes.new_project.reverse = "project/new-%s"

You can then access the ID using $this->_getParam('project_type_id') in your controller.

Altri suggerimenti

It looks like you cannot specify URL variables as parts - they must occupy the whole of the segment (where a segment is delimited by /) - that is, the first character must be a : and the name is then the remainder of the value.

See function __construct in Zend/Controller/Router/Route.php (lines 158..196) - it only checks if substr($part, 0, 1) matches :, (rather than looking for it anywhere within the segment).


I think this means you would need to use /project/new/:id in Zend to use the same action - you can potentially use mod_rewrite in Apache to have URLs like /project/new-(\d+) presented to the user but rewritten to /project/new/$1 for Zend.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top