The navigation helpers are used for rendering navigational elements from Zend\Navigation\Navigation instances.
There are 5 built-in helpers:
All built-in helpers extend Zend\View\Helper\Navigation\AbstractHelper, which adds integration with ACL and translation. The abstract class implements the interface Zend\View\Helper\Navigation\HelperInterface, which defines the following methods:
In addition to the method stubs from the interface, the abstract class also implements the following methods:
If a container is not explicitly set, the helper will create an empty Zend\Navigation\Navigation container when calling $helper->getContainer().
Proxying calls to the navigation container
Navigation view helpers use the magic method __call() to proxy method calls to the navigation container that is registered in the view helper.
1 2 3 | $this->navigation()->addPage(array(
'type' => 'uri',
'label' => 'New page'));
|
The call above will add a page to the container in the Navigation helper.
The navigation helpers support translation of page labels and titles. You can set a translator of type Zend\I18n\Translator in the helper using $helper->setTranslator($translator).
If you want to disable translation, use $helper->setUseTranslator(false).
The proxy helper will inject its own translator to the helper it proxies to if the proxied helper doesn’t already have a translator.
Note
There is no translation in the sitemap helper, since there are no page labels or titles involved in an XML sitemap.
All navigational view helpers support ACL inherently from the class Zend\View\Helper\Navigation\AbstractHelper. An object implementing Zend\Permissions\Acl\AclInterface can be assigned to a helper instance with $helper->setAcl($acl), and role with $helper->setRole(‘member’) or $helper->setRole(new Zend\Permissions\Acl\Role\GenericRole(‘member’)). If ACL is used in the helper, the role in the helper must be allowed by the ACL to access a page’s resource and/or have the page’s privilege for the page to be included when rendering.
If a page is not accepted by ACL, any descendant page will also be excluded from rendering.
The proxy helper will inject its own ACL and role to the helper it proxies to if the proxied helper doesn’t already have any.
The examples below all show how ACL affects rendering.
This example shows the setup of a navigation container for a fictional software company.
Notes on the setup:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | /*
* Navigation container (config/array)
* Each element in the array will be passed to
* Zend\Navigation\Page\AbstractPage::factory() when constructing
* the navigation container below.
*/
$pages = array(
array(
'label' => 'Home',
'title' => 'Go Home',
'module' => 'default',
'controller' => 'index',
'action' => 'index',
'order' => -100 // make sure home is the first page
),
array(
'label' => 'Special offer this week only!',
'module' => 'store',
'controller' => 'offer',
'action' => 'amazing',
'visible' => false // not visible
),
array(
'label' => 'Products',
'module' => 'products',
'controller' => 'index',
'action' => 'index',
'pages' => array(
array(
'label' => 'Foo Server',
'module' => 'products',
'controller' => 'server',
'action' => 'index',
'pages' => array(
array(
'label' => 'FAQ',
'module' => 'products',
'controller' => 'server',
'action' => 'faq',
'rel' => array(
'canonical' => 'http://www.example.com/?page=faq',
'alternate' => array(
'module' => 'products',
'controller' => 'server',
'action' => 'faq',
'params' => array('format' => 'xml')
)
)
),
array(
'label' => 'Editions',
'module' => 'products',
'controller' => 'server',
'action' => 'editions'
),
array(
'label' => 'System Requirements',
'module' => 'products',
'controller' => 'server',
'action' => 'requirements'
)
)
),
array(
'label' => 'Foo Studio',
'module' => 'products',
'controller' => 'studio',
'action' => 'index',
'pages' => array(
array(
'label' => 'Customer Stories',
'module' => 'products',
'controller' => 'studio',
'action' => 'customers'
),
array(
'label' => 'Support',
'module' => 'products',
'controller' => 'studio',
'action' => 'support'
)
)
)
)
),
array(
'label' => 'Company',
'title' => 'About us',
'module' => 'company',
'controller' => 'about',
'action' => 'index',
'pages' => array(
array(
'label' => 'Investor Relations',
'module' => 'company',
'controller' => 'about',
'action' => 'investors'
),
array(
'label' => 'News',
'class' => 'rss', // class
'module' => 'company',
'controller' => 'news',
'action' => 'index',
'pages' => array(
array(
'label' => 'Press Releases',
'module' => 'company',
'controller' => 'news',
'action' => 'press'
),
array(
'label' => 'Archive',
'route' => 'archive', // route
'module' => 'company',
'controller' => 'news',
'action' => 'archive'
)
)
)
)
),
array(
'label' => 'Community',
'module' => 'community',
'controller' => 'index',
'action' => 'index',
'pages' => array(
array(
'label' => 'My Account',
'module' => 'community',
'controller' => 'account',
'action' => 'index',
'resource' => 'mvc:community.account' // resource
),
array(
'label' => 'Forums',
'uri' => 'http://forums.example.com/',
'class' => 'external' // class
)
)
),
array(
'label' => 'Administration',
'module' => 'admin',
'controller' => 'index',
'action' => 'index',
'resource' => 'mvc:admin', // resource
'pages' => array(
array(
'label' => 'Write new article',
'module' => 'admin',
'controller' => 'post',
'action' => 'write'
)
)
)
);
// Create container from array
$container = new Zend\Navigation\Navigation($pages);
// Store the container in the proxy helper:
$view->plugin('navigation')->setContainer($container);
// ...or simply:
$view->navigation($container);
|
In addition to the container above, the following setup is assumed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?php
// module/MyModule/config/module.config.php
return array(
/* ... */
'router' array(
'routes' => array(
'archive' => array(
'type' => 'Segment',
'options' => array(
'route' => '/archive/:year',
'defaults' => array(
'module' => 'company',
'controller' => 'news',
'action' => 'archive',
'year' => (int) date('Y') - 1,
),
'constraints' => array(
'year' => '\d+',
),
),
),
/* You can have other routes here... */
),
),
/* ... */
);
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?php
// module/MyModule/Module.php
namespace MyModule;
use Zend\View\HelperPluginManager;
use Zend\Permissions\Acl\Acl;
use Zend\Permissions\Acl\Role\GenericRole;
use Zend\Permissions\Acl\Resource\GenericResource;
class Module
{
/* ... */
public function getViewHelperConfig()
{
return array(
'factories' => array(
// This will overwrite the native navigation helper
'navigation' => function(HelperPluginManager $pm) {
// Setup ACL:
$acl = new Acl();
$acl->addRole(new GenericRole('member'));
$acl->addRole(new GenericRole('admin'));
$acl->addResource(new GenericResource('mvc:admin'));
$acl->addResource(new GenericResource('mvc:community.account'));
$acl->allow('member', 'mvc:community.account');
$acl->allow('admin', null);
// Get an instance of the proxy helper
$navigation = $pm->get('Zend\View\Helper\Navigation');
// Store ACL and role in the proxy helper:
$navigation->setAcl($acl)
->setRole('member');
// Return the new navigation helper instance
return $navigation;
}
)
);
}
/* ... */
}
|
Full-text doc search.
Introduction to Zend\Paginator
Enter search terms or a module, class or function name.
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.