PhpRiot
Follow phpriot on Twitter
Sponsored Link
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store
Free iPad/iPhone App
Available on the App Store

  • PHP manual
  • Zend Framework manual
  • Smarty manual
  • PHP articles
  • PHP training

Creating containers

Zend_Navigation_Container is abstract, and can not be instantiated directly. Use Zend_Navigation if you want to instantiate a container.

Zend_Navigation can be constructed entirely empty, or take an array or a Zend_Config object with pages to put in the container. Each page in the given array/config will eventually be passed to the addPage() method of the container class, which means that each element in the array/config can be an array or a config object, or a Zend_Navigation_Page instance.

Example 615. Creating a container using an array

<?php
/*
 * Create a container from an array
 *
 * Each element in the array will be passed to
 * Zend_Navigation_Page::factory() when constructing.
 */
$container = new Zend_Navigation(array(
    array(
        
'label' => 'Page 1',
        
'id' => 'home-link',
        
'uri' => '/'
    
),
    array(
        
'label' => 'Zend',
        
'uri' => 'http://www.zend-project.com/',
        
'order' => 100
    
),
    array(
        
'label' => 'Page 2',
        
'controller' => 'page2',
        
'pages' => array(
            array(
                
'label' => 'Page 2.1',
                
'action' => 'page2_1',
                
'controller' => 'page2',
                
'class' => 'special-one',
                
'title' => 'This element has a special class',
                
'active' => true
            
),
            array(
                
'label' => 'Page 2.2',
                
'action' => 'page2_2',
                
'controller' => 'page2',
                
'class' => 'special-two',
                
'title' => 'This element has a special class too'
            
)
        )
    ),
    array(
        
'label' => 'Page 2 with params',
        
'action' => 'index',
        
'controller' => 'page2',
        
// specify a param or two
        
'params' => array(
            
'format' => 'json',
            
'foo' => 'bar'
        
)
    ),
    array(
        
'label' => 'Page 2 with params and a route',
        
'action' => 'index',
        
'controller' => 'page2',
        
// specify a route name and a param for the route
        
'route' => 'nav-route-example',
        
'params' => array(
            
'format' => 'json'
        
)
    ),
    array(
        
'label' => 'Page 3',
        
'action' => 'index',
        
'controller' => 'index',
        
'module' => 'mymodule',
        
'reset_params' => false
    
),
    array(
        
'label' => 'Page 4',
        
'uri' => '#',
        
'pages' => array(
            array(
                
'label' => 'Page 4.1',
                
'uri' => '/page4',
                
'title' => 'Page 4 using uri',
                
'pages' => array(
                    array(
                        
'label' => 'Page 4.1.1',
                        
'title' => 'Page 4 using mvc params',
                        
'action' => 'index',
                        
'controller' => 'page4',
                        
// let's say this page is active
                        
'active' => '1'
                    
)
                )
            )
        )
    ),
    array(
        
'label' => 'Page 0?',
        
'uri' => '/setting/the/order/option',
        
// setting order to -1 should make it appear first
        
'order' => -1
    
),
    array(
        
'label' => 'Page 5',
        
'uri' => '/',
        
// this page should not be visible
        
'visible' => false,
        
'pages' => array(
            array(
                
'label' => 'Page 5.1',
                
'uri' => '#',
                
'pages' => array(
                    array(
                        
'label' => 'Page 5.1.1',
                        
'uri' => '#',
                        
'pages' => array(
                            array(
                                
'label' => 'Page 5.1.2',
                                
'uri' => '#',
                                
// let's say this page is active
                                
'active' => true
                            
)
                        )
                    )
                )
            )
        )
    ),
    array(
        
'label' => 'ACL page 1 (guest)',
        
'uri' => '#acl-guest',
        
'resource' => 'nav-guest',
        
'pages' => array(
            array(
                
'label' => 'ACL page 1.1 (foo)',
                
'uri' => '#acl-foo',
                
'resource' => 'nav-foo'
            
),
            array(
                
'label' => 'ACL page 1.2 (bar)',
                
'uri' => '#acl-bar',
                
'resource' => 'nav-bar'
            
),
            array(
                
'label' => 'ACL page 1.3 (baz)',
                
'uri' => '#acl-baz',
                
'resource' => 'nav-baz'
            
),
            array(
                
'label' => 'ACL page 1.4 (bat)',
                
'uri' => '#acl-bat',
                
'resource' => 'nav-bat'
            
)
        )
    ),
    array(
        
'label' => 'ACL page 2 (member)',
        
'uri' => '#acl-member',
        
'resource' => 'nav-member'
    
),
    array(
        
'label' => 'ACL page 3 (admin',
        
'uri' => '#acl-admin',
        
'resource' => 'nav-admin',
        
'pages' => array(
            array(
                
'label' => 'ACL page 3.1 (nothing)',
                
'uri' => '#acl-nada'
            
)
        )
    ),
    array(
        
'label' => 'Zend Framework',
        
'route' => 'zf-route'
    
)
));

Example 616. Creating a container using a config object

<?php
/* CONTENTS OF /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>Page 1</label>
            <uri>page1</uri>
            <pages>

                <page1_1>
                    <label>Page 1.1</label>
                    <uri>page1/page1_1</uri>
                </page1_1>

            </pages>
        </page1>

        <page2>
            <label>Page 2</label>
            <uri>page2</uri>
            <pages>

                <page2_1>
                    <label>Page 2.1</label>
                    <uri>page2/page2_1</uri>
                </page2_1>

                <page2_2>
                    <label>Page 2.2</label>
                    <uri>page2/page2_2</uri>
                    <pages>

                        <page2_2_1>
                            <label>Page 2.2.1</label>
                            <uri>page2/page2_2/page2_2_1</uri>
                        </page2_2_1>

                        <page2_2_2>
                            <label>Page 2.2.2</label>
                            <uri>page2/page2_2/page2_2_2</uri>
                            <active>1</active>
                        </page2_2_2>

                    </pages>
                </page2_2>

                <page2_3>
                    <label>Page 2.3</label>
                    <uri>page2/page2_3</uri>
                    <pages>

                        <page2_3_1>
                            <label>Page 2.3.1</label>
                            <uri>page2/page2_3/page2_3_1</uri>
                        </page2_3_1>

                        <page2_3_2>
                            <label>Page 2.3.2</label>
                            <uri>page2/page2_3/page2_3_2</uri>
                            <visible>0</visible>
                            <pages>

                                    <page2_3_2_1>
                                        <label>Page 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>Page 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>Ignore</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>Page 2.3.3</label>
                            <uri>page2/page2_3/page2_3_3</uri>
                            <resource>admin</resource>
                            <pages>

                                    <page2_3_3_1>
                                        <label>Page 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>Page 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>Page 3</label>
            <uri>page3</uri>
            <pages>

                <page3_1>
                    <label>Page 3.1</label>
                    <uri>page3/page3_1</uri>
                    <resource>guest</resource>
                </page3_1>

                <page3_2>
                    <label>Page 3.2</label>
                    <uri>page3/page3_2</uri>
                    <resource>member</resource>
                    <pages>

                        <page3_2_1>
                            <label>Page 3.2.1</label>
                            <uri>page3/page3_2/page3_2_1</uri>
                        </page3_2_1>

                        <page3_2_2>
                            <label>Page 3.2.2</label>
                            <uri>page3/page3_2/page3_2_2</uri>
                            <resource>admin</resource>
                        </page3_2_2>

                    </pages>
                </page3_2>

                <page3_3>
                    <label>Page 3.3</label>
                    <uri>page3/page3_3</uri>
                    <resource>special</resource>
                    <pages>

                        <page3_3_1>
                            <label>Page 3.3.1</label>
                            <uri>page3/page3_3/page3_3_1</uri>
                            <visible>0</visible>
                        </page3_3_1>

                        <page3_3_2>
                            <label>Page 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);

Zend Framework