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
- /*
- * Erstellt einen Container von einem Array
- *
- * Jedes Element im Array wird an Zend_Navigation_Page::factory() übergeben
- * wenn es erstellt wird.
- */
- 'label' => 'Seite 1',
- 'id' => 'home-link',
- 'uri' => '/'
- ),
- 'label' => 'Zend',
- 'uri' => 'http://www.zend-project.com/',
- 'order' => 100
- ),
- 'label' => 'Seite 2',
- 'controller' => 'page2',
- 'label' => 'Seite 2.1',
- 'action' => 'page2_1',
- 'controller' => 'page2',
- 'class' => 'special-one',
- 'title' => 'Dieses Element hat eine spezielle Klasse',
- 'active' => true
- ),
- 'label' => 'Seite 2.2',
- 'action' => 'page2_2',
- 'controller' => 'page2',
- 'class' => 'special-two',
- 'title' => 'Dieses Element hat auch eine spezielle Klasse'
- )
- )
- ),
- 'label' => 'Seite 2 mit Parametern',
- 'action' => 'index',
- 'controller' => 'page2',
- // Spezifiziert einen Parameter oder zwei
- 'format' => 'json',
- 'foo' => 'bar'
- )
- ),
- 'label' => 'Seite 2 mit Parametern und einer Route',
- 'action' => 'index',
- 'controller' => 'page2',
- // Spezifiziert einen Routen Namen und einen Parameter für die Route
- 'route' => 'nav-route-example',
- 'format' => 'json'
- )
- ),
- 'label' => 'Seite 3',
- 'action' => 'index',
- 'controller' => 'index',
- 'module' => 'mymodule',
- 'reset_params' => false
- ),
- 'label' => 'Seite 4',
- 'uri' => '#',
- 'label' => 'Seite 4.1',
- 'uri' => '/page4',
- 'title' => 'Seite 4 mit Verwendung von URI',
- 'label' => 'Seite 4.1.1',
- 'title' => 'Seite 4 mit Verwendung von MVC Parametern',
- 'action' => 'index',
- 'controller' => 'page4',
- // Sagen wir das diese Seite aktiv ist
- 'active' => '1'
- )
- )
- )
- )
- ),
- 'label' => 'Seite 0?',
- 'uri' => '/setting/the/order/option',
- // Setzt die Reihenfolge auf -1, damit Sie als erstes erscheint
- 'order' => -1
- ),
- 'label' => 'Seite 5',
- 'uri' => '/',
- // Diese Seite sollte nicht sichtbar sein
- 'visible' => false,
- 'label' => 'Seite 5.1',
- 'uri' => '#',
- 'label' => 'Seite 5.1.1',
- 'uri' => '#',
- 'label' => 'Seite 5.1.2',
- 'uri' => '#',
- // Sagen wir das die Seite aktiv ist
- 'active' => true
- )
- )
- )
- )
- )
- )
- ),
- 'label' => 'ACL Seite 1 (guest)',
- 'uri' => '#acl-guest',
- 'resource' => 'nav-guest',
- 'label' => 'ACL Seite 1.1 (foo)',
- 'uri' => '#acl-foo',
- 'resource' => 'nav-foo'
- ),
- 'label' => 'ACL Seite 1.2 (bar)',
- 'uri' => '#acl-bar',
- 'resource' => 'nav-bar'
- ),
- 'label' => 'ACL Seite 1.3 (baz)',
- 'uri' => '#acl-baz',
- 'resource' => 'nav-baz'
- ),
- 'label' => 'ACL Seite 1.4 (bat)',
- 'uri' => '#acl-bat',
- 'resource' => 'nav-bat'
- )
- )
- ),
- 'label' => 'ACL Seite 2 (member)',
- 'uri' => '#acl-member',
- 'resource' => 'nav-member'
- ),
- 'label' => 'ACL Seite 3 (admin)',
- 'uri' => '#acl-admin',
- 'resource' => 'nav-admin',
- 'label' => 'ACL Seite 3.1 (nothing)',
- 'uri' => '#acl-nada'
- )
- )
- ),
- 'label' => 'Zend Framework',
- 'route' => 'zf-route'
- )
- ));
Beispiel #2 Erstellung eines Containers indem ein Config Objekt erstellt wird
- /* INHALT VON /path/to/navigation.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <config>
- <nav>
- <zend>
- <label>Zend</label>
- <uri>http://www.zend-project.com/</uri>
- <order>100</order>
- </zend>
- <page1>
- <label>Seite 1</label>
- <uri>page1</uri>
- <pages>
- <page1_1>
- <label>Seite 1.1</label>
- <uri>page1/page1_1</uri>
- </page1_1>
- </pages>
- </page1>
- <page2>
- <label>Seite 2</label>
- <uri>page2</uri>
- <pages>
- <page2_1>
- <label>Seite 2.1</label>
- <uri>page2/page2_1</uri>
- </page2_1>
- <page2_2>
- <label>Seite 2.2</label>
- <uri>page2/page2_2</uri>
- <pages>
- <page2_2_1>
- <label>Seite 2.2.1</label>
- <uri>page2/page2_2/page2_2_1</uri>
- </page2_2_1>
- <page2_2_2>
- <label>Seite 2.2.2</label>
- <uri>page2/page2_2/page2_2_2</uri>
- <active>1</active>
- </page2_2_2>
- </pages>
- </page2_2>
- <page2_3>
- <label>Seite 2.3</label>
- <uri>page2/page2_3</uri>
- <pages>
- <page2_3_1>
- <label>Seite 2.3.1</label>
- <uri>page2/page2_3/page2_3_1</uri>
- </page2_3_1>
- <page2_3_2>
- <label>Seite 2.3.2</label>
- <uri>page2/page2_3/page2_3_2</uri>
- <visible>0</visible>
- <pages>
- <page2_3_2_1>
- <label>Seite 2.3.2.1</label>
- <uri>page2/page2_3/page2_3_2/1</uri>
- <active>1</active>
- </page2_3_2_1>
- <page2_3_2_2>
- <label>Seite 2.3.2.2</label>
- <uri>page2/page2_3/page2_3_2/2</uri>
- <active>1</active>
- <pages>
- <page_2_3_2_2_1>
- <label>Ignoriert</label>
- <uri>#</uri>
- <active>1</active>
- </page_2_3_2_2_1>
- </pages>
- </page2_3_2_2>
- </pages>
- </page2_3_2>
- <page2_3_3>
- <label>Seite 2.3.3</label>
- <uri>page2/page2_3/page2_3_3</uri>
- <resource>admin</resource>
- <pages>
- <page2_3_3_1>
- <label>Seite 2.3.3.1</label>
- <uri>page2/page2_3/page2_3_3/1</uri>
- <active>1</active>
- </page2_3_3_1>
- <page2_3_3_2>
- <label>Seite 2.3.3.2</label>
- <uri>page2/page2_3/page2_3_3/2</uri>
- <resource>guest</resource>
- <active>1</active>
- </page2_3_3_2>
- </pages>
- </page2_3_3>
- </pages>
- </page2_3>
- </pages>
- </page2>
- <page3>
- <label>Seite 3</label>
- <uri>page3</uri>
- <pages>
- <page3_1>
- <label>Seite 3.1</label>
- <uri>page3/page3_1</uri>
- <resource>guest</resource>
- </page3_1>
- <page3_2>
- <label>Seite 3.2</label>
- <uri>page3/page3_2</uri>
- <resource>member</resource>
- <pages>
- <page3_2_1>
- <label>Seite 3.2.1</label>
- <uri>page3/page3_2/page3_2_1</uri>
- </page3_2_1>
- <page3_2_2>
- <label>Seite 3.2.2</label>
- <uri>page3/page3_2/page3_2_2</uri>
- <resource>admin</resource>
- </page3_2_2>
- </pages>
- </page3_2>
- <page3_3>
- <label>Seite 3.3</label>
- <uri>page3/page3_3</uri>
- <resource>special</resource>
- <pages>
- <page3_3_1>
- <label>Seite 3.3.1</label>
- <uri>page3/page3_3/page3_3_1</uri>
- <visible>0</visible>
- </page3_3_1>
- <page3_3_2>
- <label>Seite 3.3.2</label>
- <uri>page3/page3_3/page3_3_2</uri>
- <resource>admin</resource>
- </page3_3_2>
- </pages>
- </page3_3>
- </pages>
- </page3>
- <home>
- <label>Home</label>
- <order>-100</order>
- <module>default</module>
- <controller>index</controller>
- <action>index</action>
- </home>
- </nav>
- </config>
- */
- $config = new Zend_Config_Xml('/path/to/navigation.xml', 'nav');
- $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
- // Container erstellen
- $container = new Zend_Navigation();
- // Seite durch die Angabe eine Instanz einer Page hinzufügen
- 'uri' => 'http://www.example.com/'
- )))
- // Seite durch die Angabe eines Arrays hinzufügen
- 'uri' => 'http://www.example.com/'
- )))
- // Seite durch die Angabe eines Config Objekts hinzufügen
- 'uri' => 'http://www.example.com/'
- )))
- 'label' => 'Speichern'
- 'action' => 'save',
- ),
- 'label' => 'Löschen',
- 'action' => 'delete'
- )
- );
- // Zwei Seiten hinzufügen
- $container->addPages($pages);
- // Bestehende Seite entfernen und die gegebenen Seiten hinzufügen
- $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
- 'label' => 'Seite 1',
- 'action' => 'page1'
- ),
- 'label' => 'Seite 2',
- 'action' => 'page2',
- 'order' => 200
- ),
- 'label' => 'Seite 3',
- 'action' => 'page3'
- )
- ));
- // Entfernt eine Seite implizit durch die Reihenfolge der Seite
- $container->removePage(0); // Entfernt Seite 1
- // Entfernt eine Seite durch die Instanz
- $page3 = $container->findOneByAction('Seite 3');
- $container->removePage($page3); // Entfernt Seite 3
- // Entfernt eine Seite durch explizite angabe der Reihenfolge der Seite
- $container->removePage(200); // Entfernt Seite 2
- // Entfernt alle Seiten
- $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
- 'label' => 'Seite 1',
- 'uri' => 'page-1',
- 'foo' => 'bar',
- 'label' => 'Seite 1.1',
- 'uri' => 'page-1.1',
- 'foo' => 'bar',
- ),
- 'label' => 'Seite 1.2',
- 'uri' => 'page-1.2',
- 'class' => 'my-class',
- ),
- 'type' => 'uri',
- 'label' => 'Seite 1.3',
- 'uri' => 'page-1.3',
- 'action' => 'about'
- )
- )
- ),
- 'label' => 'Seite 2',
- 'id' => 'page_2_and_3',
- 'class' => 'my-class',
- 'module' => 'page2',
- 'controller' => 'index',
- 'action' => 'page1'
- ),
- 'label' => 'Seite 3',
- 'id' => 'page_2_and_3',
- 'module' => 'page3',
- 'controller' => 'index'
- )
- ));
- // Die 'id' muß nicht eindeutig sein, aber man sollte darauf achten das wenn
- // man zwei Seiten mit der gleichen Id hat, diese die gleichen Id Attribute in
- // Menüs und Breadcrumbs darstellen werden
- $found = $container->findBy('id',
- 'page_2_and_3'); // Gibt Seite 2 zurück
- $found = $container->findOneBy('id',
- 'page_2_and_3'); // Gibt Seite 2 zurück
- $found = $container->findBy('id',
- 'page_2_and_3',
- true); // Gibt Seite 2 und 3 zurück
- $found = $container->findById('page_2_and_3'); // Gibt Seite 2 zurück
- $found = $container->findOneById('page_2_and_3'); // Gibt Seite 2 zurück
- $found = $container->findAllById('page_2_and_3'); // Gibt Seite 2 und 3 zurück
- // Finde alle zu my-class passenden CSS Klassen
- $found = $container->findAllBy('class',
- 'my-class'); // Gibt Seite 1.2 und 2 zurück
- $found = $container->findAllByClass('my-class'); // Gibt Seite 1.2 und 2 zurück
- // Finde die erste zu my-class passende CSS Klasse
- $found = $container->findOneByClass('my-class'); // Gibt Seite 1.2 zurück
- // Findet alle zu non-existant passenden CSS Klassen
- $found = $container->findAllByClass('non-existant'); // Gibt array() zurück
- // Findet die erste zu non-existant passende CSS Klasse
- $found = $container->findOneByClass('non-existant'); // Gibt null zurück
- // Findet alle Seiten mit den eigenen Eigenschaften 'foo' = 'bar'
- $found = $container->findAllBy('foo', 'bar'); // Gibt Seite 1 und 1.1 zurück
- // Um das gleiche auf Magische Weise zu ermöglichen, muß 'foo' kleingeschrieben
- // sein weil 'foo' eine eigene Eigenschaft ist und deshalb der Name der
- // Eigenschaft nicht zu 'Foo' normalisiert wird
- $found = $container->findAllByfoo('bar');
- // Findet alle mit controller = 'index'
- $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
- /*
- * Erstellt einen Container von einem Array
- */
- 'label' => 'Seite 1',
- 'uri' => '#'
- ),
- 'label' => 'Seite 2',
- 'uri' => '#',
- 'label' => 'Seite 2.1',
- 'uri' => '#'
- ),
- 'label' => 'Seite 2.2',
- 'uri' => '#'
- )
- )
- )
- 'label' => 'Seite 3',
- 'uri' => '#'
- )
- ));
- // Durchsucht flach indem ein normales foreach verwendet wird:
- // Ausgabe: Seite 1, Seite 2, Seite 3
- foreach ($container as $page) {
- }
- // Durchsucht rekursiv indem RecursiveIteratorIterator verwendet wird
- $it = new RecursiveIteratorIterator(
- $container, RecursiveIteratorIterator::SELF_FIRST);
- // Ausgabe: Seite 1, Seite 2, Seite 2.1, Seite 2.2, Seite 3
- foreach ($it as $page) {
- }
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
- 'label' => 'Seite 1',
- 'uri' => '#'
- ),
- 'label' => 'Seite 2',
- 'uri' => '#',
- 'label' => 'Seite 2.1',
- 'uri' => '#'
- ),
- 'label' => 'Seite 2.2',
- 'uri' => '#'
- )
- )
- )
- ));
- /* Ausgabe:
- array(2) {
- [0]=> array(15) {
- ["label"]=> string(6) "Seite 1"
- ["id"]=> NULL
- ["class"]=> NULL
- ["title"]=> NULL
- ["target"]=> NULL
- ["rel"]=> array(0) {
- }
- ["rev"]=> array(0) {
- }
- ["order"]=> NULL
- ["resource"]=> NULL
- ["privilege"]=> NULL
- ["active"]=> bool(false)
- ["visible"]=> bool(true)
- ["type"]=> string(23) "Zend_Navigation_Page_Uri"
- ["pages"]=> array(0) {
- }
- ["uri"]=> string(1) "#"
- }
- [1]=> array(15) {
- ["label"]=> string(6) "Seite 2"
- ["id"]=> NULL
- ["class"]=> NULL
- ["title"]=> NULL
- ["target"]=> NULL
- ["rel"]=> array(0) {
- }
- ["rev"]=> array(0) {
- }
- ["order"]=> NULL
- ["resource"]=> NULL
- ["privilege"]=> NULL
- ["active"]=> bool(false)
- ["visible"]=> bool(true)
- ["type"]=> string(23) "Zend_Navigation_Page_Uri"
- ["pages"]=> array(2) {
- [0]=> array(15) {
- ["label"]=> string(8) "Seite 2.1"
- ["id"]=> NULL
- ["class"]=> NULL
- ["title"]=> NULL
- ["target"]=> NULL
- ["rel"]=> array(0) {
- }
- ["rev"]=> array(0) {
- }
- ["order"]=> NULL
- ["resource"]=> NULL
- ["privilege"]=> NULL
- ["active"]=> bool(false)
- ["visible"]=> bool(true)
- ["type"]=> string(23) "Zend_Navigation_Page_Uri"
- ["pages"]=> array(0) {
- }
- ["uri"]=> string(1) "#"
- }
- [1]=>
- array(15) {
- ["label"]=> string(8) "Seite 2.2"
- ["id"]=> NULL
- ["class"]=> NULL
- ["title"]=> NULL
- ["target"]=> NULL
- ["rel"]=> array(0) {
- }
- ["rev"]=> array(0) {
- }
- ["order"]=> NULL
- ["resource"]=> NULL
- ["privilege"]=> NULL
- ["active"]=> bool(false)
- ["visible"]=> bool(true)
- ["type"]=> string(23) "Zend_Navigation_Page_Uri"
- ["pages"]=> array(0) {
- }
- ["uri"]=> string(1) "#"
- }
- }
- ["uri"]=> string(1) "#"
- }
- }
- */
| Seiten |
+ 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.

Comments
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!
---and post questions in the forum---
http://forums.zend.com/
*** 21,23 ****
)
! )
array(
--- 21,23 ----
)
! ),
array(
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!
<?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) > 0 ? $pages : false);
}
/**
* Puts the pertinent link information needed to create a Zend_Nav..._Page
* within an array.
* @param object $link
* @return array
*/
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;
}
/**
* Injects the front acl into the Zend_Navigation object passed in
* @param Zend_Navigation $nav
* @return Zend_Navigation
*/
private function _injectAcl( Zend_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;
}
}
The
setOnlyActiveBranch((bool)$var)doesn't work for me. If you leave the line out the class works fine. Kudos.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
"Example #2 Creating a container using a config object" is not correct. You do not need the nav node.
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...
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.