0

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?

Eduard Luca
  • 6,514
  • 16
  • 85
  • 137

2 Answers2

2

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.

Tim Fountain
  • 33,093
  • 5
  • 41
  • 69
  • *Almost* what I've come up with. This didn't work for me so in `new_project.route` I removed the beginning slash, and ended up with `^project/new-(\d+)`. – Eduard Luca Mar 07 '13 at 14:27
  • Yes, you're quite right, I've removed those from my answer. ZF actually strips them off before doing the route matching, but you may have ended up with a double slash in the assembled route. – Tim Fountain Mar 07 '13 at 14:31
1

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.

Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
  • +1 for what seems like a working solution. I found something that works for me from Zend's `application.ini` file. Will post later. Thank you! – Eduard Luca Mar 07 '13 at 13:31