Zend_Controller-Router.xml 19 KB

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