Zend_Controller-Router.xml 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.controller.router" xmlns:xi="http://www.w3.org/2001/XInclude">
  4. <title>The Standard Router</title>
  5. <sect2 id="zend.controller.router.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. <classname>Zend_Controller_Router_Rewrite</classname> is the standard
  9. framework router. Routing is the process of taking a URI endpoint
  10. (that part of the URI which comes after the base URL) and
  11. decomposing it into parameters to determine which module,
  12. controller, and action of that controller should receive the
  13. request. This values of the module, controller, action and other
  14. parameters are packaged into a
  15. <classname>Zend_Controller_Request_Http</classname> object which is then
  16. processed by <classname>Zend_Controller_Dispatcher_Standard</classname>.
  17. Routing occurs only once: when the request is initially received and
  18. before the first controller is dispatched.
  19. </para>
  20. <para>
  21. <classname>Zend_Controller_Router_Rewrite</classname> is designed to allow for
  22. mod_rewrite-like functionality using pure PHP structures. It is very
  23. loosely based on Ruby on Rails routing and does not require any
  24. prior knowledge of webserver URL rewriting. It is designed to work
  25. with a single Apache mod_rewrite rule (one of):
  26. </para>
  27. <programlisting role="php"><![CDATA[
  28. RewriteEngine on
  29. RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php
  30. ]]></programlisting>
  31. <para>
  32. or (preferred):
  33. </para>
  34. <programlisting role="php"><![CDATA[
  35. RewriteEngine On
  36. RewriteCond %{REQUEST_FILENAME} -s [OR]
  37. RewriteCond %{REQUEST_FILENAME} -l [OR]
  38. RewriteCond %{REQUEST_FILENAME} -d
  39. RewriteRule ^.*$ - [NC,L]
  40. RewriteRule ^.*$ index.php [NC,L]
  41. ]]></programlisting>
  42. <para>
  43. The rewrite router can also be used with the IIS webserver (versions &lt;= 7.0) if <ulink
  44. url="http://www.isapirewrite.com">Isapi_Rewrite</ulink> has been
  45. installed as an Isapi extension with the following rewrite rule:
  46. </para>
  47. <programlisting role="php"><![CDATA[
  48. RewriteRule ^[\w/\%]*(?:\.(?!(?:js|ico|gif|jpg|png|css|html)$)[\w\%]*$)? /index.php [I]
  49. ]]></programlisting>
  50. <note>
  51. <title>IIS Isapi_Rewrite</title>
  52. <para>
  53. When using IIS, <code>$_SERVER['REQUEST_URI']</code> will either
  54. not exist, or be set as an empty string. In this case,
  55. <classname>Zend_Controller_Request_Http</classname> will attempt to use
  56. the <code>$_SERVER['HTTP_X_REWRITE_URL']</code> value set by the
  57. Isapi_Rewrite extension.
  58. </para>
  59. </note>
  60. <para>
  61. IIS 7.0 introduces a native URL rewriting module, and it can be
  62. configured as follows:
  63. </para>
  64. <programlisting role="xml"><![CDATA[
  65. <?xml version="1.0" encoding="UTF-8"?>
  66. <configuration>
  67. <system.webServer>
  68. <rewrite>
  69. <rules>
  70. <rule name="Imported Rule 1" stopProcessing="true">
  71. <match url="^.*$" />
  72. <conditions logicalGrouping="MatchAny">
  73. <add input="{REQUEST_FILENAME}"
  74. matchType="IsFile" pattern=""
  75. ignoreCase="false" />
  76. <add input="{REQUEST_FILENAME}"
  77. matchType="IsDirectory"
  78. pattern="" ignoreCase="false" />
  79. </conditions>
  80. <action type="None" />
  81. </rule>
  82. <rule name="Imported Rule 2" stopProcessing="true">
  83. <match url="^.*$" />
  84. <action type="Rewrite" url="index.php" />
  85. </rule>
  86. </rules>
  87. </rewrite>
  88. </system.webServer>
  89. </configuration>]></programlisting>
  90. <para>
  91. If using Lighttpd, the following rewrite rule is valid:
  92. </para>
  93. <programlisting role="lighttpd"><![CDATA[
  94. url.rewrite-once = (
  95. ".*\?(.*)$" => "/index.php?$1",
  96. ".*\.(js|ico|gif|jpg|png|css|html)$" => "$0",
  97. "" => "/index.php"
  98. )
  99. ]]></programlisting>
  100. </sect2>
  101. <sect2 id="zend.controller.router.usage">
  102. <title>Using a Router</title>
  103. <para>
  104. To properly use the rewrite router you have to instantiate it, add
  105. some user defined routes and inject it into the controller. The
  106. following code illustrates the procedure:
  107. </para>
  108. <programlisting role="php"><![CDATA[
  109. // Create a router
  110. $router = $ctrl->getRouter(); // returns a rewrite router by default
  111. $router->addRoute(
  112. 'user',
  113. new Zend_Controller_Router_Route('user/:username',
  114. array('controller' => 'user',
  115. 'action' => 'info'))
  116. );
  117. ]]></programlisting>
  118. </sect2>
  119. <sect2 id="zend.controller.router.basic">
  120. <title>Basic Rewrite Router Operation</title>
  121. <para>
  122. The heart of the RewriteRouter is the definition of user defined
  123. routes. Routes are added by calling the addRoute method of
  124. RewriteRouter and passing in a new instance of a class implementing
  125. <classname>Zend_Controller_Router_Route_Interface</classname>. Eg.:
  126. </para>
  127. <programlisting role="php"><![CDATA[
  128. $router->addRoute('user',
  129. new Zend_Controller_Router_Route('user/:username'));
  130. ]]></programlisting>
  131. <para>
  132. Rewrite Router comes with six basic types of routes (one of which
  133. is special):
  134. </para>
  135. <itemizedlist mark="opencircle">
  136. <listitem><para><xref linkend="zend.controller.router.routes.standard" /></para></listitem>
  137. <listitem><para><xref linkend="zend.controller.router.routes.static" /></para></listitem>
  138. <listitem><para><xref linkend="zend.controller.router.routes.regex" /></para></listitem>
  139. <listitem><para><xref linkend="zend.controller.router.routes.hostname" /></para></listitem>
  140. <listitem><para><xref linkend="zend.controller.router.routes.chain" /></para></listitem>
  141. <listitem><para><xref linkend="zend.controller.router.default-routes" /> *</para></listitem>
  142. </itemizedlist>
  143. <para>
  144. Routes may be used numerous times to create a chain or user defined
  145. application routing schema. You may use any number of routes in any
  146. configuration, with the exception of the Module route, which should
  147. rather be used once and probably as the most generic route (i.e., as a
  148. default). Each route will be described in greater detail later on.
  149. </para>
  150. <para>
  151. The first parameter to addRoute is the name of the route. It is used
  152. as a handle for getting the routes out of the router (e.g., for URL
  153. generation purposes). The second parameter being the route itself.
  154. </para>
  155. <note>
  156. <para>
  157. The most common use of the route name is through the means of
  158. <classname>Zend_View</classname> url helper:
  159. </para>
  160. <programlisting role="php"><![CDATA[
  161. <a href=
  162. "<?php echo $this->url(array('username' => 'martel'), 'user') ?>">Martel</a>
  163. ]]></programlisting>
  164. <para>
  165. Which would result in the href: <code>user/martel</code>.
  166. </para>
  167. </note>
  168. <para>
  169. Routing is a simple process of iterating through all provided routes
  170. and matching its definitions to current request URI. When a positive
  171. match is found, variable values are returned from the Route instance
  172. and are injected into the <classname>Zend_Controller_Request</classname>
  173. object for later use in the dispatcher as well as in user created
  174. controllers. On a negative match result, the next route in the chain
  175. is checked.
  176. </para>
  177. <para>
  178. If you need to determine which route was matched, you can use the
  179. <code>getCurrentRouteName()</code> method, which will return the
  180. identifier used when registering the route with the router. If you
  181. want the actual route object, you can use
  182. <code>getCurrentRoute()</code>.
  183. </para>
  184. <note>
  185. <title>Reverse Matching</title>
  186. <para>
  187. Routes are matched in reverse order so make sure your most
  188. generic routes are defined first.
  189. </para>
  190. </note>
  191. <note>
  192. <title>Returned Values</title>
  193. <para>
  194. Values returned from routing come from URL parameters or user
  195. defined route defaults. These variables are later accessible
  196. through the <classname>Zend_Controller_Request::getParam()</classname> or
  197. <classname>Zend_Controller_Action::_getParam()</classname> methods.
  198. </para>
  199. </note>
  200. <para>
  201. There are three special variables which can be used in your routes
  202. - 'module', 'controller' and 'action'. These special variables are
  203. used by <classname>Zend_Controller_Dispatcher</classname> to find a controller and action
  204. to dispatch to.
  205. </para>
  206. <note>
  207. <title>Special Variables</title>
  208. <para>
  209. The names of these special variables may be different if you
  210. choose to alter the defaults in
  211. <classname>Zend_Controller_Request_Http</classname> by means of the
  212. <code>setControllerKey</code> and <code>setActionKey</code>
  213. methods.
  214. </para>
  215. </note>
  216. </sect2>
  217. <sect2 id="zend.controller.router.default-routes">
  218. <title>Default Routes</title>
  219. <para>
  220. <classname>Zend_Controller_Router_Rewrite</classname> comes preconfigured with a default
  221. route, which will match URIs in the shape of
  222. <code>controller/action</code>. Additionally, a module name may be
  223. specified as the first path element, allowing URIs of the form
  224. <code>module/controller/action</code>. Finally, it will also match
  225. any additional parameters appended to the URI by default -
  226. <code>controller/action/var1/value1/var2/value2</code>.
  227. </para>
  228. <para>
  229. Some examples of how such routes are matched:
  230. </para>
  231. <programlisting role="php"><![CDATA[
  232. // Assuming the following:
  233. $ctrl->setControllerDirectory(
  234. array(
  235. 'default' => '/path/to/default/controllers',
  236. 'news' => '/path/to/news/controllers',
  237. 'blog' => '/path/to/blog/controllers'
  238. )
  239. );
  240. Module only:
  241. http://example/news
  242. module == news
  243. Invalid module maps to controller name:
  244. http://example/foo
  245. controller == foo
  246. Module + controller:
  247. http://example/blog/archive
  248. module == blog
  249. controller == archive
  250. Module + controller + action:
  251. http://example/blog/archive/list
  252. module == blog
  253. controller == archive
  254. action == list
  255. Module + controller + action + params:
  256. http://example/blog/archive/list/sort/alpha/date/desc
  257. module == blog
  258. controller == archive
  259. action == list
  260. sort == alpha
  261. date == desc
  262. ]]></programlisting>
  263. <para>
  264. The default route is simply a
  265. <classname>Zend_Controller_Router_Route_Module</classname> object stored under
  266. the name (index) of 'default' in RewriteRouter. It's created
  267. more-or-less like below:
  268. </para>
  269. <programlisting role="php"><![CDATA[
  270. $compat = new Zend_Controller_Router_Route_Module(array(),
  271. $dispatcher,
  272. $request);
  273. $this->addRoute('default', $compat);
  274. ]]></programlisting>
  275. <para>
  276. If you do not want this particular default route in your routing
  277. schema, you may override it by creating your own 'default' route
  278. (i.e., storing it under the name of 'default') or removing it
  279. altogether by using <code>removeDefaultRoutes()</code>:
  280. </para>
  281. <programlisting role="php"><![CDATA[
  282. // Remove any default routes
  283. $router->removeDefaultRoutes();
  284. ]]></programlisting>
  285. </sect2>
  286. <sect2 id="zend.controller.router.rewritebase">
  287. <title>Base URL and Subdirectories</title>
  288. <para>
  289. The rewrite router can be used in subdirectories (e.g.,
  290. <code>http://domain.com/~user/application-root/</code>) in which
  291. case the base URL of the application
  292. (<code>/~user/application-root</code>) should be automatically
  293. detected by <classname>Zend_Controller_Request_Http</classname> and used
  294. accordingly.
  295. </para>
  296. <para>
  297. Should the base URL be detected incorrectly you can override it with
  298. your own base path by using
  299. <classname>Zend_Controller_Request_Http</classname> and calling the
  300. <code>setBaseUrl()</code> method (see <xref
  301. linkend="zend.controller.request.http.baseurl" />):
  302. </para>
  303. <programlisting role="php"><![CDATA[
  304. $request->setBaseUrl('/~user/application-root/');
  305. ]]></programlisting>
  306. </sect2>
  307. <sect2 id="zend.controller.router.global.parameters">
  308. <title>Global Parameters</title>
  309. <para>
  310. You can set global parameters in a router which are automatically
  311. supplied to a route when assembling through the
  312. <code>setGlobalParam</code>. If a global parameter is set
  313. but also given to the assemble method directly, the user parameter
  314. overrides the global parameter. You can set a global parameter this
  315. way:
  316. </para>
  317. <programlisting role="php"><![CDATA[
  318. $router->setGlobalParam('lang', 'en');
  319. ]]></programlisting>
  320. </sect2>
  321. <sect2 id="zend.controller.router.routes">
  322. <title>Route Types</title>
  323. <xi:include href="Zend_Controller-Router-Route.xml" />
  324. <xi:include href="Zend_Controller-Router-Route-Static.xml" />
  325. <xi:include href="Zend_Controller-Router-Route-Regex.xml" />
  326. <xi:include href="Zend_Controller-Router-Route-Hostname.xml" />
  327. <xi:include href="Zend_Controller-Router-Route-Chain.xml" />
  328. </sect2>
  329. <sect2 id="zend.controller.router.add-config">
  330. <title>Using Zend_Config with the RewriteRouter</title>
  331. <para>
  332. Sometimes it is more convenient to update a configuration file with
  333. new routes than to change the code. This is possible via the
  334. <code>addConfig()</code> method. Basically, you create a
  335. <classname>Zend_Config</classname>-compatible configuration, and in your code read it in
  336. and pass it to the RewriteRouter.
  337. </para>
  338. <para>
  339. As an example, consider the following INI file:
  340. </para>
  341. <programlisting role="php"><![CDATA[
  342. [production]
  343. routes.archive.route = "archive/:year/*"
  344. routes.archive.defaults.controller = archive
  345. routes.archive.defaults.action = show
  346. routes.archive.defaults.year = 2000
  347. routes.archive.reqs.year = "\d+"
  348. routes.news.type = "Zend_Controller_Router_Route_Static"
  349. routes.news.route = "news"
  350. routes.news.defaults.controller = "news"
  351. routes.news.defaults.action = "list"
  352. routes.archive.type = "Zend_Controller_Router_Route_Regex"
  353. routes.archive.route = "archive/(\d+)"
  354. routes.archive.defaults.controller = "archive"
  355. routes.archive.defaults.action = "show"
  356. routes.archive.map.1 = "year"
  357. ; OR: routes.archive.map.year = 1
  358. ]]></programlisting>
  359. <para>
  360. The above INI file can then be read into a <classname>Zend_Config</classname>
  361. object as follows:
  362. </para>
  363. <programlisting role="php"><![CDATA[
  364. $config = new Zend_Config_Ini('/path/to/config.ini', 'production');
  365. $router = new Zend_Controller_Router_Rewrite();
  366. $router->addConfig($config, 'routes');
  367. ]]></programlisting>
  368. <para>
  369. In the above example, we tell the router to use the 'routes' section
  370. of the INI file to use for its routes. Each first-level key under
  371. that section will be used to define a route name; the above example
  372. defines the routes 'archive' and 'news'. Each route then requires,
  373. at minimum, a 'route' entry and one or more 'defaults' entries;
  374. optionally one or more 'reqs' (short for 'required') may be
  375. provided. All told, these correspond to the three arguments provided
  376. to a <classname>Zend_Controller_Router_Route_Interface</classname> object. An
  377. option key, 'type', can be used to specify the route class type to
  378. use for that particular route; by default, it uses
  379. <classname>Zend_Controller_Router_Route</classname>. In the example above, the
  380. 'news' route is defined to use
  381. <classname>Zend_Controller_Router_Route_Static</classname>.
  382. </para>
  383. </sect2>
  384. <sect2 id="zend.controller.router.subclassing">
  385. <title>Subclassing the Router</title>
  386. <para>
  387. The standard rewrite router should provide most functionality you
  388. may need; most often, you will only need to create a new route type
  389. in order to provide new or modified functionality over the provided
  390. routes.
  391. </para>
  392. <para>
  393. That said, you may at some point find yourself wanting to use a
  394. different routing paradigm. The interface
  395. <classname>Zend_Controller_Router_Interface</classname> provides the minimal
  396. information required to create a router, and consists of a single
  397. method.
  398. </para>
  399. <programlisting role="php"><![CDATA[
  400. interface Zend_Controller_Router_Interface
  401. {
  402. /**
  403. * @param Zend_Controller_Request_Abstract $request
  404. * @throws Zend_Controller_Router_Exception
  405. * @return Zend_Controller_Request_Abstract
  406. */
  407. public function route(Zend_Controller_Request_Abstract $request);
  408. }
  409. ]]></programlisting>
  410. <para>
  411. Routing only occurs once: when the request is first received into
  412. the system. The purpose of the router is to determine the
  413. controller, action, and optional parameters based on the request
  414. environment, and then set them in the request. The request object
  415. is then passed to the dispatcher. If it is not possible to map a
  416. route to a dispatch token, the router should do nothing to the
  417. request object.
  418. </para>
  419. </sect2>
  420. </sect1>
  421. <!--
  422. vim:se ts=4 sw=4 et:
  423. -->