Zend框架:用面包与部分

问题描述:

我有一些麻烦让一个定制的痕迹: 我我layout.phtml我有:Zend框架:用面包与部分

$container = new Zend_Navigation(); 
$this->navigation($container); 

$container->addPage(
array(
    'label'  => 'Dashboard', 
    'module'  => 'default', 
    'controller' => 'dashboard', 
    'action'  => 'index', 
    'pages'  => 
    array(
     array(
      'label'  => 'Create Order', 
      'module'  => 'default', 
      'controller' => 'createorder', 
      'action'  => 'index' 
     ), 
     array(
      'label'  => 'Query', 
      'module'  => 'default', 
      'controller' => 'query', 
      'action'  => 'index', 
      'pages' => array(
       array(
        'label'  => 'View Order', 
        'module'  => 'default', 
        'controller' => 'order', 
        'action'  => 'vieworder' 
       ) 
      ) 
     ), 
     array(
      'label'  => 'Administration', 
      'module'  => 'default', 
      'controller' => 'admin', 
      'action'  => 'index', 
      'pages'  => 
      array(
       array(
        'label'  => 'News and Announcements', 
        'module'  => 'default', 
        'controller' => 'admin', 
        'action' => 'addnews', 
        'pages' => array(
         array(
          'label'  => 'Edit News and Announcements', 
          'module'  => 'default', 
          'controller' => 'admin', 
          'action' => 'editnews' 
         ) 
        ) 
       ) 
      ) 
     ) 
    ) 
) 
); 

的下一行是:

echo $this->navigation()->breadcrumbs()->setPartial(array('BreadCrumb.phtml','default')); 

面包屑。 phtml被称为很好,但我不知道如何在我的BreadCrumb.phtml中创建ul-li菜单。我如何获得我实际上的导航? 在此先感谢您的帮助。 Andrea

在视图脚本中,可以通过$this->container获取zend导航容器。该容器迭代的,所以你可以通过它用一个简单的foreach循环行走(如foreach($this->container as $page)

要做到这一点,你BreadCrumb.phtml应该是这样的:

<?php 
if (null === $this->container) { 
    $this->container = $this->breadcrumbs()->getContainer(); 
} 

// find deepest active 
if (!$active = $this->breadcrumbs()->findActive($this->container)) { 
    return ''; 
} 

$active = $active['page']; 

// put the deepest active page last in breadcrumbs 
if ($this->breadcrumbs()->getLinkLast()) { 
    $html = ' <li>' . $this->breadcrumbs()->htmlify($active) . '</li>' . PHP_EOL; 
} else { 
    $html = $active->getLabel(); 
    if ($this->breadcrumbs()->getUseTranslator() && $t = $this->breadcrumbs()->getTranslator()) { 
     $html = $t->translate($html); 
    } 
    $html = ' <li>' . $this->escape($html) . '</li>' . PHP_EOL; 
} 

// walk back to root 
while (($parent = $active->getParent()) != null) { 
    if ($parent instanceof Zend_Navigation_Page) { 
     // prepend crumb to html 
     $html = ' <li>' . $this->breadcrumbs()->htmlify($parent) . '</li>' . PHP_EOL . $html; 
    } 

    if ($parent === $this->container) { 
     // at the root of the given container 
     break; 
    } 

    $active = $parent; 
} 
echo strlen($html) ? $this->breadcrumbs()->getIndent() . '<ul id="breadcrumbs">' . PHP_EOL 
       . $html . '</ul>' . PHP_EOL : ''; 
?> 

然后你会得到这样的事情:

<ul id="breadcrumbs"> 
    <li><a href="/home">Home</a></li> 
    <li><a href="/articles">Articles</a></li> 
    <li><a href="/shop">Shop</a></li> 
    <li>Last link</li> 
</ul> 
+0

感谢你的回复,将进行测试很快,给你一个反馈...... – cwhisperer 2012-01-18 15:02:30

+0

没问题,我提高了一点点我的代码,使其更工作,如果有什么好让我知道! – Liyali 2012-04-04 04:53:32

+0

这是一个godd渲染面包屑的例子@Liyali!你在哪里找到它?这一个应该被添加到官方文档的例子 - 目前的是“穷人”:http://framework.zend.com/manual/2.3/en/modules/zend.navigation.view.helper.breadcrumbs.html#rendering -using-a-view-script – webDEVILopers 2014-06-18 09:10:07