PhpRiot
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

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 633. 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 634. Creating a container using a config object

Example for INI:

[nav]
zend.label = "Zend"
zend.uri = "http://www.zend-project.com/"
zend.order = "100"

page1.label = "Page 1"
page1.uri = "page1"
page1.pages.page1_1.label = "Page 1.1"
page1.pages.page1_1.uri = "page1/page1_1"

page2.label = "Page 2"
page2.uri = "page2"
page2.pages.page2_1.label = "Page 2.1"
page2.pages.page2_1.uri = "page2/page2_1"
page2.pages.page2_2.label = "Page 2.2"
page2.pages.page2_2.uri = "page2/page2_2"
page2.pages.page2_2.pages.page2_2_1.label = "Page 2.2.1"
page2.pages.page2_2.pages.page2_2_1.uri = "page2/page2_2/page2_2_1"
page2.pages.page2_2.pages.page2_2_2.label = "Page 2.2.2"
page2.pages.page2_2.pages.page2_2_2.uri = "page2/page2_2/page2_2_2"
page2.pages.page2_2.pages.page2_2_2.active = "1"
page2.pages.page2_3.label = "Page 2.3"
page2.pages.page2_3.uri = "page2/page2_3"
page2.pages.page2_3.pages.page2_3_1.label = "Page 2.3.1"
page2.pages.page2_3.pages.page2_3_1.uri = "page2/page2_3/page2_3_1"
page2.pages.page2_3.pages.page2_3_2.label = "Page 2.3.2"
page2.pages.page2_3.pages.page2_3_2.uri = "page2/page2_3/page2_3_2"
page2.pages.page2_3.pages.page2_3_2.visible = "0"
page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_1.label = "Page 2.3.2.1"
page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_1.uri = "page2/page2_3/page2_3_2/1"
page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_1.active = "1"
page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_2.label = "Page 2.3.2.2"
page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_2.uri = "page2/page2_3/page2_3_2/2"
page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_2.active = "1"
page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_2.pages.page_2_3_2_2_1.label = "Ignore"
page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_2.pages.page_2_3_2_2_1.uri = "#"
page2.pages.page2_3.pages.page2_3_2.pages.page2_3_2_2.pages.page_2_3_2_2_1.active = "1"
page2.pages.page2_3.pages.page2_3_3.label = "Page 2.3.3"
page2.pages.page2_3.pages.page2_3_3.uri = "page2/page2_3/page2_3_3"
page2.pages.page2_3.pages.page2_3_3.resource = "admin"
page2.pages.page2_3.pages.page2_3_3.pages.page2_3_3_1.label = "Page 2.3.3.1"
page2.pages.page2_3.pages.page2_3_3.pages.page2_3_3_1.uri = "page2/page2_3/page2_3_3/1"
page2.pages.page2_3.pages.page2_3_3.pages.page2_3_3_1.active = "1"
page2.pages.page2_3.pages.page2_3_3.pages.page2_3_3_2.label = "Page 2.3.3.2"
page2.pages.page2_3.pages.page2_3_3.pages.page2_3_3_2.uri = "page2/page2_3/page2_3_3/2"
page2.pages.page2_3.pages.page2_3_3.pages.page2_3_3_2.resource = "guest"
page2.pages.page2_3.pages.page2_3_3.pages.page2_3_3_2.active = "1"

page3.label = "Page 3"
page3.uri = "page3"
page3.pages.page3_1.label = "Page 3.1"
page3.pages.page3_1.uri = "page3/page3_1"
page3.pages.page3_1.resource = "guest"
page3.pages.page3_2.label = "Page 3.2"
page3.pages.page3_2.uri = "page3/page3_2"
page3.pages.page3_2.resource = "member"
page3.pages.page3_2.pages.page3_2_1.label = "Page 3.2.1"
page3.pages.page3_2.pages.page3_2_1.uri = "page3/page3_2/page3_2_1"
page3.pages.page3_2.pages.page3_2_2.label = "Page 3.2.2"
page3.pages.page3_2.pages.page3_2_2.uri = "page3/page3_2/page3_2_2"
page3.pages.page3_2.pages.page3_2_2.resource = "admin"
page3.pages.page3_3.label = "Page 3.3"
page3.pages.page3_3.uri = "page3/page3_3"
page3.pages.page3_3.resource = "special"
page3.pages.page3_3.pages.page3_3_1.label = "Page 3.3.1"
page3.pages.page3_3.pages.page3_3_1.uri = "page3/page3_3/page3_3_1"
page3.pages.page3_3.pages.page3_3_1.visible = "0"
page3.pages.page3_3.pages.page3_3_2.label = "Page 3.3.2"
page3.pages.page3_3.pages.page3_3_2.uri = "page3/page3_3/page3_3_2"
page3.pages.page3_3.pages.page3_3_2.resource = "admin"

home.label = "Home"
home.order = "-100"
home.module = "default"
home.controller = "index"
home.action = "index"
<?php
$config    
= new Zend_Config_Ini('/path/to/navigation.ini''nav');
$container = new Zend_Navigation($config);

Example for JSON:

{
  "nav": {
    "zend": {
      "label": "Zend",
      "uri": "http:\/\/www.zend-project.com\/",
      "order": "100"
    },
    "page1": {
      "label": "Page 1",
      "uri": "page1",
      "pages": {
        "page1_1": {
          "label": "Page 1.1",
          "uri": "page1\/page1_1"
        }
      }
    },
    "page2": {
      "label": "Page 2",
      "uri": "page2",
      "pages": {
        "page2_1": {
          "label": "Page 2.1",
          "uri": "page2\/page2_1"
        },
        "page2_2": {
          "label": "Page 2.2",
          "uri": "page2\/page2_2",
          "pages": {
            "page2_2_1": {
              "label": "Page 2.2.1",
              "uri": "page2\/page2_2\/page2_2_1"
            },
            "page2_2_2": {
              "label": "Page 2.2.2",
              "uri": "page2\/page2_2\/page2_2_2",
              "active": "1"
            }
          }
        },
        "page2_3": {
          "label": "Page 2.3",
          "uri": "page2\/page2_3",
          "pages": {
            "page2_3_1": {
              "label": "Page 2.3.1",
              "uri": "page2\/page2_3\/page2_3_1"
            },
            "page2_3_2": {
              "label": "Page 2.3.2",
              "uri": "page2\/page2_3\/page2_3_2",
              "visible": "0",
              "pages": {
                "page2_3_2_1": {
                  "label": "Page 2.3.2.1",
                  "uri": "page2\/page2_3\/page2_3_2\/1",
                  "active": "1"
                },
                "page2_3_2_2": {
                  "label": "Page 2.3.2.2",
                  "uri": "page2\/page2_3\/page2_3_2\/2",
                  "active": "1",
                  "pages": {
                    "page_2_3_2_2_1": {
                      "label": "Ignore",
                      "uri": "#",
                      "active": "1"
                    }
                  }
                }
              }
            },
            "page2_3_3": {
              "label": "Page 2.3.3",
              "uri": "page2\/page2_3\/page2_3_3",
              "resource": "admin",
              "pages": {
                "page2_3_3_1": {
                  "label": "Page 2.3.3.1",
                  "uri": "page2\/page2_3\/page2_3_3\/1",
                  "active": "1"
                },
                "page2_3_3_2": {
                  "label": "Page 2.3.3.2",
                  "uri": "page2\/page2_3\/page2_3_3\/2",
                  "resource": "guest",
                  "active": "1"
                }
              }
            }
          }
        }
      }
    },
    "page3": {
      "label": "Page 3",
      "uri": "page3",
      "pages": {
        "page3_1": {
          "label": "Page 3.1",
          "uri": "page3\/page3_1",
          "resource": "guest"
        },
        "page3_2": {
          "label": "Page 3.2",
          "uri": "page3\/page3_2",
          "resource": "member",
          "pages": {
            "page3_2_1": {
              "label": "Page 3.2.1",
              "uri": "page3\/page3_2\/page3_2_1"
            },
            "page3_2_2": {
              "label": "Page 3.2.2",
              "uri": "page3\/page3_2\/page3_2_2",
              "resource": "admin"
            }
          }
        },
        "page3_3": {
          "label": "Page 3.3",
          "uri": "page3\/page3_3",
          "resource": "special",
          "pages": {
            "page3_3_1": {
              "label": "Page 3.3.1",
              "uri": "page3\/page3_3\/page3_3_1",
              "visible": "0"
            },
            "page3_3_2": {
              "label": "Page 3.3.2",
              "uri": "page3\/page3_3\/page3_3_2",
              "resource": "admin"
            }
          }
        }
      }
    },
    "home": {
      "label": "Home",
      "order": "-100",
      "module": "default",
      "controller": "index",
      "action": "index"
    }
  }
}
<?php
$config    
= new Zend_Config_Json('/path/to/navigation.json''nav');
$container = new Zend_Navigation($config);

Example for 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>
<?php
$config    
= new Zend_Config_Xml('/path/to/navigation.xml''nav');
$container = new Zend_Navigation($config);

Example for YAML:

nav: 
  zend: 
    label: Zend
    uri: http://www.zend-project.com/
    order: 100
  page1: 
    label: Page 1
    uri: page1
    pages: 
      page1_1: 
        label: Page 1.1
        uri: page1/page1_1
  page2: 
    label: Page 2
    uri: page2
    pages: 
      page2_1: 
        label: Page 2.1
        uri: page2/page2_1
      page2_2: 
        label: Page 2.2
        uri: page2/page2_2
        pages: 
          page2_2_1: 
            label: Page 2.2.1
            uri: page2/page2_2/page2_2_1
          page2_2_2: 
            label: Page 2.2.2
            uri: page2/page2_2/page2_2_2
            active: 1
      page2_3: 
        label: Page 2.3
        uri: page2/page2_3
        pages: 
          page2_3_1: 
            label: Page 2.3.1
            uri: page2/page2_3/page2_3_1
          page2_3_2: 
            label: Page 2.3.2
            uri: page2/page2_3/page2_3_2
            visible: 0
            pages: 
              page2_3_2_1: 
                label: Page 2.3.2.1
                uri: page2/page2_3/page2_3_2/1
                active: 1
              page2_3_2_2: 
                label: Page 2.3.2.2
                uri: page2/page2_3/page2_3_2/2
                active: 1
                pages: 
                  page_2_3_2_2_1: 
                    label: Ignore
                    uri: #
                    active: 1
          page2_3_3: 
            label: Page 2.3.3
            uri: page2/page2_3/page2_3_3
            resource: admin
            pages: 
              page2_3_3_1: 
                label: Page 2.3.3.1
                uri: page2/page2_3/page2_3_3/1
                active: 1
              page2_3_3_2: 
                label: Page 2.3.3.2
                uri: page2/page2_3/page2_3_3/2
                resource: guest
                active: 1
  page3: 
    label: Page 3
    uri: page3
    pages: 
      page3_1: 
        label: Page 3.1
        uri: page3/page3_1
        resource: guest
      page3_2: 
        label: Page 3.2
        uri: page3/page3_2
        resource: member
        pages: 
          page3_2_1: 
            label: Page 3.2.1
            uri: page3/page3_2/page3_2_1
          page3_2_2: 
            label: Page 3.2.2
            uri: page3/page3_2/page3_2_2
            resource: admin
      page3_3: 
        label: Page 3.3
        uri: page3/page3_3
        resource: special
        pages: 
          page3_3_1: 
            label: Page 3.3.1
            uri: page3/page3_3/page3_3_1
            visible: 0
          page3_3_2: 
            label: Page 3.3.2
            uri: page3/page3_3/page3_3_2
            resource: admin
  home: 
    label: Home
    order: -100
    module: default
    controller: index
    action: index
<?php
$config    
= new Zend_Config_Yaml'/path/to/navigation.yaml''nav');
$container = new Zend_Navigation($config);

Zend Framework