doutang2017 2014-08-28 17:58
浏览 25

在自定义视图助手zend框架2中确定当前请求

I'm trying to determine the current request in a view helper for zend framework 2. the purpose is to add the class active to the navigation element. Here is my current code:

            <li class="active"><a href="<?php echo $this->url('home') ?>"><?php echo $this->translate('Home') ?></a></li>
            <li><a href="<?php echo $this->url('companies'); ?>"><?php echo $this->translate('Companies') ?></a>
            <li><a href="<?php echo $this->url('contacts'); ?>"><?php echo $this->translate('Contacts') ?></a>
            <li><a href="<?php echo $this->url('tasks'); ?>"><?php echo $this->translate('Tasks') ?></a>
            <li><a href="<?php echo $this->url('projects'); ?>"><?php echo $this->translate('Projects') ?></a>
            <li><a href="<?php echo $this->url('reports'); ?>"><?php echo $this->translate('Reports') ?></a>
            <li><a href="<?php echo $this->url('logout'); ?>"><?php echo $this->translate('Logout') ?></a>

As you can see, currently the home anchor is set to active.

My intentions are to do the following, per say:

            <li <?php echo $this->active('companies');?>

As you can see, I'm going to create a custom view helper called active that passes the companies module to the view helper. In the view helper, i need to access the request and ensure for this example the module is companies. So, any requests that use the company module would strategically set that navigation li element as active.

So really my question is how do i access the current module in the request in a custom view helper.

  • 写回答

1条回答 默认 最新

  • doujing6436 2014-08-28 19:00
    关注

    Here is the solution:

            <?php
            namespace Sam\View\Helper;
    
            use Zend\View\Helper\AbstractHelper;
            use Zend\ServiceManager\ServiceLocatorAwareInterface;
    
            class Active extends AbstractHelper implements ServiceLocatorAwareInterface
            {
                protected $activemodule;
    
                use \Zend\ServiceManager\ServiceLocatorAwareTrait;
    
                public function __invoke($module)
                {
                    if($this->getRouteName() === $module){
                        return 'class="active"';
                    }       
                    return null;
                }
    
                public function getRouteName()
                {
                    if (!$this->activemodule){          
                        $routeMatch = $this->getServiceLocator()->getServiceLocator()->get('Application')->getMvcEvent()->getRouteMatch();
                        $this->activemodule = $routeMatch->getMatchedRouteName();
                    }
                    return $this->activemodule;
                }
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP