ZF2-在调用控制器之前将页面注入导航 [英] ZF2 - Injecting pages to navigation before controller is called

查看:108
本文介绍了ZF2-在调用控制器之前将页面注入导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个动态应用程序,其中的内容是通过CMS添加的.在CMS内部,我正在设置一个数据库条目,该条目说明要用于每个内容页面的模块.

I'm creating a dynamic Application in which the content is added through a CMS. Inside the CMS, I'm setting a db entry which states what module to use for each content page.

NodeId, ParentNodeId, Name_de, 姓名, ModuleName, foreignkey_ContentLinks,

NodeId, ParentNodeId, Name_de, Name_en, ModuleName, foreignkey_ContentLinks,

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

in this table entries look as follows: 6, 1, Veranstaltung-21-02-2013, Event-21-02-2013, Events, 682

整个树应该最终出现在我的导航中(完美地也应该出现在我的路由中).我不想在某个控制器中添加它,因为我的应用程序由一堆模块组成,并且我想在所有模块中访问该信息.

The entire tree should end up in my navigation (and perfectly also in my routing). I do not want to add it in some controller, because my Application consists of a whole bunch of Modules and I want to access that Info across all my Modules.

我已经尝试过将其注入global.php中,但无济于事,因为在那个阶段我无法使用数据库适配器或任何其他重要的类.

I already tried injecting it in the global.php, but to no avail because I can't my db adapter or any other important classes at that stage.

有任何想法或最佳做法的链接吗?

Any ideas or links to best practices?

推荐答案

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

The navigation containers are composed by factory classes. The easiest approach is to write your own factory and have the getPages() method fetch pages from a database instead of from config. If you extend from the AbstractNavigationFactory you only need to write a couple of methods.

<?php
namespace Application\Navigation\Service;

use Zend\Navigation\Service\AbstractNavigationFactory;
use Zend\ServiceManager\ServiceLocatorInterface;

class CmsNavigationFactory extends AbstractNavigationFactory
{
    /**
     * @param ServiceLocatorInterface $serviceLocator
     * @return array
     * @throws \Zend\Navigation\Exception\InvalidArgumentException
     */
    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';
    }
}

将工厂添加到服务经理,就像处理其他容器一样

Add the factory to the service manager, just like you would for other containers

'service_manager' => array(
    'factories' => array(
        'CmsNavigation' => 'Application\Navigation\Service\CmsNavigationFactory',
    ),
),

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

And use it with the navigation view helpers in the same way

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

这篇关于ZF2-在调用控制器之前将页面注入导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆