如何制作导航的简单示例
文件路径 module/Application/config/module.config.php
<?php
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
'default' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/[:namespace[/:controller[/:action]]]',
'constraints' => array(
'namespace' => '[a-zA-Z][a-zA-Z0-9_-]*',
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
//'locale' => 'da_DK',
'namespace' => 'Application',
'controller' => 'index',
'action' => 'index',
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'index' => 'Application\Controller\IndexController',
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),
'service_manager' => array(
'factories' => array(
'Navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
接下来是导航配置
<?php
/*
* This file path config/autoload/application.global.php
*/
return array(
// All navigation-related configuration is collected in the 'navigation' key
'navigation' => array(
// The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key
'default' => array(
// And finally, here is where we define our page hierarchy
'home' => array(
'label' => 'Home',
'route' => 'home',
),
'news' => array(
'label' => 'News',
'controller' => 'news',
'action' => 'news',
'route' => 'default',
'pages' => array(
'add' => array(
'label' => 'Add news',
'controller' => 'news', /* or create a seperate route insteed*/
'action' => 'add',
'route' => 'default',
),
),
),
),
),
);
最后回显导航布局文件或视图文件
示例模块/Application/view/layout/layout.phtml
<?php echo $this->navigation('Navigation')->menu(); ?>