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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了zend-framework2 – ZF2 – 在调用控制器之前将页面注入导航脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在创建一个动态应用程序,通过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 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';
    }
}

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

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

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

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

脚本宝典总结

以上是脚本宝典为你收集整理的zend-framework2 – ZF2 – 在调用控制器之前将页面注入导航全部内容,希望文章能够帮你解决zend-framework2 – ZF2 – 在调用控制器之前将页面注入导航所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。