Breadcrumb navigation with Doctrine NestedSet

前端 未结 3 337
半阙折子戏
半阙折子戏 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

    Since I hate having any kind of logic in templates (and partials), here's my slightly improved version.

    //module/templates/_breadcrumbElement.php
    
    
  • getName() ?> get('__children')) > 0): ?>
      $child->get('__children'), 'parent' => $child)) ?>
  • So, all the logic for building a url is now in Page::getPath() method.

    class Page extends BasePage
    {
      /**
       * Full path to node from root
       *
       */
      protected $path = false;
    
      public function __toString()
      {
        return $this->getSlug();
      }
      public function getPath($parent = null)
      {
        if (!$this->path)
        {
          $this->path = join('/', null !== $parent ? array($parent->getPath(), $this) : array($this));
        }
        return $this->path;
      } 
    }
    

    What I don't like it having to pass $parent to Page::getPath(). It just doesn't make any semantical sense.

提交回复
热议问题