Zend_Controller-Router-Route-Regex.xml 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect3 id="zend.controller.router.routes.regex">
  4. <title>Zend_Controller_Router_Route_Regex</title>
  5. <para>
  6. In addition to the default and static route types, a Regular
  7. Expression route type is available. This route offers more power and
  8. flexibility over the others, but at a slight cost of complexity. At the
  9. same time, it should be faster than the standard Route.
  10. </para>
  11. <para>
  12. Like the standard route, this route has to be initialized with a route
  13. definition and some defaults. Let's create an archive route as an
  14. example, similar to the previously defined one, only using the Regex
  15. route this time:
  16. </para>
  17. <programlisting language="php"><![CDATA[
  18. $route = new Zend_Controller_Router_Route_Regex(
  19. 'archive/(\d+)',
  20. array(
  21. 'controller' => 'archive',
  22. 'action' => 'show'
  23. )
  24. );
  25. $router->addRoute('archive', $route);
  26. ]]></programlisting>
  27. <para>
  28. Every defined regex subpattern will be injected to the request
  29. object. With our above example, after successful matching
  30. <filename>http://domain.com/archive/2006</filename>, the resulting value
  31. array may look like:
  32. </para>
  33. <programlisting language="php"><![CDATA[
  34. $values = array(
  35. 1 => '2006',
  36. 'controller' => 'archive',
  37. 'action' => 'show'
  38. );
  39. ]]></programlisting>
  40. <note>
  41. <para>
  42. Leading and trailing slashes are trimmed from the <acronym>URL</acronym> in the Router
  43. prior to a match. As a result, matching the <acronym>URL</acronym>
  44. <filename>http://domain.com/foo/bar/</filename>, would involve a regex of
  45. <filename>foo/bar</filename>, and not <filename>/foo/bar</filename>.
  46. </para>
  47. </note>
  48. <note>
  49. <para>
  50. Line start and line end anchors ('^' and '$', respectively) are
  51. automatically pre- and appended to all expressions. Thus, you
  52. should not use these in your regular expressions, and you should
  53. match the entire string.
  54. </para>
  55. </note>
  56. <note>
  57. <para>
  58. This route class uses the '<emphasis>#</emphasis>' character for a delimiter.
  59. This means that you will need to escape hash characters ('#') but
  60. not forward slashes ('/') in your route definitions. Since the '#'
  61. character (named anchor) is rarely passed to the webserver, you will
  62. rarely need to use that character in your regex.
  63. </para>
  64. </note>
  65. <para>
  66. You can get the contents of the defined subpatterns the usual way:
  67. </para>
  68. <programlisting language="php"><![CDATA[
  69. public function showAction()
  70. {
  71. $request = $this->getRequest();
  72. $year = $request->getParam(1); // $year = '2006';
  73. }
  74. ]]></programlisting>
  75. <note>
  76. <para>Notice the key is an integer (1) instead of a string ('1').</para>
  77. </note>
  78. <para>
  79. This route will not yet work exactly the same as its standard route
  80. counterpart since the default for 'year' is not yet set. And what may
  81. not yet be evident is that we will have a problem with a trailing slash
  82. even if we declare a default for the year and make the subpattern
  83. optional. The solution is to make the whole year part optional along
  84. with the slash but catch only the numeric part:
  85. </para>
  86. <programlisting language="php"><![CDATA[
  87. $route = new Zend_Controller_Router_Route_Regex(
  88. 'archive(?:/(\d+))?',
  89. array(
  90. 1 => '2006',
  91. 'controller' => 'archive',
  92. 'action' => 'show'
  93. )
  94. );
  95. $router->addRoute('archive', $route);
  96. ]]></programlisting>
  97. <para>
  98. Now let's get to the problem you have probably noticed on your own by
  99. now. Using integer based keys for parameters is not an easily manageable
  100. solution and may be potentially problematic in the long run. And that's
  101. where the third parameter comes in. This parameter is an associative
  102. array that represents a map of regex subpatterns to parameter named
  103. keys. Let's work on our easier example:
  104. </para>
  105. <programlisting language="php"><![CDATA[
  106. $route = new Zend_Controller_Router_Route_Regex(
  107. 'archive/(\d+)',
  108. array(
  109. 'controller' => 'archive',
  110. 'action' => 'show'
  111. ),
  112. array(
  113. 1 => 'year'
  114. )
  115. );
  116. $router->addRoute('archive', $route);
  117. ]]></programlisting>
  118. <para>
  119. This will result in following values injected into Request:
  120. </para>
  121. <programlisting language="php"><![CDATA[
  122. $values = array(
  123. 'year' => '2006',
  124. 'controller' => 'archive',
  125. 'action' => 'show'
  126. );
  127. ]]></programlisting>
  128. <para>
  129. The map may be defined in either direction to make it work in any
  130. environment. Keys may contain variable names or subpattern indexes:
  131. </para>
  132. <programlisting language="php"><![CDATA[
  133. $route = new Zend_Controller_Router_Route_Regex(
  134. 'archive/(\d+)',
  135. array( ... ),
  136. array(1 => 'year')
  137. );
  138. // OR
  139. $route = new Zend_Controller_Router_Route_Regex(
  140. 'archive/(\d+)',
  141. array( ... ),
  142. array('year' => 1)
  143. );
  144. ]]></programlisting>
  145. <note>
  146. <para>
  147. Subpattern keys have to be represented by integers.
  148. </para>
  149. </note>
  150. <para>
  151. Notice that the numeric index in Request values is now gone and a named
  152. variable is shown in its place. Of course you can mix numeric and named
  153. variables if you wish:
  154. </para>
  155. <programlisting language="php"><![CDATA[
  156. $route = new Zend_Controller_Router_Route_Regex(
  157. 'archive/(\d+)/page/(\d+)',
  158. array( ... ),
  159. array('year' => 1)
  160. );
  161. ]]></programlisting>
  162. <para>
  163. Which will result in mixed values available in the Request. As an example, the
  164. <acronym>URL</acronym> <filename>http://domain.com/archive/2006/page/10</filename>
  165. will result in following values:
  166. </para>
  167. <programlisting language="php"><![CDATA[
  168. $values = array(
  169. 'year' => '2006',
  170. 2 => 10,
  171. 'controller' => 'archive',
  172. 'action' => 'show'
  173. );
  174. ]]></programlisting>
  175. <para>
  176. Since regex patterns are not easily reversed, you will need to prepare
  177. a reverse <acronym>URL</acronym> if you wish to use a <acronym>URL</acronym> helper or even
  178. an assemble method of this class. This reversed path is represented by a string parsable by
  179. <methodname>sprintf()</methodname> and is defined as a fourth construct parameter:
  180. </para>
  181. <programlisting language="php"><![CDATA[
  182. $route = new Zend_Controller_Router_Route_Regex(
  183. 'archive/(\d+)',
  184. array( ... ),
  185. array('year' => 1),
  186. 'archive/%s'
  187. );
  188. ]]></programlisting>
  189. <para>
  190. All of this is something which was already possible by the means of a
  191. standard route object, so where's the benefit in using the Regex route,
  192. you ask? Primarily, it allows you to describe any type of <acronym>URL</acronym> without
  193. any restrictions. Imagine you have a blog and wish to create <acronym>URL</acronym>s like:
  194. <filename>http://domain.com/blog/archive/01-Using_the_Regex_Router.html</filename>,
  195. and have it decompose the last path element,
  196. <filename>01-Using_the_Regex_Router.html</filename>, into an article ID and
  197. article title or description; this is not possible with the standard route.
  198. With the Regex route, you can do something like the following solution:
  199. </para>
  200. <programlisting language="php"><![CDATA[
  201. $route = new Zend_Controller_Router_Route_Regex(
  202. 'blog/archive/(\d+)-(.+)\.html',
  203. array(
  204. 'controller' => 'blog',
  205. 'action' => 'view'
  206. ),
  207. array(
  208. 1 => 'id',
  209. 2 => 'description'
  210. ),
  211. 'blog/archive/%d-%s.html'
  212. );
  213. $router->addRoute('blogArchive', $route);
  214. ]]></programlisting>
  215. <para>
  216. As you can see, this adds a tremendous amount of flexibility over the
  217. standard route.
  218. </para>
  219. </sect3>
  220. <!--
  221. vim:se ts=4 sw=4 et:
  222. -->