Zend Framework: Zend_Controller_Router_RegexRoute Component Proposal
| Proposed Component Name | Zend_Controller_Router_RegexRoute |
|---|---|
| Developer Notes | http://framework.zend.com/wiki/display/ZFDEV/Zend_Controller_Router_RegexRoute |
| Proposers | Micha? Minicki |
| Revision | 1.0 - 23 January 2007: Initial version (wiki revision: 15) |
Table of Contents
1. Overview
This new route class allows for defining routes in pure regular expressions without any constraints. It allows to create recursive, look
ahead, conditional patterns. You name it. And should I mention it's fast? Or at least should be considerably faster than the current standard route.
2. References
No references at this point
3. Component Requirements, Constraints, and Acceptance Criteria
No requirements at this point. The class tightly integrates with current framework modules.
4. Dependencies on Other Framework Components
- Zend_Controller_RewriteRouter
5. Theory of Operation
This Route class is similar in nature to the rest of route classes. It allows mapping of path_info to request parameters.
Regex (first parameter) and defaults (second) are compatible with all other route objects
with one exception - you are be able to define integer indexed defaults in
order to enable it to work with regex subpatterns.
There is a third optional parameter named $map, which holds key-value
pairs for mapping subpatterns to named parameters.
6. Milestones / Tasks
- Milestone 1: [DONE] Working prototype
- Milestone 2: [DONE] Unit tests exist and work.
- Milestone 3: Initial documentation exists.
7. Class Index
- Zend_Controller_Router_RegexRoute
8. Use Cases
| UC-01 | Simple match |
|---|
This creates a route which will match URL of: /articles/2006
Note - regex is automatically surrounded by start, end and delimiter symbols ('#^' . $regex . '$#')
| UC-02 | Simple match with map |
|---|
This will match: /articles/2006/page/3. And additionaly it will map catched subpatterns so after matching these will be available through getParam as 'page' and 'aid' (in addition to controller and action).
| UC-03 | Numeric defaults |
|---|
This will match:
/archive
/archive/2004
/archive/2007
On the first type of match, parameter indexed with an integer '1' will hold the value of '2006'. Second and third example will hold '2004' and '2007' respectively. The numeric index is an id of regex subpattern position.
| UC-04 | Mapped mixed defaults |
|---|
Example URI: /archive
On match this will return 'year' => '2006' and 'month' => '4'. Even though month default is indexed numerically.
9. Class Skeletons
I find this approach quite intuitive and even more comfortable than others, since I've worked with REs for so many years ![]()
I would suggest also including support (and use cases) for debugging and testing these routes. With REs, the most time consuming task usually becomes debugging tiny flaws in RE patterns. If there is an extremely simple API to specify a list of test cases and expected results for each of several different inputs, then I would find this component even more useful. PHPUnit tests would suffice, but perhaps we can be more creative with some sort of value add for this specific situation (testing routes)? For example, a debug option that logs input URL, matched routes, and matched params might simplify diagnosing problems.
Debug option sounds great. But I would go even further and implement it at the RewriteRouter level to work with any type of Route. This would be a great opportunity to use emerging Zend_Log after it gets approved to the incubator:
And this actually gave me an idea for the Zend_Log. I will post it in their proposal in a minute:
This would be a fine addition to the family of routers.
I have another use case to complicate things:
The goals is for the optional anchor text following '#' in the URL to be available as a parameter to the controller.
The problem is that you're using '#' as your regular expression delimiter, so the parsing of the rexpr will be complicated.
Actually this won't work - that's why the delimiter is set to this character exactly.
First of all it would be too tempting to use anchor text to make a decision on which page to route to. And it wouldn't be good since the anchor text should be used only to find anchors on the exactly same page.
Secondly, RewriteRouter operates on PATH_INFO which does not contain anchor texts. See for yourself:
http://test.localhost/articles/2006#header1
What's the status of this proposal now? I really like it and would like to see it in the incubator soon...
I'm ready to commit for at least two weeks now, Tomas. It's all in the hands of Zend guys now.
ZF Home Page
Code Browser
Wiki Dashboard
Great idea!
It would be useful if the documentation were to include examples of useful regexp's.
Example: won't UC-03 also match /articles2007, which would probably not be desirable.