加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

zend-framework2 – ZF2 – 在调用控制器之前将页面注入导航

发布时间:2020-12-13 13:46:25 所属栏目:PHP教程 来源:网络整理
导读:我正在创建一个动态应用程序,通过CMS添加内容.在CMS内部,我正在设置一个数据库条目,该条目说明了每个内容页面使用的模块. 的NodeId, ParentNodeId, Name_de, Name_en, 模块名, foreignkey_ContentLinks, 在此表中条目如下所示: 6, 1, Veranstaltung-21-02-2
我正在创建一个动态应用程序,通过CMS添加内容.在CMS内部,我正在设置一个数据库条目,该条目说明了每个内容页面使用的模块.

的NodeId,
ParentNodeId,
Name_de,
Name_en,
模块名,
foreignkey_ContentLinks,

在此表中条目如下所示:
6,
1,
Veranstaltung-21-02-2013,
事件21-02-2013,
活动,
682

整个树应该在我的导航中结束(完全也在我的路由中).我不想在某个控制器中添加它,因为我的应用程序由一大堆模块组成,我希望在所有模块中访问该信息.

我已经尝试在global.php中注入它,但无济于事,因为我不能在我的数据库适配器或该阶段的任何其他重要类.

有关最佳实践的想法或链接吗?

导航容器由工厂类组成.最简单的方法是编写自己的工厂并使用getPages()方法从数据库而不是从config获取页面.如果从AbstractNavigationFactory扩展,则只需要编写几个方法.
<?php
namespace ApplicationNavigationService;

use ZendNavigationServiceAbstractNavigationFactory;
use ZendServiceManagerServiceLocatorInterface;

class CmsNavigationFactory extends AbstractNavigationFactory
{
    /**
     * @param ServiceLocatorInterface $serviceLocator
     * @return array
     * @throws ZendNavigationExceptionInvalidArgumentException
     */
    protected function getPages(ServiceLocatorInterface $serviceLocator)
    {
        if (null === $this->pages) {

            $application = $serviceLocator->get('Application');
            $routeMatch  = $application->getMvcEvent()->getRouteMatch();
            $router      = $application->getMvcEvent()->getRouter();

            // get your pages from wherever...
            $pages       = $this->getPagesFromDB();

            $this->pages = $this->injectComponents($pages,$routeMatch,$router);
        }
        return $this->pages;
    }

    public function getName()
    { 
         // this isn't used if fetching from db,it's just here to keep the abstract factory happy
         return 'cms';
    }
}

将工厂添加到服务管理器,就像您对其他容器一样

'service_manager' => array(
    'factories' => array(
        'CmsNavigation' => 'ApplicationNavigationServiceCmsNavigationFactory',),

并以相同的方式将其与导航视图助手一起使用

<?php echo $this->navigation()->menu('CmsNavigation'); ?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读