Zend_Navigation-Pages-Custom.xml 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.navigation.pages.custom">
  4. <title>Creating custom page types</title>
  5. <para>
  6. When extending <classname>Zend_Navigation_Page</classname>, there is
  7. usually no need to override the constructor or the methods
  8. <methodname>setOptions()</methodname> or <methodname>setConfig()</methodname>. The page
  9. constructor takes a single parameter, an <type>Array</type> or a
  10. <classname>Zend_Config</classname> object, which is passed to
  11. <methodname>setOptions()</methodname> or <methodname>setConfig()</methodname> respectively.
  12. Those methods will in turn call <methodname>set()</methodname> method, which
  13. will map options to native or custom properties. If the option
  14. <property>internal_id</property> is given, the method will first look for a
  15. method named <methodname>setInternalId()</methodname>, and pass the option to this
  16. method if it exists. If the method does not exist, the option will be
  17. set as a custom property of the page, and be accessible via
  18. <command>$internalId = $page->internal_id;</command> or
  19. <command>$internalId = $page->get('internal_id');</command>.
  20. </para>
  21. <example id="zend.navigation.custom.example.simple">
  22. <title>The most simple custom page</title>
  23. <para>
  24. The only thing a custom page class needs to implement is the
  25. <methodname>getHref()</methodname> method.
  26. </para>
  27. <programlisting language="php"><![CDATA[
  28. class My_Simple_Page extends Zend_Navigation_Page
  29. {
  30. public function getHref()
  31. {
  32. return 'something-completely-different';
  33. }
  34. }
  35. ]]></programlisting>
  36. </example>
  37. <example id="zend.navigation.custom.example.properties">
  38. <title>A custom page with properties</title>
  39. <para>
  40. When adding properties to an extended page, there is no need
  41. to override/modify <methodname>setOptions()</methodname> or
  42. <methodname>setConfig()</methodname>.
  43. </para>
  44. <programlisting language="php"><![CDATA[
  45. class My_Navigation_Page extends Zend_Navigation_Page
  46. {
  47. private $_foo;
  48. private $_fooBar;
  49. public function setFoo($foo)
  50. {
  51. $this->_foo = $foo;
  52. }
  53. public function getFoo()
  54. {
  55. return $this->_foo;
  56. }
  57. public function setFooBar($fooBar)
  58. {
  59. $this->_fooBar = $fooBar;
  60. }
  61. public function getFooBar()
  62. {
  63. return $this->_fooBar;
  64. }
  65. public function getHref()
  66. {
  67. return $this->foo . '/' . $this->fooBar;
  68. }
  69. }
  70. // can now construct using
  71. $page = new My_Navigation_Page(array(
  72. 'label' => 'Property names are mapped to setters',
  73. 'foo' => 'bar',
  74. 'foo_bar' => 'baz'
  75. ));
  76. // ...or
  77. $page = Zend_Navigation_Page::factory(array(
  78. 'type' => 'My_Navigation_Page',
  79. 'label' => 'Property names are mapped to setters',
  80. 'foo' => 'bar',
  81. 'foo_bar' => 'baz'
  82. ));
  83. ]]></programlisting>
  84. </example>
  85. </sect2>