Previous Next

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.
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.

$page = new Zend_Navigation_Page_Mvc();
$page->foo = 'bar';
$page->meaning = 42;

echo $page->foo;

if ($page->meaning != 42) {
    // action should be taken
}

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.

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 show that MVC pages use Zend_Controller_Action_Helper_Url internally to generate URIs when calling $page->getHref().

// getHref() returns /
$page = new Zend_Navigation_Page_Mvc(array(
    'action'     => 'index',
    'controller' => 'index'
));

// getHref() returns /blog/post/view
$page = new Zend_Navigation_Page_Mvc(array(
    'action'     => 'view',
    'controller' => 'post',
    'module'     => 'blog'
));

// getHref() returns /blog/post/view/id/1337
$page = new Zend_Navigation_Page_Mvc(array(
    'action'     => 'view',
    'controller' => 'post',
    'module'     => 'blog',
    'params'     => array('id' => 1337)
));

Example #3 isActive() determines if page is active

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

/*
 * Dispatched request:
 * - module:     default
 * - controller: index
 * - action:     index
 */
$page1 = new Zend_Navigation_Page_Mvc(array(
    'action'     => 'index',
    'controller' => 'index'
));

$page2 = new Zend_Navigation_Page_Mvc(array(
    'action'     => 'bar',
    'controller' => 'index'
));

$page1->isActive(); // returns true
$page2->isActive(); // returns false

/*
 * Dispatched request:
 * - module:     blog
 * - controller: post
 * - action:     view
 * - id:         1337
 */
$page = new Zend_Navigation_Page_Mvc(array(
    'action'     => 'view',
    'controller' => 'post',
    'module'     => 'blog'
));

// returns true, because request has the same module, controller and action
$page->isActive();

/*
 * Dispatched request:
 * - module:     blog
 * - controller: post
 * - action:     view
 */
$page = new Zend_Navigation_Page_Mvc(array(
    'action'     => 'view',
    'controller' => 'post',
    'module'     => 'blog',
    'params'     => array('id' => null)
));

// returns false, because page requires the id param to be set in the request
$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.

// the following route is added to the ZF router
Zend_Controller_Front::getInstance()->getRouter()->addRoute(
    'article_view', // route name
    new Zend_Controller_Router_Route(
        'a/:id',
        array(
            'module'     => 'news',
            'controller' => 'article',
            'action'     => 'view',
            'id'         => null
        )
    )
);

// a page is created with a 'route' option
$page = new Zend_Navigation_Page_Mvc(array(
    'label'      => 'A news article',
    'route'      => 'article_view',
    'module'     => 'news',    // required for isActive(), see note above
    'controller' => 'article', // required for isActive(), see note above
    'action'     => 'view',    // required for isActive(), see note above
    'params'     => array('id' => 42)
));

// returns: /a/42
$page->getHref();

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 constucting.

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 #5 The most simple custom page

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

class My_Simple_Page extends Zend_Navigation_Page
{
    public function getHref()
    {
        return 'something-completely-different';
    }
}

Example #6 A custom page with properties

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

class My_Navigation_Page extends Zend_Navigation_Page
{
    private $_foo;
    private $_fooBar;

    public function setFoo($foo)
    {
        $this->_foo = $foo;
    }

    public function getFoo()
    {
        return $this->_foo;
    }

    public function setFooBar($fooBar)
    {
        $this->_fooBar = $fooBar;
    }

    public function getFooBar()
    {
        return $this->_fooBar;
    }

    public function getHref()
    {
        return $this->foo . '/' . $this->fooBar;
    }
}

// can now construct using
$page = new My_Navigation_Page(array(
    'label'   => 'Property names are mapped to setters',
    'foo'     => 'bar',
    'foo_bar' => 'baz'
));

// ...or
$page = Zend_Navigation_Page::factory(array(
    'type'    => 'My_Navigation_Page',
    'label'   => 'Property names are mapped to setters',
    'foo'     => 'bar',
    'foo_bar' => 'baz'
));

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 #7 Creating an MVC page using the page factory

$page = Zend_Navigation_Page::factory(array(
    'label'  => 'My MVC page',
    'action' => 'index'
));

$page = Zend_Navigation_Page::factory(array(
    'label'      => 'Search blog',
    'action'     => 'index',
    'controller' => 'search',
    'module'     => 'blog'
));

$page = Zend_Navigation_Page::factory(array(
    'label'      => 'Home',
    'action'     => 'index',
    'controller' => 'index',
    'module'     => 'index',
    'route'      => 'home'
));

$page = Zend_Navigation_Page::factory(array(
    'type'   => 'mvc',
    'label'  => 'My MVC page'
));

Example #8 Creating a URI page using the page factory

$page = Zend_Navigation_Page::factory(array(
    'label' => 'My URI page',
    'uri'   => 'http://www.example.com/'
));

$page = Zend_Navigation_Page::factory(array(
    'label'  => 'Search',
    'uri'    => 'http://www.example.com/search',
    'active' => true
));

$page = Zend_Navigation_Page::factory(array(
    'label' => 'My URI page',
    'uri'   => '#'
));

$page = Zend_Navigation_Page::factory(array(
    'type'   => 'uri',
    'label'  => 'My URI page'
));

Example #9 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.

class My_Navigation_Page extends Zend_Navigation_Page
{
    protected $_fooBar = 'ok';

    public function setFooBar($fooBar)
    {
        $this->_fooBar = $fooBar;
    }
}

$page = Zend_Navigation_Page::factory(array(
    'type'    => 'My_Navigation_Page',
    'label'   => 'My custom page',
    'foo_bar' => 'foo bar'
));
Previous Next
Introduction to Zend Framework
Overview
Installation
Zend_Acl
Introduction
Refining Access Controls
Advanced Usage
Zend_Amf
Introduction
Zend_Amf_Server
Zend_Application
Introduction
Zend_Application Quick Start
Theory of Operation
Examples
Core Functionality
Available Resource Plugins
Zend_Auth
Introduction
Database Table Authentication
Digest Authentication
HTTP Authentication Adapter
LDAP Authentication
Open ID Authentication
Zend_Cache
Introduction
The Theory of Caching
Zend_Cache Frontends
Zend_Cache Backends
Zend_Captcha
Introduction
Captcha Operation
CAPTCHA Adapters
Zend_CodeGenerator
Introduction
Zend_CodeGenerator Examples
Zend_CodeGenerator Reference
Zend_Config
Introduction
Theory of Operation
Zend_Config_Ini
Zend_Config_Xml
Zend_Config_Writer
Zend_Config_Writer
Zend_Console_Getopt
Introduction
Declaring Getopt Rules
Fetching Options and Arguments
Configuring Zend_Console_Getopt
Zend_Controller
Zend_Controller Quick Start
Zend_Controller Basics
The Front Controller
The Request Object
The Standard Router
The Dispatcher
Action Controllers
Action Helpers
The Response Object
Plugins
Using a Conventional Modular Directory Structure
MVC Exceptions
Migrating from Previous Versions
Zend_Currency
Introduction to Zend_Currency
How to Work with Currencies
Migrating from Previous Versions
Zend_Date
Introduction
Theory of Operation
Basic Methods
Zend_Date API Overview
Creation of Dates
Constants for General Date Functions
Working Examples
Zend_Db
Zend_Db_Adapter
Zend_Db_Statement
Zend_Db_Profiler
Zend_Db_Select
Zend_Db_Table
Zend_Db_Table_Row
Zend_Db_Table_Rowset
Zend_Db_Table Relationships
Zend_Db_Table_Definition
Zend_Debug
Dumping Variables
Zend_Dojo
Introduction
Zend_Dojo_Data: dojo.data Envelopes
Dojo View Helpers
Dojo Form Elements and Decorators
Zend_Dojo build layer support
Zend_Dom
Introduction
Zend_Dom_Query
Zend_Exception
Using Exceptions
Zend_Feed
Introduction
Importing Feeds
Retrieving Feeds from Web Pages
Consuming an RSS Feed
Consuming an Atom Feed
Consuming a Single Atom Entry
Modifying Feed and Entry structures
Custom Feed and Entry Classes
Zend_Feed_Reader
Zend_File
Zend_File_Transfer
Validators for Zend_File_Transfer
Filters for Zend_File_Transfer
Migrating from previous versions
Zend_Filter
Introduction
Standard Filter Classes
Filter Chains
Writing Filters
Zend_Filter_Input
Zend_Filter_Inflector
Migrating from Previous Versions
Zend_Form
Zend_Form
Zend_Form Quick Start
Creating Form Elements Using Zend_Form_Element
Creating Forms Using Zend_Form
Creating Custom Form Markup Using Zend_Form_Decorator
Standard Form Elements Shipped With Zend Framework
Standard Form Decorators Shipped With Zend Framework
Internationalization of Zend_Form
Advanced Zend_Form Usage
Zend_Gdata
Introduction
Authenticating with AuthSub
Using the Book Search Data API
Authenticating with ClientLogin
Using Google Calendar
Using Google Documents List Data API
Using Google Health
Using Google Spreadsheets
Using Google Apps Provisioning
Using Google Base
Using Picasa Web Albums
Using the YouTube Data API
Catching Gdata Exceptions
Zend_Http
Introduction
Zend_Http_Client - Advanced Usage
Zend_Http_Client - Connection Adapters
Migrating from previous versions
Zend_Http_Cookie and Zend_Http_CookieJar
Zend_Http_Response
Zend_InfoCard
Introduction
Zend_Json
Introduction
Basic Usage
Advanced Usage of Zend_Json
XML to JSON conversion
Zend_Json_Server - JSON-RPC server
Zend_Layout
Introduction
Zend_Layout Quick Start
Zend_Layout Configuration Options
Zend_Layout Advanced Usage
Zend_Ldap
Introduction
API overview
Usage Scenarios
Tools
Object oriented access to the LDAP tree using Zend_Ldap_Node
Getting information from the LDAP server
Serializing LDAP data to and from LDIF
Zend_Loader
Loading Files and Classes Dynamically
The Autoloader
Resource Autoloaders
Loading Plugins
Zend_Locale
Introduction
Using Zend_Locale
Normalization and Localization
Working with Dates and Times
Supported locales
Migrating from previous versions
Zend_Log
Overview
Writers
Formatters
Filters
Zend_Mail
Introduction
Sending via SMTP
Sending Multiple Mails per SMTP Connection
Using Different Transports
HTML E-Mail
Attachments
Adding Recipients
Controlling the MIME Boundary
Additional Headers
Character Sets
Encoding
SMTP Authentication
Securing SMTP Transport
Reading Mail Messages
Zend_Measure
Introduction
Creation of Measurements
Outputting measurements
Manipulating Measurements
Types of measurements
Zend_Memory
Overview
Memory Manager
Memory Objects
Zend_Mime
Zend_Mime
Zend_Mime_Message
Zend_Mime_Part
Zend_Navigation
Introduction
Pages
Containers
Migrating from Previous Versions
Zend_OpenId
Introduction
Zend_OpenId_Consumer Basics
Zend_OpenId_Provider
Zend_Paginator
Introduction
Usage
Configuration
Advanced usage
Zend_Pdf
Introduction
Creating and Loading PDF Documents
Save Changes to PDF Documents
Working with Pages
Drawing
Interactive Features
Document Info and Metadata
Zend_Pdf module usage example
Zend_ProgressBar
Zend_ProgressBar
Zend_Queue
Introduction
Example usage
Framework
Adapters
Customizing Zend_Queue
Stomp
Zend_Reflection
Introduction
Zend_Reflection Examples
Zend_Reflection Reference
Zend_Registry
Using the Registry
Zend_Rest
Introduction
Zend_Rest_Client
Zend_Rest_Server
Zend_Search_Lucene
Overview
Building Indexes
Searching an Index
Query Language
Query Construction API
Character Set
Extensibility
Interoperating with Java Lucene
Advanced
Best Practices
Zend_Server
Introduction
Zend_Server_Reflection
Zend_Service
Introduction
Zend_Service_Akismet
Zend_Service_Amazon
Zend_Service_Amazon_Ec2
Zend_Service_Amazon_Ec2: Instances
Zend_Service_Amazon_Ec2: Windows Instances
Zend_Service_Amazon_Ec2: Reserved Instances
Zend_Service_Amazon_Ec2: CloudWatch Monitoring
Zend_Service_Amazon_Ec2: Amazon Machine Images (AMI)
Zend_Service_Amazon_Ec2: Elastic Block Stroage (EBS)
Zend_Service_Amazon_Ec2: Elastic IP Addresses
Zend_Service_Amazon_Ec2: Keypairs
Zend_Service_Amazon_Ec2: Regions and Availability Zones
Zend_Service_Amazon_Ec2: Security Groups
Zend_Service_Amazon_S3
Zend_Service_Amazon_Sqs
Zend_Service_Audioscrobbler
Zend_Service_Delicious
Zend_Service_Flickr
Zend_Service_Nirvanix
Zend_Service_ReCaptcha
Zend_Service_Simpy
Introduction
Zend_Service_StrikeIron
Zend_Service_StrikeIron: Bundled Services
Zend_Service_StrikeIron: Advanced Uses
Zend_Service_Technorati
Zend_Service_Twitter
Zend_Service_Yahoo
Zend_Session
Introduction
Basic Usage
Advanced Usage
Global Session Management
Zend_Session_SaveHandler_DbTable
Zend_Soap
Zend_Soap_Server
Zend_Soap_Client
WSDL Accessor
AutoDiscovery
Zend_Tag
Introduction
Zend_Tag_Cloud
Zend_Test
Introduction
Zend_Test_PHPUnit
Zend_Test_PHPUnit_Db
Zend_Text
Zend_Text_Figlet
Zend_Text_Table
Zend_TimeSync
Introduction
Working with Zend_TimeSync
Zend_Tool_Framework
Introduction
Using the CLI Tool
Architecture
Creating Providers to use with Zend_Tool_Framework
Shipped System Providers
Extending and Configuring Zend_Tool_Framework
Zend_Tool_Project
Introduction
Create A Project
Zend Tool Project Providers
Zend_Translate
Introduction
Adapters for Zend_Translate
Using Translation Adapters
Creating source files
Additional features for translation
Plural notations for Translation
Migrating from previous versions
Zend_Uri
Zend_Uri
Zend_Validate
Introduction
Standard Validation Classes
Validator Chains
Writing Validators
Validation Messages
Zend_Version
Getting the Zend Framework Version
Zend_View
Introduction
Controller Scripts
View Scripts
View Helpers
Zend_View_Abstract
Migrating from Previous Versions
Zend_Wildfire
Zend_Wildfire
Zend_XmlRpc
Introduction
Zend_XmlRpc_Client
Zend_XmlRpc_Server
Zend Framework Requirements
Introduction
Zend Framework Coding Standard for PHP
Overview
PHP File Formatting
Naming Conventions
Coding Style
Zend Framework Documentation Standard
Overview
Documentation File Formatting
Recommendations
Recommended Project Structure for Zend Framework MVC Applications
Overview
Recommended Project Directory Structure
Module Structure
Rewrite Configuration Guide
Zend Framework Performance Guide
Introduction
Class Loading
Zend_Db Performance
Internationalization (i18n) and Localization (l10n)
View Rendering
Copyright Information