diff --git a/migrations/Version20240728050727.php b/migrations/Version20240728050727.php new file mode 100644 index 0000000..ca2f30f --- /dev/null +++ b/migrations/Version20240728050727.php @@ -0,0 +1,34 @@ +addSql('ALTER TABLE conference ADD slug VARCHAR(255)'); + $this->addSql("UPDATE conference SET slug=CONCAT(LOWER(city), '-', year)"); + $this->addSql('ALTER TABLE conference ALTER COLUMN slug SET NOT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('ALTER TABLE conference DROP slug'); + } +} diff --git a/migrations/Version20240728051153.php b/migrations/Version20240728051153.php new file mode 100644 index 0000000..4b06d08 --- /dev/null +++ b/migrations/Version20240728051153.php @@ -0,0 +1,32 @@ +addSql('CREATE UNIQUE INDEX UNIQ_911533C8989D9B62 ON conference (slug)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('DROP INDEX UNIQ_911533C8989D9B62'); + } +} diff --git a/src/Controller/ConferenceController.php b/src/Controller/ConferenceController.php index c6ed7c5..171698b 100644 --- a/src/Controller/ConferenceController.php +++ b/src/Controller/ConferenceController.php @@ -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)); diff --git a/src/Entity/Conference.php b/src/Entity/Conference.php index 65116f8..0246965 100644 --- a/src/Entity/Conference.php +++ b/src/Entity/Conference.php @@ -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; + } } diff --git a/src/EntityListener/ConferenceEntityListener.php b/src/EntityListener/ConferenceEntityListener.php new file mode 100644 index 0000000..0ad9189 --- /dev/null +++ b/src/EntityListener/ConferenceEntityListener.php @@ -0,0 +1,29 @@ +computeSlug($this->slugger); + } + + public function preUpdate(Conference $conference, LifecycleEventArgs $event) + { + $conference->computeSlug($this->slugger); + } +} diff --git a/templates/base.html.twig b/templates/base.html.twig index 7a2838c..081ba7f 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -16,7 +16,7 @@

Guestbook


diff --git a/templates/conference/index.html.twig b/templates/conference/index.html.twig index 57db8a9..2e170ea 100644 --- a/templates/conference/index.html.twig +++ b/templates/conference/index.html.twig @@ -8,7 +8,7 @@ {% for conference in conferences %}

{{ conference }}

- View + View

{% endfor %} {% endblock %} diff --git a/templates/conference/show.html.twig b/templates/conference/show.html.twig index 4fcec9d..0e7d4dc 100644 --- a/templates/conference/show.html.twig +++ b/templates/conference/show.html.twig @@ -22,10 +22,10 @@ {% endfor %} {% if previous >= 0 %} - Previous + Previous {% endif %} {% if next < comments|length %} - Next + Next {% endif %} {% else %}
No comments have been posted yet for this conference.