Zend Framework 2 入门-自定义导航

在很多情况下我们的站点拥有不止一个导航栏,在这种情况下直接使用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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值