Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
443 views
in Technique[技术] by (71.8m points)

zend framework2 - ZF2 - Injecting pages to navigation before controller is called

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, Name_en, ModuleName, foreignkey_ContentLinks,

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ApplicationNavigationService;

use ZendNavigationServiceAbstractNavigationFactory;
use ZendServiceManagerServiceLocatorInterface;

class CmsNavigationFactory extends AbstractNavigationFactory
{
    /**
     * @param ServiceLocatorInterface $serviceLocator
     * @return array
     * @throws endNavigationExceptionInvalidArgumentException
     */
    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' => 'ApplicationNavigationServiceCmsNavigationFactory',
    ),
),

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...