Programmer's Reference Guide

Seiten

Container

Container haben Methoden für das Hinzufügen, Empfangen, Löschen und Durchlaufen von Seiten. Container implementieren die » SPL Interfaces RecursiveIterator und Countable, was bedeutet das ein Container durchlaufen werden kann indem die SPL Klasse RecursiveIteratorIterator verwendet wird.

Erstellen von Containern

Zend_Navigation_Container ist abstrakt, und kann nicht direkt instanziiert werden. Verwende Zend_Navigation wenn ein Container instanziiert werden soll.

Zend_Navigation kann komplett leer erstellt werden, oder indem ein Array genommen wird, oder ein Zend_Config Objekt mit Seiten in den Container gegeben wird. Jede seite in dem gegebenen Array/Config wird eventuell an die addPage() Methode der Container Klasse übergeben, was bedeutet das jedes Element im Array/Config ein Array oder Config Objekt sein kann, oder eine Instanz von Zend_Navigation_Page.

Beispiel #1 Erstellt einen Container indem ein Array verwendet wird

  1. /*
  2. * Erstellt einen Container von einem Array
  3. *
  4. * Jedes Element im Array wird an Zend_Navigation_Page::factory() übergeben
  5. * wenn es erstellt wird.
  6. */
  7. $container = new Zend_Navigation(array(
  8.     array(
  9.         'label' => 'Seite 1',
  10.         'id' => 'home-link',
  11.         'uri' => '/'
  12.     ),
  13.     array(
  14.         'label' => 'Zend',
  15.         'uri' => 'http://www.zend-project.com/',
  16.         'order' => 100
  17.     ),
  18.     array(
  19.         'label' => 'Seite 2',
  20.         'controller' => 'page2',
  21.         'pages' => array(
  22.             array(
  23.                 'label' => 'Seite 2.1',
  24.                 'action' => 'page2_1',
  25.                 'controller' => 'page2',
  26.                 'class' => 'special-one',
  27.                 'title' => 'Dieses Element hat eine spezielle Klasse',
  28.                 'active' => true
  29.             ),
  30.             array(
  31.                 'label' => 'Seite 2.2',
  32.                 'action' => 'page2_2',
  33.                 'controller' => 'page2',
  34.                 'class' => 'special-two',
  35.                 'title' => 'Dieses Element hat auch eine spezielle Klasse'
  36.             )
  37.         )
  38.     ),
  39.     array(
  40.         'label' => 'Seite 2 mit Parametern',
  41.         'action' => 'index',
  42.         'controller' => 'page2',
  43.         // Spezifiziert einen Parameter oder zwei
  44.         'params' => array(
  45.             'format' => 'json',
  46.             'foo' => 'bar'
  47.         )
  48.     ),
  49.     array(
  50.         'label' => 'Seite 2 mit Parametern und einer Route',
  51.         'action' => 'index',
  52.         'controller' => 'page2',
  53.         // Spezifiziert einen Routen Namen und einen Parameter für die Route
  54.         'route' => 'nav-route-example',
  55.         'params' => array(
  56.             'format' => 'json'
  57.         )
  58.     ),
  59.     array(
  60.         'label' => 'Seite 3',
  61.         'action' => 'index',
  62.         'controller' => 'index',
  63.         'module' => 'mymodule',
  64.         'reset_params' => false
  65.     ),
  66.     array(
  67.         'label' => 'Seite 4',
  68.         'uri' => '#',
  69.         'pages' => array(
  70.             array(
  71.                 'label' => 'Seite 4.1',
  72.                 'uri' => '/page4',
  73.                 'title' => 'Seite 4 mit Verwendung von URI',
  74.                 'pages' => array(
  75.                     array(
  76.                         'label' => 'Seite 4.1.1',
  77.                         'title' => 'Seite 4 mit Verwendung von MVC Parametern',
  78.                         'action' => 'index',
  79.                         'controller' => 'page4',
  80.                         // Sagen wir das diese Seite aktiv ist
  81.                         'active' => '1'
  82.                     )
  83.                 )
  84.             )
  85.         )
  86.     ),
  87.     array(
  88.         'label' => 'Seite 0?',
  89.         'uri' => '/setting/the/order/option',
  90.         // Setzt die Reihenfolge auf -1, damit Sie als erstes erscheint
  91.         'order' => -1
  92.     ),
  93.     array(
  94.         'label' => 'Seite 5',
  95.         'uri' => '/',
  96.         // Diese Seite sollte nicht sichtbar sein
  97.         'visible' => false,
  98.         'pages' => array(
  99.             array(
  100.                 'label' => 'Seite 5.1',
  101.                 'uri' => '#',
  102.                 'pages' => array(
  103.                     array(
  104.                         'label' => 'Seite 5.1.1',
  105.                         'uri' => '#',
  106.                         'pages' => array(
  107.                             array(
  108.                                 'label' => 'Seite 5.1.2',
  109.                                 'uri' => '#',
  110.                                 // Sagen wir das die Seite aktiv ist
  111.                                 'active' => true
  112.                             )
  113.                         )
  114.                     )
  115.                 )
  116.             )
  117.         )
  118.     ),
  119.     array(
  120.         'label' => 'ACL Seite 1 (guest)',
  121.         'uri' => '#acl-guest',
  122.         'resource' => 'nav-guest',
  123.         'pages' => array(
  124.             array(
  125.                 'label' => 'ACL Seite 1.1 (foo)',
  126.                 'uri' => '#acl-foo',
  127.                 'resource' => 'nav-foo'
  128.             ),
  129.             array(
  130.                 'label' => 'ACL Seite 1.2 (bar)',
  131.                 'uri' => '#acl-bar',
  132.                 'resource' => 'nav-bar'
  133.             ),
  134.             array(
  135.                 'label' => 'ACL Seite 1.3 (baz)',
  136.                 'uri' => '#acl-baz',
  137.                 'resource' => 'nav-baz'
  138.             ),
  139.             array(
  140.                 'label' => 'ACL Seite 1.4 (bat)',
  141.                 'uri' => '#acl-bat',
  142.                 'resource' => 'nav-bat'
  143.             )
  144.         )
  145.     ),
  146.     array(
  147.         'label' => 'ACL Seite 2 (member)',
  148.         'uri' => '#acl-member',
  149.         'resource' => 'nav-member'
  150.     ),
  151.     array(
  152.         'label' => 'ACL Seite 3 (admin)',
  153.         'uri' => '#acl-admin',
  154.         'resource' => 'nav-admin',
  155.         'pages' => array(
  156.             array(
  157.                 'label' => 'ACL Seite 3.1 (nothing)',
  158.                 'uri' => '#acl-nada'
  159.             )
  160.         )
  161.     ),
  162.     array(
  163.         'label' => 'Zend Framework',
  164.         'route' => 'zf-route'
  165.     )
  166. ));

Beispiel #2 Erstellung eines Containers indem ein Config Objekt erstellt wird

  1. /* INHALT VON /path/to/navigation.xml:
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <config>
  4.     <nav>
  5.  
  6.         <zend>
  7.             <label>Zend</label>
  8.             <uri>http://www.zend-project.com/</uri>
  9.             <order>100</order>
  10.         </zend>
  11.  
  12.         <page1>
  13.             <label>Seite 1</label>
  14.             <uri>page1</uri>
  15.             <pages>
  16.  
  17.                 <page1_1>
  18.                     <label>Seite 1.1</label>
  19.                     <uri>page1/page1_1</uri>
  20.                 </page1_1>
  21.  
  22.             </pages>
  23.         </page1>
  24.  
  25.         <page2>
  26.             <label>Seite 2</label>
  27.             <uri>page2</uri>
  28.             <pages>
  29.  
  30.                 <page2_1>
  31.                     <label>Seite 2.1</label>
  32.                     <uri>page2/page2_1</uri>
  33.                 </page2_1>
  34.  
  35.                 <page2_2>
  36.                     <label>Seite 2.2</label>
  37.                     <uri>page2/page2_2</uri>
  38.                     <pages>
  39.  
  40.                         <page2_2_1>
  41.                             <label>Seite 2.2.1</label>
  42.                             <uri>page2/page2_2/page2_2_1</uri>
  43.                         </page2_2_1>
  44.  
  45.                         <page2_2_2>
  46.                             <label>Seite 2.2.2</label>
  47.                             <uri>page2/page2_2/page2_2_2</uri>
  48.                             <active>1</active>
  49.                         </page2_2_2>
  50.  
  51.                     </pages>
  52.                 </page2_2>
  53.  
  54.                 <page2_3>
  55.                     <label>Seite 2.3</label>
  56.                     <uri>page2/page2_3</uri>
  57.                     <pages>
  58.  
  59.                         <page2_3_1>
  60.                             <label>Seite 2.3.1</label>
  61.                             <uri>page2/page2_3/page2_3_1</uri>
  62.                         </page2_3_1>
  63.  
  64.                         <page2_3_2>
  65.                             <label>Seite 2.3.2</label>
  66.                             <uri>page2/page2_3/page2_3_2</uri>
  67.                             <visible>0</visible>
  68.                             <pages>
  69.  
  70.                                     <page2_3_2_1>
  71.                                         <label>Seite 2.3.2.1</label>
  72.                                         <uri>page2/page2_3/page2_3_2/1</uri>
  73.                                         <active>1</active>
  74.                                     </page2_3_2_1>
  75.  
  76.                                     <page2_3_2_2>
  77.                                         <label>Seite 2.3.2.2</label>
  78.                                         <uri>page2/page2_3/page2_3_2/2</uri>
  79.                                         <active>1</active>
  80.  
  81.                                         <pages>
  82.                                             <page_2_3_2_2_1>
  83.                                                 <label>Ignoriert</label>
  84.                                                 <uri>#</uri>
  85.                                                 <active>1</active>
  86.                                             </page_2_3_2_2_1>
  87.                                         </pages>
  88.                                     </page2_3_2_2>
  89.  
  90.                             </pages>
  91.                         </page2_3_2>
  92.  
  93.                         <page2_3_3>
  94.                             <label>Seite 2.3.3</label>
  95.                             <uri>page2/page2_3/page2_3_3</uri>
  96.                             <resource>admin</resource>
  97.                             <pages>
  98.  
  99.                                     <page2_3_3_1>
  100.                                         <label>Seite 2.3.3.1</label>
  101.                                         <uri>page2/page2_3/page2_3_3/1</uri>
  102.                                         <active>1</active>
  103.                                     </page2_3_3_1>
  104.  
  105.                                     <page2_3_3_2>
  106.                                         <label>Seite 2.3.3.2</label>
  107.                                         <uri>page2/page2_3/page2_3_3/2</uri>
  108.                                         <resource>guest</resource>
  109.                                         <active>1</active>
  110.                                     </page2_3_3_2>
  111.  
  112.                             </pages>
  113.                         </page2_3_3>
  114.  
  115.                     </pages>
  116.                 </page2_3>
  117.  
  118.             </pages>
  119.         </page2>
  120.  
  121.         <page3>
  122.             <label>Seite 3</label>
  123.             <uri>page3</uri>
  124.             <pages>
  125.  
  126.                 <page3_1>
  127.                     <label>Seite 3.1</label>
  128.                     <uri>page3/page3_1</uri>
  129.                     <resource>guest</resource>
  130.                 </page3_1>
  131.  
  132.                 <page3_2>
  133.                     <label>Seite 3.2</label>
  134.                     <uri>page3/page3_2</uri>
  135.                     <resource>member</resource>
  136.                     <pages>
  137.  
  138.                         <page3_2_1>
  139.                             <label>Seite 3.2.1</label>
  140.                             <uri>page3/page3_2/page3_2_1</uri>
  141.                         </page3_2_1>
  142.  
  143.                         <page3_2_2>
  144.                             <label>Seite 3.2.2</label>
  145.                             <uri>page3/page3_2/page3_2_2</uri>
  146.                             <resource>admin</resource>
  147.                         </page3_2_2>
  148.  
  149.                     </pages>
  150.                 </page3_2>
  151.  
  152.                 <page3_3>
  153.                     <label>Seite 3.3</label>
  154.                     <uri>page3/page3_3</uri>
  155.                     <resource>special</resource>
  156.                     <pages>
  157.  
  158.                         <page3_3_1>
  159.                             <label>Seite 3.3.1</label>
  160.                             <uri>page3/page3_3/page3_3_1</uri>
  161.                             <visible>0</visible>
  162.                         </page3_3_1>
  163.  
  164.                         <page3_3_2>
  165.                             <label>Seite 3.3.2</label>
  166.                             <uri>page3/page3_3/page3_3_2</uri>
  167.                             <resource>admin</resource>
  168.                         </page3_3_2>
  169.  
  170.                     </pages>
  171.                 </page3_3>
  172.  
  173.             </pages>
  174.         </page3>
  175.  
  176.         <home>
  177.             <label>Home</label>
  178.             <order>-100</order>
  179.             <module>default</module>
  180.             <controller>index</controller>
  181.             <action>index</action>
  182.         </home>
  183.  
  184.     </nav>
  185. </config>
  186. */
  187.  
  188. $config = new Zend_Config_Xml('/path/to/navigation.xml', 'nav');
  189. $container = new Zend_Navigation($config);

Hinzufügen von Seiten

Das Hinzufügen von Seiten in einen Container kann mit den Methoden addPage(), addPages() oder setPages() durchgeführt werden. Das folgende Beispiel zeigt eine Erklärung des ganzen.

Beispiel #3 Hinzufügen von Seiten zu einem Container

  1. // Container erstellen
  2. $container = new Zend_Navigation();
  3.  
  4. // Seite durch die Angabe eine Instanz einer Page hinzufügen
  5. $container->addPage(Zend_Navigation_Page::factory(array(
  6.     'uri' => 'http://www.example.com/'
  7. )))
  8.  
  9. // Seite durch die Angabe eines Arrays hinzufügen
  10. $container->addPage(array(
  11.     'uri' => 'http://www.example.com/'
  12. )))
  13.  
  14. // Seite durch die Angabe eines Config Objekts hinzufügen
  15. $container->addPage(new Zend_Config(array(
  16.     'uri' => 'http://www.example.com/'
  17. )))
  18.  
  19. $pages = array(
  20.     array(
  21.         'label'  => 'Speichern'
  22.         'action' => 'save',
  23.     ),
  24.     array(
  25.         'label' =>  'Löschen',
  26.         'action' => 'delete'
  27.     )
  28. );
  29.  
  30. // Zwei Seiten hinzufügen
  31. $container->addPages($pages);
  32.  
  33. // Bestehende Seite entfernen und die gegebenen Seiten hinzufügen
  34. $container->setPages($pages);

Seiten löschen

Das Löschen von Seiten kann mit removePage() oder removePages() durchgeführt werden. Die ersten Methode akzeptiert eine Instanz einer Seite, oder ein Integer. Der Integer korrespondiert mit der order welche die Seite hat. Die letztere Methode entfernt alle Seiten vom Container.

Beispiel #4 Seiten von einem Container entfernen

  1. $container = new Zend_Navigation(array(
  2.     array(
  3.         'label'  => 'Seite 1',
  4.         'action' => 'page1'
  5.     ),
  6.     array(
  7.         'label'  => 'Seite 2',
  8.         'action' => 'page2',
  9.         'order'  => 200
  10.     ),
  11.     array(
  12.         'label'  => 'Seite 3',
  13.         'action' => 'page3'
  14.     )
  15. ));
  16.  
  17. // Entfernt eine Seite implizit durch die Reihenfolge der Seite
  18. $container->removePage(0);      // Entfernt Seite 1
  19.  
  20. // Entfernt eine Seite durch die Instanz
  21. $page3 = $container->findOneByAction('Seite 3');
  22. $container->removePage($page3); // Entfernt Seite 3
  23.  
  24. // Entfernt eine Seite durch explizite angabe der Reihenfolge der Seite
  25. $container->removePage(200);    // Entfernt Seite 2
  26.  
  27. // Entfernt alle Seiten
  28. $container->removePages();      // Entfernt alle Seiten

Seiten finden

Container haben Finder Methoden für das Empfangen von Seiten. Es gibt findOneBy($property, $value), findAllBy($property, $value) und findBy($property, $value, $all = false). Diese Methoden durchsuchen rekursiv den Container nach Seiten die dem angegebenen $page->$property == $value entsprechen. Die erste Methode, findOneBy(), gibt eine einzelne Seite zurück die der angegebenen Eigenschaft mit dem angegebenen Wert entspricht, oder NULL wenn Sie nicht gefunden werden kann. Die zweite Methode wird alle Seiten mit einer Eigenschaft zurückgeben die dem angegebenen Wert entspricht. Die dritte Methode wird eine eine der zwei anderen Methoden aufrufen, abhängig vom $all Flag.

Die Finder Methoden können auch magisch verwendet werden indem der Name der Eigenschaft an findBy, findOneBy oder findAllBy angehängt wird, z.B. findOneByLabel('Home') um die erste passende Seite mit dem Label 'Home' zu finden. Andere Kombinationen sind findByLabel(...), findOnyByTitle(...), findAllByController(...), usw. Finder Methoden funktionieren auch mit eigenen Eigenschaften, so wie z.B. findByFoo('bar').

Beispiel #5 Seiten in einem Container finden

  1. $container = new Zend_Navigation(array(
  2.     array(
  3.         'label' => 'Seite 1',
  4.         'uri'   => 'page-1',
  5.         'foo'   => 'bar',
  6.         'pages' => array(
  7.             array(
  8.                 'label' => 'Seite 1.1',
  9.                 'uri'   => 'page-1.1',
  10.                 'foo'   => 'bar',
  11.             ),
  12.             array(
  13.                 'label' => 'Seite 1.2',
  14.                 'uri'   => 'page-1.2',
  15.                 'class' => 'my-class',
  16.             ),
  17.             array(
  18.                 'type'   => 'uri',
  19.                 'label'  => 'Seite 1.3',
  20.                 'uri'    => 'page-1.3',
  21.                 'action' => 'about'
  22.             )
  23.         )
  24.     ),
  25.     array(
  26.         'label'      => 'Seite 2',
  27.         'id'         => 'page_2_and_3',
  28.         'class'      => 'my-class',
  29.         'module'     => 'page2',
  30.         'controller' => 'index',
  31.         'action'     => 'page1'
  32.     ),
  33.     array(
  34.         'label'      => 'Seite 3',
  35.         'id'         => 'page_2_and_3',
  36.         'module'     => 'page3',
  37.         'controller' => 'index'
  38.     )
  39. ));
  40.  
  41. // Die 'id' muß nicht eindeutig sein, aber man sollte darauf achten das wenn
  42. // man zwei Seiten mit der gleichen Id hat, diese die gleichen Id Attribute in
  43. // Menüs und Breadcrumbs darstellen werden
  44. $found = $container->findBy('id',
  45.                             'page_2_and_3');      // Gibt Seite 2 zurück
  46. $found = $container->findOneBy('id',
  47.                                'page_2_and_3');   // Gibt Seite 2 zurück
  48. $found = $container->findBy('id',
  49.                             'page_2_and_3',
  50.                             true);                // Gibt Seite 2 und 3 zurück
  51. $found = $container->findById('page_2_and_3');    // Gibt Seite 2 zurück
  52. $found = $container->findOneById('page_2_and_3'); // Gibt Seite 2 zurück
  53. $found = $container->findAllById('page_2_and_3'); // Gibt Seite 2 und 3 zurück
  54.  
  55. // Finde alle zu my-class passenden CSS Klassen
  56. $found = $container->findAllBy('class',
  57.                                'my-class');      // Gibt Seite 1.2 und 2 zurück
  58. $found = $container->findAllByClass('my-class'); // Gibt Seite 1.2 und 2 zurück
  59.  
  60. // Finde die erste zu my-class passende CSS Klasse
  61. $found = $container->findOneByClass('my-class'); // Gibt Seite 1.2 zurück
  62.  
  63. // Findet alle zu non-existant passenden CSS Klassen
  64. $found = $container->findAllByClass('non-existant'); // Gibt array() zurück
  65.  
  66. // Findet die erste zu non-existant passende CSS Klasse
  67. $found = $container->findOneByClass('non-existant'); // Gibt null zurück
  68.  
  69. // Findet alle Seiten mit den eigenen Eigenschaften 'foo' = 'bar'
  70. $found = $container->findAllBy('foo', 'bar'); // Gibt Seite 1 und 1.1 zurück
  71.  
  72. // Um das gleiche auf Magische Weise zu ermöglichen, muß 'foo' kleingeschrieben
  73. // sein weil 'foo' eine eigene Eigenschaft ist und deshalb der Name der
  74. // Eigenschaft nicht zu 'Foo' normalisiert wird
  75. $found = $container->findAllByfoo('bar');
  76.  
  77. // Findet alle mit controller = 'index'
  78. $found = $container->findAllByController('index'); // Gibt Seite 2 und 3 zurück

Container durchsuchen

Zend_Navigation_Container implementiert RecursiveIteratorIterator, und kann mit jeder Iterator Klasse durchsucht werden. Um einen Container rekursiv zu durchsuchen, kann die RecursiveIteratorIterator Klasse verwendet werden.

Beispiel #6 Einen Container durchsuchen

  1. /*
  2. * Erstellt einen Container von einem Array
  3. */
  4. $container = new Zend_Navigation(array(
  5.     array(
  6.         'label' => 'Seite 1',
  7.         'uri'   => '#'
  8.     ),
  9.     array(
  10.         'label' => 'Seite 2',
  11.         'uri'   => '#',
  12.         'pages' => array(
  13.             array(
  14.                 'label' => 'Seite 2.1',
  15.                 'uri'   => '#'
  16.             ),
  17.             array(
  18.                 'label' => 'Seite 2.2',
  19.                 'uri'   => '#'
  20.             )
  21.         )
  22.     )
  23.     array(
  24.         'label' => 'Seite 3',
  25.         'uri'   => '#'
  26.     )
  27. ));
  28.  
  29. // Durchsucht flach indem ein normales foreach verwendet wird:
  30. // Ausgabe: Seite 1, Seite 2, Seite 3
  31. foreach ($container as $page) {
  32.     echo $page->label;
  33. }
  34.  
  35. // Durchsucht rekursiv indem RecursiveIteratorIterator verwendet wird
  36. $it = new RecursiveIteratorIterator(
  37.         $container, RecursiveIteratorIterator::SELF_FIRST);
  38.  
  39. // Ausgabe: Seite 1, Seite 2, Seite 2.1, Seite 2.2, Seite 3
  40. foreach ($it as $page) {
  41.     echo $page->label;
  42. }

Andere Operationen

Die Methode hasPage(Zend_Navigation_Page $page) prüft ob der Container die angegebene Seite besitzt. Die Methode hasPages() prüft ob irgendeine Seite im Container existiert, und ist gleich mit count($container) > 1.

Die toArray() Methode konvertiert den Container und die Seiten in Ihm zu einem Array. Das kann für eine Serialisierung und das Debugging nützlich sein.

Beispiel #7 Einen Container in ein Array konvertieren

  1. $container = new Zend_Navigation(array(
  2.     array(
  3.         'label' => 'Seite 1',
  4.         'uri'   => '#'
  5.     ),
  6.     array(
  7.         'label' => 'Seite 2',
  8.         'uri'   => '#',
  9.         'pages' => array(
  10.             array(
  11.                 'label' => 'Seite 2.1',
  12.                 'uri'   => '#'
  13.             ),
  14.             array(
  15.                 'label' => 'Seite 2.2',
  16.                'uri'   => '#'
  17.             )
  18.         )
  19.     )
  20. ));
  21.  
  22. var_dump($container->toArray());
  23.  
  24. /* Ausgabe:
  25. array(2) {
  26.   [0]=> array(15) {
  27.     ["label"]=> string(6) "Seite 1"
  28.     ["id"]=> NULL
  29.     ["class"]=> NULL
  30.     ["title"]=> NULL
  31.     ["target"]=> NULL
  32.     ["rel"]=> array(0) {
  33.     }
  34.     ["rev"]=> array(0) {
  35.     }
  36.     ["order"]=> NULL
  37.     ["resource"]=> NULL
  38.     ["privilege"]=> NULL
  39.     ["active"]=> bool(false)
  40.     ["visible"]=> bool(true)
  41.     ["type"]=> string(23) "Zend_Navigation_Page_Uri"
  42.     ["pages"]=> array(0) {
  43.     }
  44.     ["uri"]=> string(1) "#"
  45.   }
  46.   [1]=> array(15) {
  47.     ["label"]=> string(6) "Seite 2"
  48.     ["id"]=> NULL
  49.     ["class"]=> NULL
  50.     ["title"]=> NULL
  51.     ["target"]=> NULL
  52.     ["rel"]=> array(0) {
  53.     }
  54.     ["rev"]=> array(0) {
  55.     }
  56.     ["order"]=> NULL
  57.     ["resource"]=> NULL
  58.     ["privilege"]=> NULL
  59.     ["active"]=> bool(false)
  60.     ["visible"]=> bool(true)
  61.     ["type"]=> string(23) "Zend_Navigation_Page_Uri"
  62.     ["pages"]=> array(2) {
  63.       [0]=> array(15) {
  64.         ["label"]=> string(8) "Seite 2.1"
  65.         ["id"]=> NULL
  66.         ["class"]=> NULL
  67.         ["title"]=> NULL
  68.         ["target"]=> NULL
  69.         ["rel"]=> array(0) {
  70.         }
  71.         ["rev"]=> array(0) {
  72.         }
  73.         ["order"]=> NULL
  74.         ["resource"]=> NULL
  75.         ["privilege"]=> NULL
  76.         ["active"]=> bool(false)
  77.         ["visible"]=> bool(true)
  78.         ["type"]=> string(23) "Zend_Navigation_Page_Uri"
  79.         ["pages"]=> array(0) {
  80.         }
  81.         ["uri"]=> string(1) "#"
  82.       }
  83.       [1]=>
  84.       array(15) {
  85.         ["label"]=> string(8) "Seite 2.2"
  86.         ["id"]=> NULL
  87.         ["class"]=> NULL
  88.         ["title"]=> NULL
  89.         ["target"]=> NULL
  90.         ["rel"]=> array(0) {
  91.         }
  92.         ["rev"]=> array(0) {
  93.         }
  94.         ["order"]=> NULL
  95.         ["resource"]=> NULL
  96.         ["privilege"]=> NULL
  97.         ["active"]=> bool(false)
  98.         ["visible"]=> bool(true)
  99.         ["type"]=> string(23) "Zend_Navigation_Page_Uri"
  100.         ["pages"]=> array(0) {
  101.         }
  102.         ["uri"]=> string(1) "#"
  103.       }
  104.     }
  105.     ["uri"]=> string(1) "#"
  106.   }
  107. }
  108. */

Seiten

Comments

---this comment form captcha is bugging me---

I use a database to store my pages (or navigation) to be able to edit (CRUD) my navigation.

Example #1 (above) shows how to create a Container with an array. That's how I want to create my Container, but then dynamically from a database.

At the moment I'm stuck at finding the RIGHT way to do this and the right place to put my code.

My app looks as follows (simplified):
/application/configs/config.ini
/application/controllers/IndexController.php
/application/controllers/ProductsController.php
/application/forms/Products.php
/application/layouts/scripts/layout.phtml
/application/models/ProductsTable.php
/application/models/NavigationTable.php
/application/views/scripts/index/index.phtml
/application/views/scripts/products/index.phtml
/application/Bootstrap.php
/library/Zend/*
/public/css|js|index.php

Now, where should I put the code to read the Navigation databasetable and to create the array for Zend_Navigation_Container? (I get it, in my model, but I mean where do I call the model to do its job?)

>In the bootstrapper?
* Pro: Since a navigation menu is sitewide, I'm not going to add a line of code into every controller and action.
* Contra: The bootstrapper should not be responsible for retrieving database entries and passing them on to a view.
>In my layout.phtml?
* Pro: Its the place where the menu needs to be rendered.
* Contra: A View talking to a Model does not follow MVC standards.

Any other good places? Thanks in advance!
---then login---
---and post questions in the forum---
http://forums.zend.com/
The Example #6 Iterating a container example seems to be missing a comma sign on line 22:
*** 21,23 ****
)
! )
array(
--- 21,23 ----
)
! ),
array(
How come these examples are not very good at explaining how to implement them?
@barleone:
In case you still haven't figured out where to put the code that will help render your navigation menu,

"the code should be implemented via a view helper or action helper". I personally I use App_View_Helper_MenuModule (where "App" should be your aps namespace).

The structure for my App_View_Helper_MenuModule looks as follows:

_createPage(stdClass $link):array or (Zend_Navigation_Page_(Uri|Mvc))
_getLinkPages(stdClass $link):mixed
_getNavigationObject():Zend_Navigation
_injectAcl(Zend_Navigation $nav):Zend_Navigation
menuModule(array $options, Zend_View_Abstract $view = null)

and the call chain from within looks as follows:
get the navigation object ($nav = $this->_getNavigationObject())
get result set (using a model or service within _getNavigationObject())
loop through result set
_createPage(tuple from result set ($link))
_getLinkPages($link) (should be name getPagesForLink instead)

then return the rendered nav (from the main menuModule function of the view helper).

and here is where I create a page

    private function _createPage( stdClass $link) {
        $page = array(
            'label' => $link->label,
            'uri' => $link->uri
            //'id' => $link->id,
            //'class' => $link->class,
            //'title' => $link->title
            //'target' => $link->target,
            //'rel' => $link->rel
        );

        $children = $this->_getLinkPages($link);
        if ($children !== false) {
            $page['pages'] = $children;
        }

        return $page;
    }


Happy Phping!
Woops! Forgot to enforce the indent on the sudo code in my last comment. Here is the entire MenuModule View Helper I talked about there:


<?php
/**
 * Description of MenuModule
 *
 * @author ElyDeLaCruz
 */
class Edm_View_Helper_MenuModule extends
Edm_View_Helper_Abstract
{
    private 
$_linkServ;
    
    public function 
menuModule(array $options null,
            
Zend_View_Abstract $view null)
    {
        
$this->_linkServ = new Edm_Service_LinkService();

        
// Check if we need process options
        
if (!empty($options)) {

            
// Check if we already have valid key names
            
$validKeyNames $this->getValidKeyNames();

            
// If we don't set them.
            
if (empty($validKeyNames)) {
                
$this->setValidKeyNames(array('tuple''wrapInDivs''showTitle',
                    
'showHeader''showBody''showFooter''wrapContent',
                    
'attributes''injectAcl'));
            }

            
// Validate the option key names
            
$this->validateKeyNames($options);

            
// Set the options so that they are accessible via $this->
            
$this->setOptions($options);

            
// Set the tuple columns to options for this view helper
            // must cast it as an array since the tuples will be passed
            // stdClass objects
            
$this->setOptions((array)$this->tuple);
        }

        
// Check whether we have to set the view
        
if (!empty($view)) {
            
$this->setView($view);
        }

        
// Get a zend navigation object with all its pages (links) added to it.
        
$nav $this->_getNavigationObject();

        
$this->menuHelperName = empty($this->menuHelperName) ? 'EdmMenu' :
                    
$this->menuHelperName;

        
$this->content $this->view->navigation()
                ->
findHelper($this->menuHelperName)
                ->
setOnlyActiveBranch((bool)$this->onlyActiveBranch)
                ->
setMinDepth($this->minDepth)
                ->
setMaxDepth($this->maxDepth)
                ->
setUlClass($this->ulClass)
                ->
renderMenu($nav);

        
// Options for the plain html module
        
$plainHtmlModule_options = array('title' => $this->title,
            
'content' => $this->content'css_class' => $this->css_class,
            
'html_id' => $this->html_id'showTitle' => $this->showTitle,
            
'showFooter' => $this->showFooter,
            
'wrapContent' => $this->wrapContent);

        
// Return output wrapped in a plain html module html wrapper
        
return $this->wrapInDivs ?
                
$this->view->plainHtmlModule($plainHtmlModule_options) :
                
$this->content;

        
// minDepth
        // maxDepth
        // ulClass
        // onlyActiveBranch
        // renderParents
        // partial script
        // menuHelperName
        // module_id
        // menu_id
    
}

    
/**
     * Returns a Zend_Navigation object with its pages already populated
     * with links that have a menu_id of $menu_id
     * @param uint $menu_id
     * @return Zend_Navigation 
     */
    
private function _getNavigationObject()
    {        
        
// Get all top level links of this menu that are published
        
$rslt $this->_linkServ->read('t1.menu_id="'$this->menu_id .
                
'" AND t2.status="published" AND t1.parentId="0"');

        if (!empty(
$this->inheritFrom_menu_id)) {
            
// If inherits from another menu then get the links from that menu
            
$inherited =  $this->_linkServ->read('t1.menu_id="'.
                    
$this->inheritFrom_menu_id .
                    
'" AND t2.status="published" AND t1.parentId="0"');

            
$rslt array_merge($inherited$rslt);
        }

        
// Loop through top level link results and create a config array for our
        // Zend Navigation object.
        
$pages = array();
        foreach(
$rslt as $link) {
            
$link = (object) $link;
            
$pages[] = $this->_createPage($link);
        }
        
$nav = new Zend_Navigation($pages);
        if (
$this->injectAcl === true) {
            
$nav $this->_injectAcl($nav);
        }

        return 
$nav;
    }

    private function 
_getLinkPages(stdClass  $link) {

        
// Get all top level links of this menu that are published
        
$rslt $this->_linkServ->read('t1.menu_id="'$link->menu_id .
                
'" AND t2.status="published" AND t1.parentId="'.
                
$link->link_id .'"');

        
// Loop through results
        
$pages = array(); $i 0;
        foreach(
$rslt as $child) {
            
            
// Cast child tuple as object
            
$child = (object) $child;
            
            
// add Child to pages array
            
$pages[] = $this->_createPage($child);
        }

        return (
count($pages) > $pages false);
    }

    
/**
     * Puts the pertinent link information needed to create a Zend_Nav..._Page
     * within an array.
     * @param object $link
     * @return array
     */
    
private function _createPagestdClass $link) {
        
$page = array(
            
'label' => $link->label,
            
'uri' => $link->uri
            
//'id' => $link->id,
            //'class' => $link->class,
            //'title' => $link->title
            //'target' => $link->target,
            //'rel' => $link->rel
        
);

        
$children $this->_getLinkPages($link);
        if (
$children !== false) {
            
$page['pages'] = $children;
        }

        return 
$page;
    }

    
/**
     * Injects the front acl into the Zend_Navigation object passed in
     * @param Zend_Navigation $nav
     * @return Zend_Navigation
     */
    
private function _injectAclZend_Navigation $nav) {
        
// Pass acl to navigation
        
$acl_config = new Zend_Config_Ini(
                    
APPLICATION_PATH .'/configs/acl.ini',
                    
APPLICATION_ENV);
        
$acl = new Edm_Acl($acl_config);
        
$auth Zend_Auth::getInstance();
        
$role $auth->hasIdentity() ?
                (string)(
$auth->getIdentity()->role) : 'guest';
        
$this->view->navigation($nav)->setAcl($acl)->setRole($role);
        return 
$nav;
        
    }

}
About last comment and view helper example:

The setOnlyActiveBranch((bool)$var) doesn't work for me. If you leave the line out the class works fine. Kudos.
Very nice example Ely De La Cruz,
tried to make it work, but then realise that i am missing one part :)
$this->_linkServ = new Edm_Service_LinkService();
Any chance to see whats in there? :)
Thanks anyway!
Dovla
FYI,
"Example #2 Creating a container using a config object" is not correct. You do not need the nav node.
In regards to the guy who asked: 'How come these examples are not very good at explaining how to implement them?'

I'm also asking the same question and I think now after reading so many documents in the documentation that they're receiving income from pushing people through consultancy and support packages. The documentation appears as vague as possible and to make it much more likely that you can't do anything.

For example: the Zend_Form tutorial tells you to create your forms like ->addElement() and then the Zend_Form_Decorator examples are showing you how to implement them by using $element->addDecorator rather than 'decorators' => which fits directly into the first example.

Also, stuff like Zend_Form is extremely picky about using the correct case and it doesn't actually make logical sense so you have to spend a few hours, days searching the internet looking for a solution.

As the guy before me says 'where do I put this?'. Well it's a framework and frameworks are there to follow a standard way of doing things. I haven't seen one page yet that explains how to actually implement anything and how to follow the general rules it lays out.

I've survived thus far through asking absolutely every question through stackoverflow and even then they're pointing you to articles that are only relevant to a particular setup, installation, version and use terms that haven't been explained or detailed.

We've all been funneled through to the Zend Framework through celebrity endorsement. "Oh this is by the ACTUAL creators of PHP..." why would you use anything else? If they were behind CakePHP or any other framework for instance then we'd all be attempting to use that as well. Totally regretting even attempting to port my application over to Zend!

It's surprising really as the PHP manual has one of the best documentations available but that's to get you through the door then they can start charging you at Zend Framework...
Ignore EVERYTHING up there. It's CRAP. This is what you're after:

Copy and paste this (1.11)

/application/configs/navigation.xml
--------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<configdata>
<nav>

<home>
<label>Home</label>
<controller>index</controller>
<action>index</action>
</home>

<user>
<label>Users</label>
<controller>user</controller>
<action>index</action>

<pages>
<login>
<label>Login</label>
<controller>index</controller>
<action>index</action>
</login>
<register>
<label>Register</label>
<controller>register</controller>
<action>index</action>
</register>
</pages>
</user>

</nav>
</configdata>


/application/Bootstrap.php
-------------------------------------------------

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

protected function _initNavigation()
{

$this->bootstrap("layout");
$layout = $this->getResource('layout');
$view = $layout->getView();

$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml','nav');
$navigation = new Zend_Navigation($config);

$view->navigation($navigation);

}

}


application/layouts/scripts/layout.phtml
---------------------------------------------------------

<!DOCTYPE html>
<html>
<head>
<?php echo $this->headTitle() ?>
<?php echo $this->headMeta() ?>
<?php echo $this->headScript() ?>
<?php echo $this->headStyle() ?>
</head>
<body>
<?php echo $this->navigation()->menu()->setMaxDepth(1); ?>
<?php echo $this->layout()->content; ?>
</body>
</html>



Bing, bang, boom. This is ALL anyone wanted to come here for.
Is there any way to attach html tag parameters to "li" elements of rendered navigation menu? All html paramters of "Pages" instances are added to anchor tag.
Extend the default menu helper so that you can inject your ”li” options through there (I did this a while ago to do ”<li class=”active”>” on the condition that the current link was the active one).

+ Add A Comment

Please do not report issues via comments; use the ZF Issue Tracker.

If you have a JIRA/Crowd account, we suggest you login first before commenting.

  • BBCode is allowed in the comment markup

  • Select a Version

    Languages Available

    Components

    Search the Manual