本文介绍了使用 Doctrine NestedSet 进行面包屑导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现 NestedSet 行为的模型:

I have a model that implements NestedSet behaviour:

Page:
  actAs:
    NestedSet:
      hasManyRoots: true
      rootColumnName: root_id
  columns:
    slug: string(255)
    name: string(255)

夹具示例:

Page:
  NestedSet: true
  Page_1:
    slug: slug1
    name: name1
  Page_2:
    slug: slug2
    name: name2
    children:
      Page_3:
        slug: page3
        name: name3

我正在寻找实现面包屑导航(轨迹)的最简单方法.例如,对于 Page_3,导航将如下所示:

I am looking for the easiest way to implement breadcrumb navigation (trail). For example, for Page_3 navigation will look like this:

<a href="page2">name2</a> > <a href="page2/page3>name3</a>

推荐答案

几乎和其他问题一样,但是你必须添加一个'parentUrl'变量:

Almost the same as in the other question, but you have to add a 'parentUrl' variable :

//module/templates/_breadcrumbElement.php
foreach ($node->get('__children') as $child) :
  if ($child->isAncestorOf($pageNode)):
     $currentNodeUrl = $parentUrl . $child->getSlug() . '/';
     echo link_to($child->getName(), $currentNodeUrl) . ' > ' ;
     include_partial('module/breadcrumbElement', array('node' => $child, 'pageNode' => $pageNode, 'parentUrl' => $currentNodeUrl));
  endif;
endforeach;

将你的树的根作为 $node (分层水合它),当前页面的节点作为 $pageNode,并将 '' 作为 $currentNodeUrl 并添加 ' > ' 和当前页面的链接.

Feed it the root of your tree as $node (hydrate it hierarchically), the node of the current page as $pageNode, and '' as $currentNodeUrl and add ' > ' and the link to the current page.

为什么这个解决方案使用递归而不是 getAncestors()?因为您的网址似乎暗示着递归.

Why does this solution use recursion and not getAncestors()? Because your urls seem to imply recursion.

这篇关于使用 Doctrine NestedSet 进行面包屑导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 00:03