Zend Framework 2 入门-自定义导航

来源:互联网 发布:股票入门基础知识软件 编辑:程序博客网 时间:2024/06/08 06:46

在很多情况下我们的站点拥有不止一个导航栏,在这种情况下直接使用Zend\Navigation\Service\DefaultNavigationFactory来生成的导航栏就很有可能满足不了自己的要求,下面我们在DefaultNavigationFactory的基础上扩展自己的导航栏。

需求:我们需要一个帐号设置界面的导航。
在这个导航栏中包括主导航栏和子导航栏,结构见下:

消息>>我的消息>>我的私信帐号>>帐号设置>>消息设置>>隐私设置

 

Step 1:

首先我们建立一个AccountNavigationFactory类扩展自DefaultNavigationFactory这个类 (path:/Module/Application/src/Application/Model/Navigation/AccountNavigationFactory.php)

<?php/** * 本类实现自定义导航 * * 继承自Zend\Navigation\Service\DefaultNavigationFactory *   * @author Star <wmistar@gmail.com> * @license http://mushroot.com * @version: 1.0 * */namespace Application\Model\Navigation;use Zend\Navigation\Service\DefaultNavigationFactory;use Zend\ServiceManager\ServiceLocatorInterface;class AccountNavigationFactory extends DefaultNavigationFactory{    protected function getName()    {        return 'account_nav';    }    protected function getPages(ServiceLocatorInterface $serviceLocator)    {        if (null === $this->pages) {            $application = $serviceLocator->get('Application');            $routeMatch  = $application->getMvcEvent()->getRouteMatch();            $router      = $application->getMvcEvent()->getRouter();            $pages       = $this->getPagesFromConfig($this->accountMenu());            $this->pages = $this->injectComponents($pages, $routeMatch, $router);        }        return $this->pages;    }    public function accountMenu()    {                return array(                    array(                        'label'  => '消息',                        'route'  => 'message',                        'pages'  => array(                                array(                                        'label'  => '我的消息',                                        'route'  => 'message',                                        'params' => array('action' => 'index'),                                ),                                array(                                        'label'  => '我的私信',                                        'route'  => 'message',                                        'params' => array('action' => 'letter'),                                ),                        ),                    ),                    array(                        'label'  => '帐号',                        'route'  => 'user_account',                        'pages'  => array(                                array(                                        'label'  => '帐号设置',                                        'route'  => 'user_account',                                        'params' => array('action' => 'account'),                                ),                                array(                                        'label'  => '消息设置',                                        'route'  => 'user_account',                                        'params' => array('action' => 'message'),                                ),                                array(                                        'label'  => '隐私设置',                                        'route'  => 'user_account',                                        'params' => array('action' => 'privacy'),                                ),            );    }}

Step 2:

在Module.php中添加当前导航的Service;
(path: /Module/Application/Module.php)

public function getServiceConfig(){    return array(        'factories' => array(                'account_navigation' => 'User\Model\Navigation\AccountNavigationFactory',        )    );}

在module.config.php中添加service_manager
(path:/Module/Application/config/module.config.php)

'service_manager' => array(        'factories' => array(            'Navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',            ...        ),    ),

Step 3:

在View中显示导航
(path: /Module/Application/views/Application/index.phtml)
3.1显示全部导航,并添加class标签“account_nav”:

<?php echo $this->navigation()          ->menu('account_navigation')          ->setUlClass('account_nav');?>

3.2显示“帐号的子导航”,并添加class标签“left_nav”

 <?php $community =  $this->navigation()                   ->menu('account_navigation')                   ->findOneBy('route', 'user_account');echo          $this->navigation()                   ->menu()                   ->setUlClass('left_nav')                   ->renderMenu($community);?>

其他可用查找标签的方法(摘自官方手册):

$found = $container->findBy('id', 'page_2_and_3'); $found = $container->findOneBy('id','page_2_and_3');  $found = $container->findById('page_2_and_3'); $found = $container->findOneById('page_2_and_3');$found = $container->findAllById('page_2_and_3'); $found = $container->findAllBy('class','my-class'); $found = $container->findAllByClass('my-class');$found = $container->findOneByClass('my-class'); $found = $container->findAllBy('foo', 'bar');$found = $container->findAllByfoo('bar');$found = $container->findAllByController('index');

3.3只显示主导航

 <?php echo $this->navigation()                 ->menu('account_navigation')                 ->setMaxDepth(0);?>
0 0