2016-11-17 116 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中調用相同的方式。 –