首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ZF3动态导航菜单

ZF3动态导航菜单
EN

Stack Overflow用户
提问于 2017-09-07 03:18:25
回答 1查看 739关注 0票数 1

我想从ZF3应用程序中的SQL数据创建导航菜单;出于对DRY的兴趣,我想尝试使用通用函数来查询数据。

代码语言:javascript
复制
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';
    }
}

工厂将被添加到服务管理器,如下所示:

代码语言:javascript
复制
// 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' => [ /* ... */ ],
];

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

代码语言:javascript
复制
<?php echo $this->navigation()->menu('BlogNavigation'); ?>

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

如何编写导航工厂以使用ZendDbSqlRepository中的findAllPosts函数

EN

回答 1

Stack Overflow用户

发布于 2017-09-08 02:49:30

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

代码语言:javascript
复制
/**
 * @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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46082810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档