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.
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.
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().
'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)
));
]]>
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.
'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
]]>
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 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.
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();
]]>
Set parameters to use when assembling URL
// The following route is added to the ZF router
Zend_Controller_Front::getInstance()->getRouter()->addRoute(
'article_list', // route name
new Zend_Controller_Router_Route(
'blog/:category/:page',
array(
'module' => 'blog',
'controller' => 'article',
'action' => 'list',
'category' => null,
'page' => null,
)
)
);
// A page is created with the 'route' option
$page = new Zend_Navigation_Page_Mvc(array(
'label' => 'Article list',
'module' => 'blog',
'controller' => 'post',
'action' => 'list',
));
// Add multiple parameters at once
$page->addParams(
array(
'category' => 'news',
'page' => 1,
)
);
// Add a single parameter
$page->addParam('category', 'news');
// Set multiple parameters at once (Overwrites any previously set parameters!)
$page->setParams(
array(
'category' => 'news',
'page' => 1,
)
);
// Set a single parameter
$page->setParam('category', 'news');
// Retrieve all parameters
$params = $page->getParams();
// Retrieve a single parameter
$category = $page->getParam('category');
// Remove a parameter
$page->removeParam('page');
// Clear all parameters
$page->clearParams();