Zend_Navigation-Pages-MVC.xml 8.0 KB

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