ZF3动态导航菜单

时间:2017-09-06 19:18:25

标签: php zend-framework3

我想在ZF3应用程序中从SQL数据创建导航菜单;为了DRY的利益,我想尝试使用常用函数来查询数据。基于@Crisp对similar question的回答,如果我们想要从Blog tutorial中所示的模块创建动态菜单并使用ZendDbSqlRepository中的函数,那么导航工厂似乎应该看起来像这样:

namespace Blog\Factory;

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

use Blog\Model\ZendDbSqlRepository;

class BlogNavigationFactory 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...
            $postRepository = new ZendDbSqlRepository();
            $pages       = $postRepository->findAllPosts();

            $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';
    }
}

工厂将像这样添加到服务管理器中:

// In module/Blog/config/module.config.php:

return [
    'service_manager' => [
        'aliases' => [ /* ... */ ],
        'factories' => [
            Model\PostRepository::class => InvokableFactory::class,
            Model\ZendDbSqlRepository::class => Factory\ZendDbSqlRepositoryFactory::class,
            Model\PostCommand::class => InvokableFactory::class,
            Model\ZendDbSqlCommand::class => Factory\ZendDbSqlCommandFactory::class,
            'BlogNavigation' => 'Blog\Factory\BlogNavigationFactory',
        ],
    ],
    'controllers'  => [ /* ... */ ],
    'router'       => [ /* ... */ ],
    'view_manager' => [ /* ... */ ],
];

并在导航视图助手中使用,如下所示:

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

这不起作用,因为Blog教程通过导航工厂需要以某种方式引用的工厂和接口传递数据。

如何编写导航工厂以使用findAllPosts中的ZendDbSqlRepository功能?

1 个答案:

答案 0 :(得分:0)

getPages()方法接收ServiceManager实例(实现ServiceLocatorInterface),您可以像这样使用它来获取您的存储库

/**
 * @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 repository from the service locator
        $postRepository = $serviceLocator->get(\Blog\Model\ZendDbSqlRepository::class);
        // get your pages from repository
        $pages       = $postRepository->findAllPosts();

        $this->pages = $this->injectComponents($pages, $routeMatch, $router);
    }
    return $this->pages;
}
相关问题