Breadcrumb navigation with Doctrine NestedSet

前端 未结 3 328
半阙折子戏
半阙折子戏 2021-01-26 15:29

I have a model that implements NestedSet behaviour:

Page:
  actAs:
    NestedSet:
      hasManyRoots: true
      rootColumnName: root_id
  columns:
    slug: str         


        
3条回答
  •  一向
    一向 (楼主)
    2021-01-26 16:31

    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.

提交回复
热议问题