ZF2 - Injecting pages to navigation before controller is called

前端 未结 2 1370
南方客
南方客 2020-12-21 12:30

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 pag

2条回答
  •  星月不相逢
    2020-12-21 12:46

    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.

    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

    navigation()->menu('CmsNavigation'); ?>
    

提交回复
热议问题