面向导航与Doctrine NestedSet [英] Breadcrumb navigation with Doctrine NestedSet
问题描述
我有一个实现NestedSet行为的模型:
页面:
actAs:
NestedSet:
hasManyRoots:true
rootColumnName:root_id
列:
slug:string(255)
名称:string(255)
示例灯具:
页面:
NestedSet:true
Page_1:
slug:slug1
名称:name1
Page_2:
slug:slug2
名称:name2
儿童:
Page_3:
slug:page3
name:name3
我正在寻找最简单的方式来实现导航痕迹导航(trail)。例如,对于Page_3导航将如下所示:
< a href =page2> name2< / a> > < a href =page2 / page3> name3< / a>
与其他问题基本相同,但您必须添加一个'parentUrl'变量:
// $ / $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b $ 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
,as as $ code $ $ CurrentNodeUrl 并添加>以及当前页面的链接。
为什么这个解决方案使用递归而不是 getAncestors()
?因为你的网址似乎暗示递归。
I have a model that implements NestedSet behaviour:
Page:
actAs:
NestedSet:
hasManyRoots: true
rootColumnName: root_id
columns:
slug: string(255)
name: string(255)
Example fixtures:
Page:
NestedSet: true
Page_1:
slug: slug1
name: name1
Page_2:
slug: slug2
name: name2
children:
Page_3:
slug: page3
name: name3
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>
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;
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.
Why does this solution use recursion and not getAncestors()
? Because your urls seem to imply recursion.
这篇关于面向导航与Doctrine NestedSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!