Класс панировочных сухарей в symfony 4 - PullRequest
0 голосов
/ 11 октября 2018

новое в Symfony 4, я пытаюсь создать класс хлебных крошек, но класс сбрасывается на каждом маршруте.

Я хочу сохранить предыдущий заголовок / ссылку и добавить новый маршрут.

Но класс сбрасывается!Даже если я не использую новые Breadcrumbs ();

Как я могу это исправить?

Контроллер:

<?php    

namespace App\Controller;

use App\Utils\Breadcrumbs;
use phpDocumentor\Reflection\Types\Null_;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

    class MyController extends AbstractController
    {
       /**
        * @Route("/index", name="index")      
        */
        public function index(Breadcrumbs $breadcrumbs)
        {             

                 $breadcrumbs->add('home','http://'.STORE_NAME);      

               return $this->render('storename/index.html.twig',[

                'bread'=>$breadcrumbs->trail()
               ]);                       
        }
    }
    ?>

Класс хлебных крошек: (теперь в Symfony4, мы можем поместить этот класс везде)

<?php

namespace App\Utils;

use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;


Class Breadcrumbs{

private $_trail;
private $route_uri;
private $route_name;

 function  __construct() { 

$this->reset();
}

 function reset() { 

          $this->_trail=array();
    }

  public function add($title, $link='') { 

          $this->_trail[]= array('title'=>$title, 'link'=>$link);
    }

   public function trail($separator = ' &raquo; ') {
      $pos = 1;
      $trail_string = '<ol  itemscope itemtype="http://schema.org/BreadcrumbList" class="breadcrumb">';

      for ($i=0, $n=sizeof($this->_trail); $i<$n; $i++) {
        if (isset($this->_trail[$i]['link']) && ($this->_trail[$i]['link']!=null)) {
          $trail_string .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . $this->_trail[$i]['link'] . '" itemprop="item"><span itemprop="name">' . ucfirst(mb_strtolower($this->_trail[$i]['title'],'UTF-8')) . '</span></a>';
        } else {
          $trail_string .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name">' . ucfirst(mb_strtolower($this->_trail[$i]['title'], 'UTF-8')) . '</span>';
        }
        $trail_string .= '<meta itemprop="position" content="' . (int)$pos . '" /></li>' . PHP_EOL;
        $pos++;
              }      

      $trail_string .= '</ol>';

      return $trail_string;
    }
  }

?>

Шаблон ветки base.html.twig

 {% set bread= Breadcrumbs.trail() %}
{% block breadcrumbs %}{{ bread|raw }}{% endblock %}

Спасибо за помощь.

...