Introduction

Pages

Zend_Navigation ships with two page types:

  • MVC pages – using the class Zend_Navigation_Page_Mvc

  • URI pages – using the class Zend_Navigation_Page_Uri

MVC pages are link to on-site web pages, and are defined using MVC parameters (action, controller, module, route, params). URI pages are defined by a single property uri, which give you the full flexibility to link off-site pages or do other things with the generated links (e.g. an URI that turns into <a href="#">foo<a>).

Common page features

All page classes must extend Zend_Navigation_Page, and will thus share a common set of features and properties. Most notably they share the options in the table below and the same initialization process.

Option keys are mapped to set methods. This means that the option order maps to the method setOrder(), and reset_params maps to the method setResetParams(). If there is no setter method for the option, it will be set as a custom property of the page.

Read more on extending Zend_Navigation_Page in Creating custom page types.

Common page options
Key Type Default Description
label String NULL A page label, such as 'Home' or 'Blog'.
id String | int NULL An id tag/attribute that may be used when rendering the page, typically in an anchor element.
class String NULL A CSS class that may be used when rendering the page, typically in an anchor element.
title String NULL A short page description, typically for using as the title attribute in an anchor.
target String NULL Specifies a target that may be used for the page, typically in an anchor element.
accesskey String NULL This attribute assigns an access key to an A element. An access key is a single character from the document character set.
fragment String NULL The fragment identifier (anchor identifier) pointing to an anchor within a resource that is subordinate to another, primary resource. The fragment identifier introduced by a hash mark '#'. Example: http://www.example.org/foo.html#bar ('bar' is the fragment identifier)
rel Array array() Specifies forward relations for the page. Each element in the array is a key-value pair, where the key designates the relation/link type, and the value is a pointer to the linked page. An example of a key-value pair is 'alternate' => 'format/plain.html'. To allow full flexbility, there are no restrictions on relation values. The value does not have to be a string. Read more about rel and rev in the section on the Links helper..
rev Array array() Specifies reverse relations for the page. Works exactly like rel.
order String | int | NULL NULL Works like order for elements in Zend_Form. If specified, the page will be iterated in a specific order, meaning you can force a page to be iterated before others by setting the order attribute to a low number, e.g. -100. If a String is given, it must parse to a valid int. If NULL is given, it will be reset, meaning the order in which the page was added to the container will be used.
resource String | Zend_Acl_Resource_Interface | NULL NULL ACL resource to associate with the page. Read more in the section on ACL integration in view helpers..
privilege String | NULL NULL ACL privilege to associate with the page. Read more in the section on ACL integration in view helpers..
active bool FALSE Whether the page should be considered active for the current request. If active is FALSE or not given, MVC pages will check its properties against the request object upon calling $page->isActive().
visible bool TRUE Whether page should be visible for the user, or just be a part of the structure. Invisible pages are skipped by view helpers.
pages Array | Zend_Config | NULL NULL Child pages of the page. This could be an Array or Zend_Config object containing either page options that can be passed to the factory() method, or actual Zend_Navigation_Page instances, or a mixture of both.

Note: Custom properties
All pages support setting and getting of custom properties by use of the magic methods __set($name, $value), __get($name), __isset($name) and __unset($name). Custom properties may have any value, and will be included in the array that is returned from $page->toArray(), which means that pages can be serialized/deserialized successfully even if the pages contains properties that are not native in the page class.
Both native and custom properties can be set using $page->set($name, $value) and retrieved using $page->get($name), or by using magic methods.

Example #1 Custom page properties

This example shows how custom properties can be used.

  1. $page = new Zend_Navigation_Page_Mvc();
  2. $page->foo = 'bar';
  3. $page->meaning = 42;
  4.  
  5. echo $page->foo;
  6.  
  7. if ($page->meaning != 42) {
  8.     // action should be taken
  9. }

Zend_Navigation_Page_Mvc

MVC pages are defined using MVC parameters known from the Zend_Controller component. An MVC page will use Zend_Controller_Action_Helper_Url internally in the getHref() method to generate hrefs, and the isActive() method will intersect the Zend_Controller_Request_Abstract params with the page's params to determine if the page is active.

MVC page options
Key Type Default Description
action String NULL Action name to use when generating href to the page.
controller String NULL Controller name to use when generating href to the page.
module String NULL Module name to use when generating href to the page.
params Array array() User params to use when generating href to the page.
route String NULL Route name to use when generating href to the page.
reset_params bool TRUE Whether user params should be reset when generating href to the page.
encode_url bool TRUE Whether href should be encoded when assembling URL.

Note: The three examples below assume a default MVC setup with the default route in place.
The URI returned is relative to the baseUrl in Zend_Controller_Front. In the examples, the baseUrl is '/' for simplicity.

Example #2 getHref() generates the page URI

This example shows that MVC pages use Zend_Controller_Action_Helper_Url internally to generate URIs when calling $page->getHref().

  1. // getHref() returns /
  2. $page = new Zend_Navigation_Page_Mvc(array(
  3.     'action'     => 'index',
  4.     'controller' => 'index'
  5. ));
  6.  
  7. // getHref() returns /blog/post/view
  8. $page = new Zend_Navigation_Page_Mvc(array(
  9.     'action'     => 'view',
  10.     'controller' => 'post',
  11.     'module'     => 'blog'
  12. ));
  13.  
  14. // getHref() returns /blog/post/view/id/1337
  15. $page = new Zend_Navigation_Page_Mvc(array(
  16.     'action'     => 'view',
  17.     'controller' => 'post',
  18.     'module'     => 'blog',
  19.     'params'     => array('id' => 1337)
  20. ));

Example #3 isActive() determines if page is active

This example shows that MVC pages determine whether they are active by using the params found in the request object.

  1. /*
  2. * Dispatched request:
  3. * - module:     default
  4. * - controller: index
  5. * - action:     index
  6. */
  7. $page1 = new Zend_Navigation_Page_Mvc(array(
  8.     'action'     => 'index',
  9.     'controller' => 'index'
  10. ));
  11.  
  12. $page2 = new Zend_Navigation_Page_Mvc(array(
  13.     'action'     => 'bar',
  14.     'controller' => 'index'
  15. ));
  16.  
  17. $page1->isActive(); // returns true
  18. $page2->isActive(); // returns false
  19.  
  20. /*
  21. * Dispatched request:
  22. * - module:     blog
  23. * - controller: post
  24. * - action:     view
  25. * - id:         1337
  26. */
  27. $page = new Zend_Navigation_Page_Mvc(array(
  28.     'action'     => 'view',
  29.     'controller' => 'post',
  30.     'module'     => 'blog'
  31. ));
  32.  
  33. // returns true, because request has the same module, controller and action
  34. $page->isActive();
  35.  
  36. /*
  37. * Dispatched request:
  38. * - module:     blog
  39. * - controller: post
  40. * - action:     view
  41. */
  42. $page = new Zend_Navigation_Page_Mvc(array(
  43.     'action'     => 'view',
  44.     'controller' => 'post',
  45.     'module'     => 'blog',
  46.     'params'     => array('id' => null)
  47. ));
  48.  
  49. // returns false, because page requires the id param to be set in the request
  50. $page->isActive(); // returns false

Example #4 Using routes

Routes can be used with MVC pages. If a page has a route, this route will be used in getHref() to generate the URL for the page.

Note: Note that when using the route property in a page, you should also specify the default params that the route defines (module, controller, action, etc.), otherwise the isActive() method will not be able to determine if the page is active. The reason for this is that there is currently no way to get the default params from a Zend_Controller_Router_Route_Interface object, nor to retrieve the current route from a Zend_Controller_Router_Interface object.

  1. // the following route is added to the ZF router
  2. Zend_Controller_Front::getInstance()->getRouter()->addRoute(
  3.     'article_view', // route name
  4.     new Zend_Controller_Router_Route(
  5.         'a/:id',
  6.         array(
  7.             'module'     => 'news',
  8.             'controller' => 'article',
  9.             'action'     => 'view',
  10.             'id'         => null
  11.         )
  12.     )
  13. );
  14.  
  15. // a page is created with a 'route' option
  16. $page = new Zend_Navigation_Page_Mvc(array(
  17.     'label'      => 'A news article',
  18.     'route'      => 'article_view',
  19.     'module'     => 'news',    // required for isActive(), see note above
  20.     'controller' => 'article', // required for isActive(), see note above
  21.     'action'     => 'view',    // required for isActive(), see note above
  22.     'params'     => array('id' => 42)
  23. ));
  24.  
  25. // returns: /a/42
  26. $page->getHref();

Example #5 Set parameters to use when assembling URL

  1. // The following route is added to the ZF router
  2. Zend_Controller_Front::getInstance()->getRouter()->addRoute(
  3.     'article_list', // route name
  4.     new Zend_Controller_Router_Route(
  5.         'blog/:category/:page',
  6.         array(
  7.             'module'     => 'blog',
  8.             'controller' => 'article',
  9.             'action'     => 'list',
  10.             'category'   => null,
  11.             'page'       => null,
  12.         )
  13.     )
  14. );
  15.  
  16. // A page is created with the 'route' option
  17. $page = new Zend_Navigation_Page_Mvc(array(
  18.     'label'      => 'Article list',
  19.     'module'     => 'blog',
  20.     'controller' => 'post',
  21.     'action'     => 'list',
  22. ));
  23.  
  24. // Add multiple parameters at once
  25. $page->addParams(
  26.     array(
  27.         'category' => 'news',
  28.         'page'     => 1,
  29.     )
  30. );
  31.  
  32. // Add a single parameter
  33. $page->addParam('category', 'news');
  34.  
  35. // Set multiple parameters at once (Overwrites any previously set parameters!)
  36. $page->setParams(
  37.     array(
  38.         'category' => 'news',
  39.         'page'     => 1,
  40.     )
  41. );
  42.  
  43. // Set a single parameter
  44. $page->setParam('category', 'news');
  45.  
  46. // Retrieve all parameters
  47. $params = $page->getParams();
  48.  
  49. // Retrieve a single parameter
  50. $category = $page->getParam('category');
  51.  
  52. // Remove a parameter
  53. $page->removeParam('page');
  54.  
  55. // Clear all parameters
  56. $page->clearParams();

Zend_Navigation_Page_Uri

Pages of type Zend_Navigation_Page_Uri can be used to link to pages on other domains or sites, or to implement custom logic for the page. URI pages are simple; in addition to the common page options, a URI page takes only one option — uri. The uri will be returned when calling $page->getHref(), and may be a String or NULL.

Note: Zend_Navigation_Page_Uri will not try to determine whether it should be active when calling $page->isActive(). It merely returns what currently is set, so to make a URI page active you have to manually call $page->setActive() or specifying active as a page option when constructing.

URI page options
Key Type Default Description
uri String NULL URI to page. This can be any string or NULL.

Creating custom page types

When extending Zend_Navigation_Page, there is usually no need to override the constructor or the methods setOptions() or setConfig(). The page constructor takes a single parameter, an Array or a Zend_Config object, which is passed to setOptions() or setConfig() respectively. Those methods will in turn call set() method, which will map options to native or custom properties. If the option internal_id is given, the method will first look for a method named setInternalId(), and pass the option to this method if it exists. If the method does not exist, the option will be set as a custom property of the page, and be accessible via $internalId = $page->internal_id; or $internalId = $page->get('internal_id');.

Example #6 The most simple custom page

The only thing a custom page class needs to implement is the getHref() method.

  1. class My_Simple_Page extends Zend_Navigation_Page
  2. {
  3.     public function getHref()
  4.     {
  5.         return 'something-completely-different';
  6.     }
  7. }

Example #7 A custom page with properties

When adding properties to an extended page, there is no need to override/modify setOptions() or setConfig().

  1. class My_Navigation_Page extends Zend_Navigation_Page
  2. {
  3.     private $_foo;
  4.     private $_fooBar;
  5.  
  6.     public function setFoo($foo)
  7.     {
  8.         $this->_foo = $foo;
  9.     }
  10.  
  11.     public function getFoo()
  12.     {
  13.         return $this->_foo;
  14.     }
  15.  
  16.     public function setFooBar($fooBar)
  17.     {
  18.         $this->_fooBar = $fooBar;
  19.     }
  20.  
  21.     public function getFooBar()
  22.     {
  23.         return $this->_fooBar;
  24.     }
  25.  
  26.     public function getHref()
  27.     {
  28.         return $this->foo . '/' . $this->fooBar;
  29.     }
  30. }
  31.  
  32. // can now construct using
  33. $page = new My_Navigation_Page(array(
  34.     'label'   => 'Property names are mapped to setters',
  35.     'foo'     => 'bar',
  36.     'foo_bar' => 'baz'
  37. ));
  38.  
  39. // ...or
  40. $page = Zend_Navigation_Page::factory(array(
  41.     'type'    => 'My_Navigation_Page',
  42.     'label'   => 'Property names are mapped to setters',
  43.     'foo'     => 'bar',
  44.     'foo_bar' => 'baz'
  45. ));

Creating pages using the page factory

All pages (also custom classes), can be created using the page factory, Zend_Navigation_Page::factory(). The factory can take an array with options, or a Zend_Config object. Each key in the array/config corresponds to a page option, as seen in the section on Pages. If the option uri is given and no MVC options are given (action, controller, module, route), an URI page will be created. If any of the MVC options are given, an MVC page will be created.

If type is given, the factory will assume the value to be the name of the class that should be created. If the value is mvc or uri and MVC/URI page will be created.

Example #8 Creating an MVC page using the page factory

  1. $page = Zend_Navigation_Page::factory(array(
  2.     'label'  => 'My MVC page',
  3.     'action' => 'index'
  4. ));
  5.  
  6. $page = Zend_Navigation_Page::factory(array(
  7.     'label'      => 'Search blog',
  8.     'action'     => 'index',
  9.     'controller' => 'search',
  10.     'module'     => 'blog'
  11. ));
  12.  
  13. $page = Zend_Navigation_Page::factory(array(
  14.     'label'      => 'Home',
  15.     'action'     => 'index',
  16.     'controller' => 'index',
  17.     'module'     => 'index',
  18.     'route'      => 'home'
  19. ));
  20.  
  21. $page = Zend_Navigation_Page::factory(array(
  22.     'type'   => 'mvc',
  23.     'label'  => 'My MVC page'
  24. ));

Example #9 Creating a URI page using the page factory

  1. $page = Zend_Navigation_Page::factory(array(
  2.     'label' => 'My URI page',
  3.     'uri'   => 'http://www.example.com/'
  4. ));
  5.  
  6. $page = Zend_Navigation_Page::factory(array(
  7.     'label'  => 'Search',
  8.     'uri'    => 'http://www.example.com/search',
  9.     'active' => true
  10. ));
  11.  
  12. $page = Zend_Navigation_Page::factory(array(
  13.     'label' => 'My URI page',
  14.     'uri'   => '#'
  15. ));
  16.  
  17. $page = Zend_Navigation_Page::factory(array(
  18.     'type'   => 'uri',
  19.     'label'  => 'My URI page'
  20. ));

Example #10 Creating a custom page type using the page factory

To create a custom page type using the factory, use the option type to specify a class name to instantiate.

  1. class My_Navigation_Page extends Zend_Navigation_Page
  2. {
  3.     protected $_fooBar = 'ok';
  4.  
  5.     public function setFooBar($fooBar)
  6.     {
  7.         $this->_fooBar = $fooBar;
  8.     }
  9. }
  10.  
  11. $page = Zend_Navigation_Page::factory(array(
  12.     'type'    => 'My_Navigation_Page',
  13.     'label'   => 'My custom page',
  14.     'foo_bar' => 'foo bar'
  15. ));

Introduction