add better routing naming
This commit is contained in:
parent
169bd4ecf3
commit
3ded5260fa
|
@ -138,4 +138,9 @@ class Menu implements EntityInterface
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getRouteName(): string
|
||||||
|
{
|
||||||
|
return $this->getNavigation()->getRouteName().'_'.($this->getCode() ? $this->getCode() : $this->getId());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,4 +119,9 @@ class Navigation implements EntityInterface
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getRouteName(): string
|
||||||
|
{
|
||||||
|
return $this->getCode() ? $this->getCode() : 'navigation_'.$this->getId();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,6 +92,11 @@ class Node implements EntityInterface
|
||||||
*/
|
*/
|
||||||
private $page;
|
private $page;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="string", length=255, nullable=true)
|
||||||
|
*/
|
||||||
|
private $code;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->children = new ArrayCollection();
|
$this->children = new ArrayCollection();
|
||||||
|
@ -279,4 +284,21 @@ class Node implements EntityInterface
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getRouteName(): string
|
||||||
|
{
|
||||||
|
return $this->getMenu()->getRouteName().'_'.($this->getCode() ? $this->getCode() : $this->getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCode(): ?string
|
||||||
|
{
|
||||||
|
return $this->code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setCode(?string $code): self
|
||||||
|
{
|
||||||
|
$this->code = $code;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ use App\EventSuscriber\EntityManagerEventSubscriber;
|
||||||
use App\Factory\Site\NodeFactory;
|
use App\Factory\Site\NodeFactory;
|
||||||
use App\Manager\EntityManager;
|
use App\Manager\EntityManager;
|
||||||
use App\Repository\Site\NodeRepository;
|
use App\Repository\Site\NodeRepository;
|
||||||
|
use App\Slugify\CodeSlugify;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* class MenuEventSubscriber.
|
* class MenuEventSubscriber.
|
||||||
|
@ -18,16 +19,20 @@ use App\Repository\Site\NodeRepository;
|
||||||
class MenuEventSubscriber extends EntityManagerEventSubscriber
|
class MenuEventSubscriber extends EntityManagerEventSubscriber
|
||||||
{
|
{
|
||||||
protected NodeFactory $nodeFactory;
|
protected NodeFactory $nodeFactory;
|
||||||
|
protected NodeRepository $nodeRepository;
|
||||||
protected EntityManager $entityManager;
|
protected EntityManager $entityManager;
|
||||||
|
protected CodeSlugify $slugify;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
NodeFactory $nodeFactory,
|
NodeFactory $nodeFactory,
|
||||||
NodeRepository $nodeRepository,
|
NodeRepository $nodeRepository,
|
||||||
EntityManager $entityManager
|
EntityManager $entityManager,
|
||||||
|
CodeSlugify $slugify
|
||||||
) {
|
) {
|
||||||
$this->nodeFactory = $nodeFactory;
|
$this->nodeFactory = $nodeFactory;
|
||||||
$this->nodeRepository = $nodeRepository;
|
$this->nodeRepository = $nodeRepository;
|
||||||
$this->entityManager = $entityManager;
|
$this->entityManager = $entityManager;
|
||||||
|
$this->slugify = $slugify;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function support(EntityInterface $entity)
|
public function support(EntityInterface $entity)
|
||||||
|
@ -35,6 +40,16 @@ class MenuEventSubscriber extends EntityManagerEventSubscriber
|
||||||
return $entity instanceof Menu;
|
return $entity instanceof Menu;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function onPreUpdate(EntityManagerEvent $event)
|
||||||
|
{
|
||||||
|
if (!$this->support($event->getEntity())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$menu = $event->getEntity();
|
||||||
|
$menu->setCode($this->slugify->slugify($menu->getCode()));
|
||||||
|
}
|
||||||
|
|
||||||
public function onCreate(EntityManagerEvent $event)
|
public function onCreate(EntityManagerEvent $event)
|
||||||
{
|
{
|
||||||
if (!$this->support($event->getEntity())) {
|
if (!$this->support($event->getEntity())) {
|
||||||
|
@ -69,4 +84,9 @@ class MenuEventSubscriber extends EntityManagerEventSubscriber
|
||||||
{
|
{
|
||||||
return $this->onCreate($event);
|
return $this->onCreate($event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function onPreCreate(EntityManagerEvent $event)
|
||||||
|
{
|
||||||
|
return $this->onPreUpdate($event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
46
src/EventSuscriber/Site/NavigationEventSubscriber.php
Normal file
46
src/EventSuscriber/Site/NavigationEventSubscriber.php
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\EventSuscriber\Site;
|
||||||
|
|
||||||
|
use App\Entity\EntityInterface;
|
||||||
|
use App\Entity\Site\Navigation;
|
||||||
|
use App\Event\EntityManager\EntityManagerEvent;
|
||||||
|
use App\EventSuscriber\EntityManagerEventSubscriber;
|
||||||
|
use App\Manager\EntityManager;
|
||||||
|
use App\Slugify\CodeSlugify;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class NavigationEventSubscriber.
|
||||||
|
*
|
||||||
|
* @author Simon Vieille <simon@deblan.fr>
|
||||||
|
*/
|
||||||
|
class NavigationEventSubscriber extends EntityManagerEventSubscriber
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
EntityManager $entityManager,
|
||||||
|
CodeSlugify $slugify
|
||||||
|
) {
|
||||||
|
$this->entityManager = $entityManager;
|
||||||
|
$this->slugify = $slugify;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function support(EntityInterface $entity)
|
||||||
|
{
|
||||||
|
return $entity instanceof Navigation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onPreUpdate(EntityManagerEvent $event)
|
||||||
|
{
|
||||||
|
if (!$this->support($event->getEntity())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$menu = $event->getEntity();
|
||||||
|
$menu->setCode($this->slugify->slugify($menu->getCode()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onPreCreate(EntityManagerEvent $event)
|
||||||
|
{
|
||||||
|
return $this->onPreUpdate($event);
|
||||||
|
}
|
||||||
|
}
|
|
@ -14,6 +14,8 @@ use Symfony\Bundle\FrameworkBundle\Console\Application;
|
||||||
use Symfony\Component\Console\Input\ArrayInput;
|
use Symfony\Component\Console\Input\ArrayInput;
|
||||||
use Symfony\Component\Console\Output\BufferedOutput;
|
use Symfony\Component\Console\Output\BufferedOutput;
|
||||||
use Symfony\Component\HttpKernel\KernelInterface;
|
use Symfony\Component\HttpKernel\KernelInterface;
|
||||||
|
use App\Entity\Site\Menu;
|
||||||
|
use App\Entity\Site\Navigation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* class NodeEventSubscriber.
|
* class NodeEventSubscriber.
|
||||||
|
@ -90,6 +92,12 @@ class NodeEventSubscriber extends EntityManagerEventSubscriber
|
||||||
|
|
||||||
public function onUpdate(EntityManagerEvent $event)
|
public function onUpdate(EntityManagerEvent $event)
|
||||||
{
|
{
|
||||||
|
$entity = $event->getEntity();
|
||||||
|
|
||||||
|
if (!$entity instanceof Node && !$entity instanceof Menu && !$entity instanceof Navigation) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$application = new Application($this->kernel);
|
$application = new Application($this->kernel);
|
||||||
$application->setAutoExit(false);
|
$application->setAutoExit(false);
|
||||||
|
|
||||||
|
|
|
@ -45,6 +45,20 @@ class NodeType extends AbstractType
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$builder->add(
|
||||||
|
'code',
|
||||||
|
TextType::class,
|
||||||
|
[
|
||||||
|
'label' => 'Code',
|
||||||
|
'required' => false,
|
||||||
|
'help' => 'Sans espace, en minusule, sans caractère spécial',
|
||||||
|
'attr' => [
|
||||||
|
],
|
||||||
|
'constraints' => [
|
||||||
|
],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
$actions = [
|
$actions = [
|
||||||
'Nouvelle page' => 'new',
|
'Nouvelle page' => 'new',
|
||||||
'Associer à une page existante' => 'existing',
|
'Associer à une page existante' => 'existing',
|
||||||
|
|
|
@ -16,8 +16,11 @@ class NodeRepository extends NestedTreeRepository
|
||||||
public function urlExists($url, Node $node)
|
public function urlExists($url, Node $node)
|
||||||
{
|
{
|
||||||
$query = $this->createQueryBuilder('n')
|
$query = $this->createQueryBuilder('n')
|
||||||
|
->join('n.menu', 'm')
|
||||||
->where('n.url = :url')
|
->where('n.url = :url')
|
||||||
|
->andWhere('m.navigation = :navigation')
|
||||||
->setParameter(':url', $url)
|
->setParameter(':url', $url)
|
||||||
|
->setParameter(':navigation', $node->getMenu()->getNavigation())
|
||||||
;
|
;
|
||||||
|
|
||||||
if ($node->getId()) {
|
if ($node->getId()) {
|
||||||
|
|
|
@ -48,7 +48,7 @@ class SiteRouteLoader extends Loader
|
||||||
$route = new Route($node->getUrl(), $defaults, $requirements);
|
$route = new Route($node->getUrl(), $defaults, $requirements);
|
||||||
$route->setHost($navigation->getDomain());
|
$route->setHost($navigation->getDomain());
|
||||||
|
|
||||||
$routes->add('site_page_'.$node->getId(), $route);
|
$routes->add($node->getRouteName(), $route);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
26
src/Slugify/CodeSlugify.php
Normal file
26
src/Slugify/CodeSlugify.php
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Slugify;
|
||||||
|
|
||||||
|
use Cocur\Slugify\Slugify as BaseSlugify;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class CodeSlugify.
|
||||||
|
*
|
||||||
|
* @author Simon Vieille <simon@deblan.fr>
|
||||||
|
*/
|
||||||
|
class CodeSlugify extends Slugify
|
||||||
|
{
|
||||||
|
protected function create(): BaseSlugify
|
||||||
|
{
|
||||||
|
$slugify = new BaseSlugify([
|
||||||
|
'separator' => '_',
|
||||||
|
'lowercase' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$slugify->activateRuleSet('french');
|
||||||
|
$slugify->addRule("'", '');
|
||||||
|
|
||||||
|
return $slugify;
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,7 +20,7 @@ class Slugify
|
||||||
{
|
{
|
||||||
$slugify = new BaseSlugify([
|
$slugify = new BaseSlugify([
|
||||||
'separator' => '-',
|
'separator' => '-',
|
||||||
'lowercase' => false,
|
'lowercase' => true,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$slugify->activateRuleSet('french');
|
$slugify->activateRuleSet('french');
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
{{ form_row(form.position) }}
|
{{ form_row(form.position) }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<div class="accordion" id="node-page-action">
|
<div class="accordion mb-3" id="node-page-action">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
{% set action = form.pageAction[0] %}
|
{% set action = form.pageAction[0] %}
|
||||||
{% set options = not entity.id ? {'attr': {'checked': 'checked'}} : {} %}
|
{% set options = not entity.id ? {'attr': {'checked': 'checked'}} : {} %}
|
||||||
|
|
|
@ -10,6 +10,10 @@
|
||||||
<form action="{{ path('admin_site_node_edit', {entity: entity.id}) }}" id="form-node-edit" method="POST">
|
<form action="{{ path('admin_site_node_edit', {entity: entity.id}) }}" id="form-node-edit" method="POST">
|
||||||
{{ include('site/node_admin/_form.html.twig') }}
|
{{ include('site/node_admin/_form.html.twig') }}
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
<div class="p1">
|
||||||
|
Nom de la route : <code>{{ entity.routeName }}</code>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
<button type="button" class="btn btn-secondary" data-dismiss="modal">Annuler</button>
|
<button type="button" class="btn btn-secondary" data-dismiss="modal">Annuler</button>
|
||||||
|
|
|
@ -113,9 +113,11 @@
|
||||||
{{ node.label }}
|
{{ node.label }}
|
||||||
|
|
||||||
{% if node.url %}
|
{% if node.url %}
|
||||||
<a href="{{ node.url }}" target="_blank" class="ml-3 btn btn-sm btn-light">
|
<span class="ml-3 btn-group">
|
||||||
{{ node.url }}
|
<a href="{{ absolute_url(path(node.routeName)) }}" target="_blank" class="btn btn-sm border border-secondary btn-light">
|
||||||
</a>
|
{{ node.url }}
|
||||||
|
</a>
|
||||||
|
</span>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue