2013-07-26 50 views
3

我是Zend Framework 2的新手,我試圖用ZF2生成動態麪包屑,但沒有成功。zend framework 2動態麪包屑 - 傳遞參數

我使用http://adam.lundrigan.ca/2012/07/quick-and-dirty-zf2-zend-navigation/作爲我的工作的基礎,並描述了我爲我的模塊公開的路由構建了站點地圖。

實施例:

// config/autoload/nav_zfcuser.global.php 

<?php 
return array(
    // All navigation-related configuration is collected in the 'navigation' key 
    'navigation' => array(
     // The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key 
     'default' => array(
      // And finally, here is where we define our page hierarchy 
      'home' => array(
       'label' => 'Home', 
       'route' => 'home', 
       'pages' => array(
        'sitemap' => array(
         'label' => 'Sitemap', 
         'title' => 'Sitemap', 
         'route' => 'sitemap', 
        ), 
        'aboutus' => array(...), 
        'privacy' => array(...), 
       ), 
      ), 
     ), 
    ), 
); 

然後以使麪包屑中使用的下列視圖助手:

<?php echo $this->navigation()->breadcrumbs('Navigation'); ?> 

這工作正常,並且例如,如果我導航上mywebsite.com/sitemap渲染bredcrumbs將是。

首頁>網站地圖

問題是,當我開始具有動態URL's(例如用產品ID's)。

module.config.php路由是..

<?php 
return array(
    'router' => array(
     'routes' => array(

     (...) 

    'productHome' => array(
      'type'  => "Literal", 
      'options' => array(
       'route'  => "/product", 
       'defaults' => array(
        '__NAMESPACE__' => "Application\Controller", 
        'controller' => "Product", 
        'action'  => "index", 
       ), 
      ), 
      'may_terminate' => true, 
      'child_routes' => array(
       'product' => array(
        'type'  => "Segment", 
        'options' => array(
         'route'  => "/:idProduct[/:name]", 
         'defaults' => array(
          '__NAMESPACE__' => "Application\Controller", 
          'controller' => "Product", 
          'action'  => "product", 
         ), 
        ), 
        'may_terminate' => true, 
       ), 
      ), 

上述(module.config.php)定義的路由轉換爲下列網址結構:

mysite的.com/Product /:idProduct [/名稱]

因此構建反映此結構的站點地圖應該是:

// config/autoload/nav_zfcuser.global.php 

<?php 
    return array(
     // All navigation-related configuration is collected in the 'navigation' key 
     'navigation' => array(
      // The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key 
      'default' => array(
       // And finally, here is where we define our page hierarchy 
       'home' => array(
        'label' => 'Home', 
        'route' => 'home', 
        'pages' => array(
         'sitemap' => array(...), 
         'productHome' => array(
          'label' => 'Product', 
          'route' => 'product', 
          'controller' => 'Product', 
          'action' => 'index', 
          'pages' => array(
           'product' => array(
           'label' => 'Product', 
           'controller' => 'Product', 
           'action' => 'product', 
           'route' => 'product/:idProduct[/:name]', 

          ), 
         ), 
        ), 
        ), 
       ), 
      ), 
     ), 
    ); 

瀏覽器刷新頁面的產品mywebsite.com/product/flv6n768/ProductName 沒有顯示麪包屑。

如何傳遞「idProduct」和「名」 PARAMS這樣,當我打電話視圖助手

<?php echo $this->navigation()->breadcrumbs('Navigation'); ?> 

它呈現:產品首頁>產品> idProduct(不可見)>的名字嗎?

回答

4

如果它是麪包屑的目的,只有你能做到以下幾點:

修改您的導航陣列如下 - 刪除特定的產品頁規格。並將ID添加到產品頁面。

<?php 
return array(
    // All navigation-related configuration is collected in the 'navigation' key 
    'navigation' => array(
     // The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key 
     'default' => array(
      // And finally, here is where we define our page hierarchy 
      'home' => array(
       'label' => 'Home', 
       'route' => 'home', 
       'pages' => array(
        'sitemap' => array(...), 
        'productHome' => array(
         'id' => 'productHome',//<<< Page unique ID 
         'label' => 'Product', 
         'route' => 'product', 
         'controller' => 'Product', 
         'action' => 'index', 
        ), 
       ), 
       ), 
      ), 
     ), 
    ), 
); 

在Application \ Controller \ Product :: product()中添加了以下代碼。這會在導航靜態結構中將「動態」新的子頁面添加到產品索引頁面。另外請注意,我認爲你已經在$ product變量中獲得了實際的產品實例。

$navigation = $this->getServiceLocator()->get('Navigation'); 
    $page = $navigation->findBy('id', 'productHome'); 
    $page->addPage(array(
     'uri' => $this->getRequest()->getRequestUri(),//current page URI 
     'label' => $product->getName(),//<<<<< product name 
     'active' => true, 
    ));