Zend_Controller-Router-Route.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect3 id="zend.controller.router.routes.standard">
  4. <title>Zend_Controller_Router_Route</title>
  5. <para>
  6. <classname>Zend_Controller_Router_Route</classname> is the standard framework
  7. route. It combines ease of use with flexible route definition. Each
  8. route consists primarily of <acronym>URL</acronym> mapping (of static and dynamic parts
  9. (variables)) and may be initialized with defaults as well as with
  10. variable requirements.
  11. </para>
  12. <para>
  13. Let's imagine our fictional application will need some informational
  14. page about the content authors. We want to be able to point our web
  15. browsers to <filename>http://domain.com/author/martel</filename> to see the
  16. information about this "martel" guy. And the route for such
  17. functionality could look like:
  18. </para>
  19. <programlisting language="php"><![CDATA[
  20. $route = new Zend_Controller_Router_Route(
  21. 'author/:username',
  22. array(
  23. 'controller' => 'profile',
  24. 'action' => 'userinfo'
  25. )
  26. );
  27. $router->addRoute('user', $route);
  28. ]]></programlisting>
  29. <para>
  30. The first parameter in the <classname>Zend_Controller_Router_Route</classname>
  31. constructor is a route definition that will be matched to a <acronym>URL</acronym>. Route
  32. definitions consist of static and dynamic parts separated by the slash
  33. ('/') character. Static parts are just simple text:
  34. <command>author</command>. Dynamic parts, called variables, are marked by
  35. prepending a colon to the variable name: <command>:username</command>.
  36. </para>
  37. <note>
  38. <title>Character Usage</title>
  39. <para>
  40. The current implementation allows you to use any character (except a
  41. slash) as a variable identifier, but it is strongly recommended that
  42. one uses only characters that are valid for <acronym>PHP</acronym> variable
  43. identifiers. Future implementations may alter this behaviour, which
  44. could result in hidden bugs in your code.
  45. </para>
  46. </note>
  47. <para>
  48. This example route should be matched when you point your browser to
  49. <filename>http://domain.com/author/martel</filename>, in which case all its
  50. variables will be injected to the <classname>Zend_Controller_Request</classname>
  51. object and will be accessible in your <classname>ProfileController</classname>.
  52. Variables returned by this example may be represented as an array of
  53. the following key and value pairs:
  54. </para>
  55. <programlisting language="php"><![CDATA[
  56. $values = array(
  57. 'username' => 'martel',
  58. 'controller' => 'profile',
  59. 'action' => 'userinfo'
  60. );
  61. ]]></programlisting>
  62. <para>
  63. Later on, <classname>Zend_Controller_Dispatcher_Standard</classname> should invoke
  64. the <methodname>userinfoAction()</methodname> method of your
  65. <classname>ProfileController</classname> class (in the default module) based on
  66. these values. There you will be able to access all variables by means of
  67. the <methodname>Zend_Controller_Action::_getParam()</methodname> or
  68. <methodname>Zend_Controller_Request::getParam()</methodname> methods:
  69. </para>
  70. <programlisting language="php"><![CDATA[
  71. public function userinfoAction()
  72. {
  73. $request = $this->getRequest();
  74. $username = $request->getParam('username');
  75. $username = $this->_getParam('username');
  76. }
  77. ]]></programlisting>
  78. <para>
  79. Route definition can contain one more special character - a wildcard
  80. - represented by '*' symbol. It is used to gather parameters similarly
  81. to the default Module route (var => value pairs defined in the <acronym>URI</acronym>). The
  82. following route more-or-less mimics the Module route behavior:
  83. </para>
  84. <programlisting language="php"><![CDATA[
  85. $route = new Zend_Controller_Router_Route(
  86. ':module/:controller/:action/*',
  87. array('module' => 'default')
  88. );
  89. $router->addRoute('default', $route);
  90. ]]></programlisting>
  91. <sect4 id="zend.controller.router.routes.standard.variable-defaults">
  92. <title>Variable Defaults</title>
  93. <para>
  94. Every variable in the route can have a default and this is what the
  95. second parameter of the <classname>Zend_Controller_Router_Route</classname>
  96. constructor is used for. This parameter is an array with keys
  97. representing variable names and with values as desired defaults:
  98. </para>
  99. <programlisting language="php"><![CDATA[
  100. $route = new Zend_Controller_Router_Route(
  101. 'archive/:year',
  102. array('year' => 2006)
  103. );
  104. $router->addRoute('archive', $route);
  105. ]]></programlisting>
  106. <para>
  107. The above route will match <acronym>URL</acronym>s like
  108. <filename>http://domain.com/archive/2005</filename> and
  109. <filename>http://example.com/archive</filename>. In the latter case the
  110. variable year will have an initial default value of 2006.
  111. </para>
  112. <para>
  113. This example will result in injecting a year variable to the request
  114. object. Since no routing information is present (no controller and
  115. action parameters are defined), the application will be dispatched
  116. to the default controller and action method (which are both defined
  117. in <classname>Zend_Controller_Dispatcher_Abstract</classname>). To make it
  118. more usable, you have to provide a valid controller and a valid
  119. action as the route's defaults:
  120. </para>
  121. <programlisting language="php"><![CDATA[
  122. $route = new Zend_Controller_Router_Route(
  123. 'archive/:year',
  124. array(
  125. 'year' => 2006,
  126. 'controller' => 'archive',
  127. 'action' => 'show'
  128. )
  129. );
  130. $router->addRoute('archive', $route);
  131. ]]></programlisting>
  132. <para>
  133. This route will then result in dispatching to the method
  134. <methodname>showAction()</methodname> of the class
  135. <classname>ArchiveController</classname>.
  136. </para>
  137. </sect4>
  138. <sect4 id="zend.controller.router.routes.standard.variable-requirements">
  139. <title>Variable Requirements</title>
  140. <para>
  141. One can add a third parameter to the
  142. <classname>Zend_Controller_Router_Route</classname> constructor where variable
  143. requirements may be set. These are defined as parts of a regular
  144. expression:
  145. </para>
  146. <programlisting language="php"><![CDATA[
  147. $route = new Zend_Controller_Router_Route(
  148. 'archive/:year',
  149. array(
  150. 'year' => 2006,
  151. 'controller' => 'archive',
  152. 'action' => 'show'
  153. ),
  154. array('year' => '\d+')
  155. );
  156. $router->addRoute('archive', $route);
  157. ]]></programlisting>
  158. <para>
  159. With a route defined like above, the router will match it only when
  160. the year variable will contain numeric data, eg.
  161. <filename>http://domain.com/archive/2345</filename>. A <acronym>URL</acronym> like
  162. <filename>http://example.com/archive/test</filename> will not be matched and
  163. control will be passed to the next route in the chain instead.
  164. </para>
  165. </sect4>
  166. <sect4 id="zend.controller.router.routes.standard.translated-segments">
  167. <title>Translated segments</title>
  168. <para>
  169. The standard route supports translated segments. To use this
  170. feature, you have to define at least a translator (an instance
  171. of <classname>Zend_Translate</classname>) via one of the following ways:
  172. </para>
  173. <itemizedlist>
  174. <listitem>
  175. <para>
  176. Put it into the registry with the key <classname>Zend_Translate</classname>.
  177. </para>
  178. </listitem>
  179. <listitem>
  180. <para>
  181. Set it via the static method
  182. <methodname>Zend_Controller_Router_Route::setDefaultTranslator()</methodname>.
  183. </para>
  184. </listitem>
  185. <listitem>
  186. <para>
  187. Pass it as fourth parameter to the constructor.
  188. </para>
  189. </listitem>
  190. </itemizedlist>
  191. <para>
  192. By default, the locale specified in the <classname>Zend_Translate</classname>
  193. instance will be used. To override it, you set it
  194. (an instance of <classname>Zend_Locale</classname> or a locale string) in one
  195. of the following ways:
  196. </para>
  197. <itemizedlist>
  198. <listitem>
  199. <para>
  200. Put it into the registry with the key <classname>Zend_Locale</classname>.
  201. </para>
  202. </listitem>
  203. <listitem>
  204. <para>
  205. Set it via the static method
  206. <methodname>Zend_Controller_Router_Route::setDefaultLocale()</methodname>.
  207. </para>
  208. </listitem>
  209. <listitem>
  210. <para>
  211. Pass it as fifth parameter to the constructor.
  212. </para>
  213. </listitem>
  214. <listitem>
  215. <para>
  216. Pass it as <command>@locale</command> parameter to the assemble
  217. method.
  218. </para>
  219. </listitem>
  220. </itemizedlist>
  221. <para>
  222. Translated segments are separated into two parts. Fixed segments
  223. are prefixed by a single <emphasis>@</emphasis>-sign, and will be
  224. translated to the current locale when assembling and reverted
  225. to the message ID when matching again. Dynamic segments
  226. are prefixed by <command>:@</command>. When assembling, the given
  227. parameter will be translated and inserted into the parameter
  228. position. When matching, the translated parameter from the
  229. <acronym>URL</acronym> will be reverted to the message ID again.
  230. </para>
  231. <note>
  232. <title>Message IDs and separate language file</title>
  233. <para>
  234. Occasionally a message ID which you want to use in one
  235. of your routes is already used in a view script or somewhere
  236. else. To have full control over safe <acronym>URL</acronym>s, you should use
  237. a separate language file for the messages used in the route.
  238. </para>
  239. </note>
  240. <para>
  241. The following is the simplest way to prepare the standard route for
  242. translated segment usage:
  243. </para>
  244. <programlisting language="php"><![CDATA[
  245. // Prepare the translator
  246. $translator = new Zend_Translate(
  247. array(
  248. 'adapter' => 'array',
  249. 'content' => array(),
  250. 'locale' => 'en'
  251. )
  252. );
  253. $translator->addTranslation(
  254. array(
  255. 'content' =>
  256. array(
  257. 'archive' => 'archiv',
  258. 'year' => 'jahr',
  259. 'month' => 'monat',
  260. 'index' => 'uebersicht'
  261. ),
  262. 'locale' => 'de'
  263. )
  264. );
  265. // Set the current locale for the translator
  266. $translator->setLocale('en');
  267. // Set it as default translator for routes
  268. Zend_Controller_Router_Route::setDefaultTranslator($translator);
  269. ]]></programlisting>
  270. <para>
  271. This example demonstrates the usage of static segments:
  272. </para>
  273. <programlisting language="php"><![CDATA[
  274. // Create the route
  275. $route = new Zend_Controller_Router_Route(
  276. '@archive',
  277. array(
  278. 'controller' => 'archive',
  279. 'action' => 'index'
  280. )
  281. );
  282. $router->addRoute('archive', $route);
  283. // Assemble the URL in default locale: archive
  284. $route->assemble(array());
  285. // Assemble the URL in german: archiv
  286. $route->assemble(array());
  287. ]]></programlisting>
  288. <para>
  289. You can use the dynamic segments to create a module-route like
  290. translated version:
  291. </para>
  292. <programlisting language="php"><![CDATA[
  293. // Create the route
  294. $route = new Zend_Controller_Router_Route(
  295. ':@controller/:@action/*',
  296. array(
  297. 'controller' => 'index',
  298. 'action' => 'index'
  299. )
  300. );
  301. $router->addRoute('archive', $route);
  302. // Assemble the URL in default locale: archive/index/foo/bar
  303. $route->assemble(array('controller' => 'archive', 'foo' => 'bar'));
  304. // Assemble the URL in german: archiv/uebersicht/foo/bar
  305. $route->assemble(array('controller' => 'archive', 'foo' => 'bar'));
  306. ]]></programlisting>
  307. <para>
  308. You can also mix static and dynamic segments:
  309. </para>
  310. <programlisting language="php"><![CDATA[
  311. // Create the route
  312. $route = new Zend_Controller_Router_Route(
  313. '@archive/:@mode/:value',
  314. array(
  315. 'mode' => 'year'
  316. 'value' => 2005,
  317. 'controller' => 'archive',
  318. 'action' => 'show'
  319. ),
  320. array('mode' => '(month|year)'
  321. 'value' => '\d+')
  322. );
  323. $router->addRoute('archive', $route);
  324. // Assemble the URL in default locale: archive/month/5
  325. $route->assemble(array('mode' => 'month', 'value' => '5'));
  326. // Assemble the URL in german: archiv/monat/5
  327. $route->assemble(array('mode' => 'month', 'value' => '5', '@locale' => 'de'));
  328. ]]></programlisting>
  329. </sect4>
  330. </sect3>
  331. <!--
  332. vim:se ts=4 sw=4 et:
  333. -->