Zend_Navigation-Pages-MVC.xml 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. <row>
  65. <entry><code>encode_url</code></entry>
  66. <entry><code>bool</code></entry>
  67. <entry><constant>TRUE</constant></entry>
  68. <entry>
  69. Whether href should be encoded when assembling URL.
  70. </entry>
  71. </row>
  72. </tbody>
  73. </tgroup>
  74. </table>
  75. <note>
  76. <para>
  77. The three examples below assume a default <acronym>MVC</acronym> setup with
  78. the <code>default</code> route in place.
  79. </para>
  80. <para>
  81. The <acronym>URI</acronym> returned is relative to the <code>baseUrl</code> in
  82. <classname>Zend_Controller_Front</classname>. In the examples, the baseUrl
  83. is '/' for simplicity.
  84. </para>
  85. </note>
  86. <example id="zend.navigation.pages.mvc.example.getHref">
  87. <title>getHref() generates the page URI</title>
  88. <para>
  89. This example shows that <acronym>MVC</acronym> pages use
  90. <classname>Zend_Controller_Action_Helper_Url</classname> internally
  91. to generate <acronym>URI</acronym>s when calling <code>$page->getHref()</code>.
  92. </para>
  93. <programlisting language="php"><![CDATA[
  94. // getHref() returns /
  95. $page = new Zend_Navigation_Page_Mvc(array(
  96. 'action' => 'index',
  97. 'controller' => 'index'
  98. ));
  99. // getHref() returns /blog/post/view
  100. $page = new Zend_Navigation_Page_Mvc(array(
  101. 'action' => 'view',
  102. 'controller' => 'post',
  103. 'module' => 'blog'
  104. ));
  105. // getHref() returns /blog/post/view/id/1337
  106. $page = new Zend_Navigation_Page_Mvc(array(
  107. 'action' => 'view',
  108. 'controller' => 'post',
  109. 'module' => 'blog',
  110. 'params' => array('id' => 1337)
  111. ));
  112. ]]></programlisting>
  113. </example>
  114. <example id="zend.navigation.pages.mvc.example.isActive">
  115. <title>isActive() determines if page is active</title>
  116. <para>
  117. This example shows that <acronym>MVC</acronym> pages determine whether they are active
  118. by using the params found in the request object.
  119. </para>
  120. <programlisting language="php"><![CDATA[
  121. /*
  122. * Dispatched request:
  123. * - module: default
  124. * - controller: index
  125. * - action: index
  126. */
  127. $page1 = new Zend_Navigation_Page_Mvc(array(
  128. 'action' => 'index',
  129. 'controller' => 'index'
  130. ));
  131. $page2 = new Zend_Navigation_Page_Mvc(array(
  132. 'action' => 'bar',
  133. 'controller' => 'index'
  134. ));
  135. $page1->isActive(); // returns true
  136. $page2->isActive(); // returns false
  137. /*
  138. * Dispatched request:
  139. * - module: blog
  140. * - controller: post
  141. * - action: view
  142. * - id: 1337
  143. */
  144. $page = new Zend_Navigation_Page_Mvc(array(
  145. 'action' => 'view',
  146. 'controller' => 'post',
  147. 'module' => 'blog'
  148. ));
  149. // returns true, because request has the same module, controller and action
  150. $page->isActive();
  151. /*
  152. * Dispatched request:
  153. * - module: blog
  154. * - controller: post
  155. * - action: view
  156. */
  157. $page = new Zend_Navigation_Page_Mvc(array(
  158. 'action' => 'view',
  159. 'controller' => 'post',
  160. 'module' => 'blog',
  161. 'params' => array('id' => null)
  162. ));
  163. // returns false, because page requires the id param to be set in the request
  164. $page->isActive(); // returns false
  165. ]]></programlisting>
  166. </example>
  167. <example id="zend.navigation.pages.mvc.example.routes">
  168. <title>Using routes</title>
  169. <para>
  170. Routes can be used with <acronym>MVC</acronym> pages. If a page has a route, this
  171. route will be used in <methodname>getHref()</methodname> to generate the
  172. <acronym>URL</acronym> for the page.
  173. </para>
  174. <para>
  175. <note>
  176. <para>
  177. Note that when using the <code>route</code> property in a
  178. page, you should also specify the default params that the
  179. route defines (module, controller, action, etc.), otherwise
  180. the <methodname>isActive()</methodname> method will not be able to
  181. determine if the page is active. The reason for this is that
  182. there is currently no way to get the default params from a
  183. <classname>Zend_Controller_Router_Route_Interface</classname> object,
  184. nor to retrieve the current route from a
  185. <classname>Zend_Controller_Router_Interface</classname> object.
  186. </para>
  187. </note>
  188. </para>
  189. <programlisting language="php"><![CDATA[
  190. // the following route is added to the ZF router
  191. Zend_Controller_Front::getInstance()->getRouter()->addRoute(
  192. 'article_view', // route name
  193. new Zend_Controller_Router_Route(
  194. 'a/:id',
  195. array(
  196. 'module' => 'news',
  197. 'controller' => 'article',
  198. 'action' => 'view',
  199. 'id' => null
  200. )
  201. )
  202. );
  203. // a page is created with a 'route' option
  204. $page = new Zend_Navigation_Page_Mvc(array(
  205. 'label' => 'A news article',
  206. 'route' => 'article_view',
  207. 'module' => 'news', // required for isActive(), see note above
  208. 'controller' => 'article', // required for isActive(), see note above
  209. 'action' => 'view', // required for isActive(), see note above
  210. 'params' => array('id' => 42)
  211. ));
  212. // returns: /a/42
  213. $page->getHref();
  214. ]]></programlisting>
  215. </example>
  216. <example id="zend.navigation.pages.mvc.example.setparams">
  217. <title>Set parameters to use when assembling URL</title>
  218. <programlisting language="php">
  219. // The following route is added to the ZF router
  220. Zend_Controller_Front::getInstance()->getRouter()->addRoute(
  221. 'article_list', // route name
  222. new Zend_Controller_Router_Route(
  223. 'blog/:category/:page',
  224. array(
  225. 'module' => 'blog',
  226. 'controller' => 'article',
  227. 'action' => 'list',
  228. 'category' => null,
  229. 'page' => null,
  230. )
  231. )
  232. );
  233. // A page is created with the 'route' option
  234. $page = new Zend_Navigation_Page_Mvc(array(
  235. 'label' => 'Article list',
  236. 'module' => 'blog',
  237. 'controller' => 'post',
  238. 'action' => 'list',
  239. ));
  240. // Add multiple parameters at once
  241. $page->addParams(
  242. array(
  243. 'category' => 'news',
  244. 'page' => 1,
  245. )
  246. );
  247. // Add a single parameter
  248. $page->addParam('category', 'news');
  249. // Set multiple parameters at once (Overwrites any previously set parameters!)
  250. $page->setParams(
  251. array(
  252. 'category' => 'news',
  253. 'page' => 1,
  254. )
  255. );
  256. // Set a single parameter
  257. $page->setParam('category', 'news');
  258. // Retrieve all parameters
  259. $params = $page->getParams();
  260. // Retrieve a single parameter
  261. $category = $page->getParam('category');
  262. // Remove a parameter
  263. $page->removeParam('page');
  264. // Clear all parameters
  265. $page->clearParams();
  266. </programlisting>
  267. </example>
  268. </sect2>