php - 在 zf3 中创建动态导航

标签 php zend-framework3

我在我的项目中使用zend Framework3。我可以按照文档 link 创建静态导航

现在我必须从数据库中获取菜单数据,然后创建导航。 为此,我使用的是 module.config.php 提供的配置,它是相册模块的配置文件。

  <?php
  namespace Album;

  use Zend\Router\Http\Literal;
  use Zend\Router\Http\Segment;
  use Zend\ServiceManager\Factory\InvokableFactory;
  use Zend\Navigation\Service\DefaultNavigationFactory;
  use Album\Navigation\AlbumNavigationFactory;

  return [
    'controllers' => [
      'factories' => [
         Controller\AlbumController::class => Factory\AlbumControllerFactory::class,
        Controller\IndexController::class => InvokableFactory::class,
          ],
       ],

     // Add this section:
    'service_manager' => [
        'factories' => [
           'navigation' => Navigation\AlbumNavigationFactory::class,
            Model\AlbumTable::class => Factory\AlbumTableFactory::class,
         ],
       ],
     // The following section is new and should be added to your file:
     'router' => [
        'routes' => [
        'album' => [
            'type'    => Segment::class,
            'options' => [
                'route' => '/album[/:action[/:id]]',
                'constraints' => [
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ],
                'defaults' => [
                    'controller' => Controller\AlbumController::class,
                    'action'     => 'index',
                ],
            ],
        ],
        'index' => [
            'type'    => Segment::class,
            'options' => [
                'route' => '/index[/:action[/:id]]',
                'constraints' => [
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ],
                'defaults' => [
                    'controller' => Controller\IndexController::class,
                    'action'     => 'index',
                ],
              ],
            ],
         ],
      ],

     'view_manager' => [
        'template_path_stack' => [
          'album' => __DIR__ . '/../view',
       ],
    ],
  ];

在 zend Framework2 中,我们简单地传递一个带有工厂类的导航键

    return array(
        'factories' => array(
            'Navigation' => 'Album\Navigation\AlbumNavigationFactory'
         ),
    );

在 zend Framework3 中我正在做与下面相同的事情

    'service_manager' => [
       'factories' => [
         'navigation' => Navigation\AlbumNavigationFactory::class,
          Model\AlbumTable::class => Factory\AlbumTableFactory::class,
        ],
    ],

我正在使用 Navigation\AlbumNavigationFactory::class 来调用工厂来获取数据。 但我无法获得导航。任何帮助,将不胜感激。

最佳答案

这是我的代码的一部分。我认为会有帮助。工作完美。

在Module.php中

public function getServiceConfig()
 { 
    return array( 
      'factories' => array(
          'ItemsFromDatabase::class => Navigation\BlogNavigationFactory::class,
       )
            );
}

public function getViewHelperConfig() {  
      return[
        'factories' => [
            'AddItemsInNavigation' => function($helpers) {
               $navigation = $helpers->get('Application')->getServiceManager()->get('Zend\Navigation\Default')->findOneByLabel('Blog');
               $newItems = $helpers->get(ItemsFromDatabase::class);
             return new View\Helper\AddItemsInNavigation($navigation, $newItems);    
                    },

                ],

博客\ View \Helper\AddItemsInNavigation.php

<?php
namespace Blog\View\Helper;

use Zend\View\Helper\AbstractHelper;

class AddItemsInNavigation extends AbstractHelper {

protected $navigation;
protected $newItems;

public function __construct($navigation, $newItems) {
    $this->navigation = $navigation;
    $this->newItems = $newItems; 
}

public function addItems() {
   return $this->navigation->addPages($this->newItems);

}

}

布局中

      <?php
      $this->AddItemsInNavigation()->addItems(); //plugin

      $nawDef = $this->navigation('Zend\Navigation\Default')->menu();

      echo $nawDef->setMinDepth(0)->setMaxDepth(4)->setUlClass('nav navbar-nav');

                ?>

W Blog\Navigation\BlogNavigationFactory.php

<?php
namespace Blog\Navigation;

use Interop\Container\ContainerInterface;
use Zend\Navigation\Navigation;
use Zend\Navigation\Service\DefaultNavigationFactory;

class BlogNavigationFactory extends DefaultNavigationFactory {

protected $pages;

public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
    return new Navigation($this->getPages($container));
}

protected function getPages(ContainerInterface $container) {

    $navigation = array();

    if (null === $this->pages) {

        $navigation[] = array ( //for exemple 
            'label' => 'Jaapsblog.nl',
            'uri'   => 'http://www.jaapsblog.nl'
        );

        $mvcEvent = $container->get('Application')
                ->getMvcEvent();

        $routeMatch = $mvcEvent->getRouteMatch();
        $router = $mvcEvent->getRouter();
        $pages = $this->getPagesFromConfig($navigation);

        $this->pages = $this->injectComponents(
                $pages, $routeMatch, $router
        );
    }

    return $this->pages;
}

}

关于php - 在 zf3 中创建动态导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40662171/

相关文章:

PHP如何使请求失败

php - 使用 throw try catch 错误处理的正确方法

php - MySQL 和 PHP 多个 list 数据库插入

javascript - URL PHP/jQuery 的链接预览

database - zf3 中的 doctrine2 多数据库连接?

php - 如何根据数量更新商品的总价?

css - Zend 3 样式表不包含在 IE 中

php - composer PSR-4 自动加载器 "class not found"错误

php - 如何使用 zf-console 执行 Zend Framework 3 操作?

zend-framework2 - ZF3 - EventManager 和调度事件