2
0

Zend_Controller-Router.xml 19 KB

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