2016-11-17 95 views
1

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

现在我必须从数据库中提取菜单数据,然后创建导航。 为此我正在使用我已提供配置到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的框架3我在做同样的事情,下面

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

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

回答

0

我不知道这是否是你要找的,但我建议把这个页面上看看:

https://github.com/fabiopaiva/zf2-navigation-bootstrap3

+0

@Andry布埃诺我已经检查所提供的URL。我的问题是我们在导航键中传递的数组为 'navigation'=> array( 'default'=> array(array( 'label'=>'Home', 'route'=>'home' , 'icon'=>'glyphicon glyphicon-home' ), 我想从数据库中得到这个数组,你有什么想法我怎么能从数据库中得到这个数组? –

0

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

在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);  
        }, 

       ], 

博客\查看\助手\ 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¯¯博客\导航\ 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; 
} 

} 
+0

感谢评论。错误消息“无法解析服务”Zend \ Navigation \ Default“到工厂;”你有什么想法吗?“ –

+0

Zend \ Navigation \ Default - 是我的导航的名称,你可以在module.config.php文件中找到它,例如: 'navigation'=> array( 'default'=> array( – marcin

+0

)你能提供那个吗?这对我很有帮助,谢谢。 –

0

cd。

在module.config.php

'navigation' => array(
'default' => array( 

    'blog' => array(
      'label' => 'Blog', 
      'route' => 'blog-front', 
      'controller' => 'blog', 
      'action' => 'index', 
     ) 
) 
) 
+0

我已将上面的代码片段添加到module.config.php文件中。但是我得到Class'Album \ view \ Helper \ AddItemsInNavigation'在Module.php文件中找不到错误,而该类存在于各自的目录中。你有什么主意吗? –

+0

class AddItemsInNavigation.php这是帮手。 我写了如何看待类AddItemsInNavigation.php 正如我看到你创建比我的diffrent项目。您应该在AddItemsInNavigation.php命名空间中更改Blog \ View \ Helper;在命名空间Album \ View \ Helper上; – marcin

+0

我已经在AddItemsInNavigation(命名空间Album \ view \ Helper;)中检查了它是否正确。我刚刚更改了View来查看文件夹,但按照您的建议做它也不起作用。我不知道班级没有找到的原因,因为我可以在ZF2中调用相同的方式。 –