2
0

Zend_Navigation-Pages-MVC.xml 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.navigation.pages.mvc">
  4. <title>Zend_Navigation_Page_Mvc</title>
  5. <para>
  6. MVC pages are defined using MVC parameters known from the
  7. <classname>Zend_Controller</classname> component. An MVC page will use
  8. <classname>Zend_Controller_Action_Helper_Url</classname> internally
  9. in the <code>getHref()</code> method to generate hrefs, and
  10. the <code>isActive()</code> method will intersect the
  11. <classname>Zend_Controller_Request_Abstract</classname> params
  12. with the page's params to determine if the page is active.
  13. </para>
  14. <table id="zend.navigation.pages.mvc.options">
  15. <title>MVC page options</title>
  16. <tgroup cols="4">
  17. <thead>
  18. <row>
  19. <entry>Key</entry>
  20. <entry>Type</entry>
  21. <entry>Default</entry>
  22. <entry>Description</entry>
  23. </row>
  24. </thead>
  25. <tbody>
  26. <row>
  27. <entry><code>action</code></entry>
  28. <entry><type>String</type></entry>
  29. <entry><constant>NULL</constant></entry>
  30. <entry>
  31. Action name to use when generating
  32. href to the page.
  33. </entry>
  34. </row>
  35. <row>
  36. <entry><code>controller</code></entry>
  37. <entry><type>String</type></entry>
  38. <entry><constant>NULL</constant></entry>
  39. <entry>
  40. Controller name to use when generating
  41. href to the page.
  42. </entry>
  43. </row>
  44. <row>
  45. <entry><code>module</code></entry>
  46. <entry><type>String</type></entry>
  47. <entry><constant>NULL</constant></entry>
  48. <entry>
  49. Module name to use when generating
  50. href to the page.
  51. </entry>
  52. </row>
  53. <row>
  54. <entry><code>params</code></entry>
  55. <entry><type>Array</type></entry>
  56. <entry><code>array()</code></entry>
  57. <entry>
  58. User params to use when generating href to the page.
  59. </entry>
  60. </row>
  61. <row>
  62. <entry><code>route</code></entry>
  63. <entry><type>String</type></entry>
  64. <entry><constant>NULL</constant></entry>
  65. <entry>
  66. Route name to use when generating
  67. href to the page.
  68. </entry>
  69. </row>
  70. <row>
  71. <entry><code>reset_params</code></entry>
  72. <entry><code>bool</code></entry>
  73. <entry><constant>TRUE</constant></entry>
  74. <entry>
  75. Whether user params should be reset
  76. when generating href to the page.
  77. </entry>
  78. </row>
  79. </tbody>
  80. </tgroup>
  81. </table>
  82. <note>
  83. <para>
  84. The three examples below assume a default MVC setup with
  85. the <code>default</code> route in place.
  86. </para>
  87. <para>
  88. The URI returned is relative to the <code>baseUrl</code> in
  89. <classname>Zend_Controller_Front</classname>. In the examples, the baseUrl
  90. is '/' for simplicity.
  91. </para>
  92. </note>
  93. <example id="zend.navigation.pages.mvc.example.getHref">
  94. <title>getHref() generates the page URI</title>
  95. <para>
  96. This example show that MVC pages use
  97. <classname>Zend_Controller_Action_Helper_Url</classname> internally
  98. to generate URIs when calling <code>$page->getHref()</code>.
  99. </para>
  100. <programlisting language="php"><![CDATA[
  101. // getHref() returns /
  102. $page = new Zend_Navigation_Page_Mvc(array(
  103. 'action' => 'index',
  104. 'controller' => 'index'
  105. ));
  106. // getHref() returns /blog/post/view
  107. $page = new Zend_Navigation_Page_Mvc(array(
  108. 'action' => 'view',
  109. 'controller' => 'post',
  110. 'module' => 'blog'
  111. ));
  112. // getHref() returns /blog/post/view/id/1337
  113. $page = new Zend_Navigation_Page_Mvc(array(
  114. 'action' => 'view',
  115. 'controller' => 'post',
  116. 'module' => 'blog',
  117. 'params' => array('id' => 1337)
  118. ));
  119. ]]></programlisting>
  120. </example>
  121. <example id="zend.navigation.pages.mvc.example.isActive">
  122. <title>isActive() determines if page is active</title>
  123. <para>
  124. This example show that MVC pages determine whether they are active
  125. by using the params found in the request object.
  126. </para>
  127. <programlisting language="php"><![CDATA[
  128. /*
  129. * Dispatched request:
  130. * - module: default
  131. * - controller: index
  132. * - action: index
  133. */
  134. $page1 = new Zend_Navigation_Page_Mvc(array(
  135. 'action' => 'index',
  136. 'controller' => 'index'
  137. ));
  138. $page2 = new Zend_Navigation_Page_Mvc(array(
  139. 'action' => 'bar',
  140. 'controller' => 'index'
  141. ));
  142. $page1->isActive(); // returns true
  143. $page2->isActive(); // returns false
  144. /*
  145. * Dispatched request:
  146. * - module: blog
  147. * - controller: post
  148. * - action: view
  149. * - id: 1337
  150. */
  151. $page = new Zend_Navigation_Page_Mvc(array(
  152. 'action' => 'view',
  153. 'controller' => 'post',
  154. 'module' => 'blog'
  155. ));
  156. // returns true, because request has the same module, controller and action
  157. $page->isActive();
  158. /*
  159. * Dispatched request:
  160. * - module: blog
  161. * - controller: post
  162. * - action: view
  163. */
  164. $page = new Zend_Navigation_Page_Mvc(array(
  165. 'action' => 'view',
  166. 'controller' => 'post',
  167. 'module' => 'blog',
  168. 'params' => array('id' => null)
  169. ));
  170. // returns false, because page requires the id param to be set in the request
  171. $page->isActive(); // returns false
  172. ]]></programlisting>
  173. </example>
  174. <example id="zend.navigation.pages.mvc.example.routes">
  175. <title>Using routes</title>
  176. <para>
  177. Routes can be used with MVC pages. If a page has a route, this
  178. route will be used in <code>getHref()</code> to generate the URL
  179. for the page.
  180. </para>
  181. <para>
  182. <note>
  183. <para>
  184. Note that when using the <code>route</code> property in a
  185. page, you should also specify the default params that the
  186. route defines (module, controller, action, etc.), otherwise
  187. the <code>isActive()</code> method will not be able to
  188. determine if the page is active. The reason for this is that
  189. there is currently no way to get the default params from a
  190. <classname>Zend_Controller_Router_Route_Interface</classname> object,
  191. nor to retrieve the current route from a
  192. <classname>Zend_Controller_Router_Interface</classname> object.
  193. </para>
  194. </note>
  195. </para>
  196. <programlisting language="php"><![CDATA[
  197. // the following route is added to the ZF router
  198. Zend_Controller_Front::getInstance()->getRouter()->addRoute(
  199. 'article_view', // route name
  200. new Zend_Controller_Router_Route(
  201. 'a/:id',
  202. array(
  203. 'module' => 'news',
  204. 'controller' => 'article',
  205. 'action' => 'view',
  206. 'id' => null
  207. )
  208. )
  209. );
  210. // a page is created with a 'route' option
  211. $page = new Zend_Navigation_Page_Mvc(array(
  212. 'label' => 'A news article',
  213. 'route' => 'article_view',
  214. 'module' => 'news', // required for isActive(), see note above
  215. 'controller' => 'article', // required for isActive(), see note above
  216. 'action' => 'view', // required for isActive(), see note above
  217. 'params' => array('id' => 42)
  218. ));
  219. // returns: /a/42
  220. $page->getHref();
  221. ]]></programlisting>
  222. </example>
  223. </sect2>