Conference URLs now use slugs instead of integer id
This commit is contained in:
@@ -21,7 +21,7 @@ class ConferenceController extends AbstractController
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/conference/{id}', name: 'conference')]
|
||||
#[Route('/conference/{slug}', name: 'conference')]
|
||||
public function show(Request $request, Conference $conference, CommentRepository $commentRepository): Response
|
||||
{
|
||||
$offset = max(0, $request->query->getInt('offset', 0));
|
||||
|
||||
@@ -6,8 +6,11 @@ use App\Repository\ConferenceRepository;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Doctrine\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||
use Symfony\Component\String\Slugger\SluggerInterface;
|
||||
|
||||
#[ORM\Entity(repositoryClass: ConferenceRepository::class)]
|
||||
#[UniqueEntity('slug')]
|
||||
class Conference
|
||||
{
|
||||
#[ORM\Id]
|
||||
@@ -30,6 +33,9 @@ class Conference
|
||||
#[ORM\OneToMany(targetEntity: Comment::class, mappedBy: 'conference', orphanRemoval: true)]
|
||||
private Collection $comments;
|
||||
|
||||
#[ORM\Column(length: 255, unique: true)]
|
||||
private ?string $slug = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->comments = new ArrayCollection();
|
||||
@@ -44,6 +50,13 @@ class Conference
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function computeSlug(SluggerInterface $slugger)
|
||||
{
|
||||
if(!$this->slug || '-' === $this->slug) {
|
||||
$this->slug = (string)$slugger->slug((string)$this)->lower();
|
||||
}
|
||||
}
|
||||
|
||||
public function getCity(): ?string
|
||||
{
|
||||
return $this->city;
|
||||
@@ -109,4 +122,16 @@ class Conference
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSlug(): ?string
|
||||
{
|
||||
return $this->slug;
|
||||
}
|
||||
|
||||
public function setSlug(string $slug): static
|
||||
{
|
||||
$this->slug = $slug;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
29
src/EntityListener/ConferenceEntityListener.php
Normal file
29
src/EntityListener/ConferenceEntityListener.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\EntityListener;
|
||||
|
||||
use App\Entity\Conference;
|
||||
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
|
||||
use Doctrine\ORM\Events;
|
||||
use Doctrine\Persistence\Event\LifecycleEventArgs;
|
||||
use Symfony\Component\String\Slugger\SluggerInterface;
|
||||
|
||||
#[AsEntityListener(event: Events::prePersist, entity: Conference::class)]
|
||||
#[AsEntityListener(event: Events::preUpdate, entity: Conference::class)]
|
||||
class ConferenceEntityListener
|
||||
{
|
||||
public function __construct(
|
||||
private SluggerInterface $slugger,
|
||||
) {
|
||||
}
|
||||
|
||||
public function prePersist(Conference $conference, LifecycleEventArgs $event)
|
||||
{
|
||||
$conference->computeSlug($this->slugger);
|
||||
}
|
||||
|
||||
public function preUpdate(Conference $conference, LifecycleEventArgs $event)
|
||||
{
|
||||
$conference->computeSlug($this->slugger);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user