ZF2 - Injecting pages to navigation before controller is called

蹲街弑〆低调 提交于 2019-11-28 09:48:12

问题


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?


回答1:


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'); ?>



回答2:


Responding to your comment on @Crisp's answer, and for future googlers, I'll explain how to do something similar for routing.

Typically you would want to create a custom router that can match URLs to the pages in your database, similarly to the standard Segment router. To do this, you will have to implement the Zend\Mvc\Router\RouteInterface interface. For example:

namespace Application\Router;

use Zend\Mvc\Router\RouteInterface;
use Application\Model\CMSTable;

class CmsRoute implements RouteInterface, ServiceLocatorAwareInterface
{
    protected $table;

    // Service locator injection code

    public function getCmsTable()
    {
        // Retrieve the table from the service manager
    }

    public function match(Request $request)
    {
        // Match the request on some route field, etc.
    }

    public function assemble(array $params = array(), array $options = array())
    {
        // Assemble a URL based on the given parameters (e.g. page ID).
    }

    public static function factory($options = array())
    {
        // Construct a new route, based on the options.
    }
}

You could then register this route as an invokable for the RoutePluginManager in your module configuration:

'route_manager' => array(
  'invokables' => array(
    'Cms' => 'Application\Router\CmsRoute'
  ),
),

Then, you can create a new route (just as you would for any other route) with type Cms. The route plugin manager will create your route instance, and since CmsRoute implements ServiceLocatorAwareInterface, the plugin manager will inject itself in the route. In turn, the plugin manager has the main service manager set, so that you can get the database table from there!

Of course you can match on page ID, but if you have a hierarchical structure, it's nicer to reflect that in your URLs. I would therefore recommend adding a route field to the database schema and match on that, beginning with the tree root and working down.



来源:https://stackoverflow.com/questions/16622179/zf2-injecting-pages-to-navigation-before-controller-is-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!