From 608872d9c4682174742f67f19ea215f0de06dd28 Mon Sep 17 00:00:00 2001 From: AchillesKal Date: Sun, 7 Apr 2024 17:37:34 +0300 Subject: [PATCH] Add tag slug --- migrations/Version20240407143332.php | 28 ++++++++++++++++++++++++++++ src/Controller/TagController.php | 2 +- src/Entity/Tag.php | 17 +++++++++++++++++ templates/tag/index.html.twig | 2 +- 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 migrations/Version20240407143332.php diff --git a/migrations/Version20240407143332.php b/migrations/Version20240407143332.php new file mode 100644 index 0000000..9c66eea --- /dev/null +++ b/migrations/Version20240407143332.php @@ -0,0 +1,28 @@ +addSql('ALTER TABLE tag ADD slug VARCHAR(255) NOT NULL'); + $this->addSql('CREATE UNIQUE INDEX UNIQ_389B783989D9B62 ON tag (slug)'); + } + + public function down(Schema $schema): void + { + $this->addSql('DROP INDEX UNIQ_389B783989D9B62'); + $this->addSql('ALTER TABLE tag DROP slug'); + } +} diff --git a/src/Controller/TagController.php b/src/Controller/TagController.php index bd8b24a..05fce51 100644 --- a/src/Controller/TagController.php +++ b/src/Controller/TagController.php @@ -41,7 +41,7 @@ public function new(Request $request, EntityManagerInterface $entityManager): Re ]); } - #[Route('/{id}', name: 'app_tag_show', methods: ['GET'])] + #[Route('/{slug}', name: 'app_tag_show', methods: ['GET'])] public function show(Tag $tag): Response { return $this->render('tag/show.html.twig', [ diff --git a/src/Entity/Tag.php b/src/Entity/Tag.php index a151a0f..c6db17f 100644 --- a/src/Entity/Tag.php +++ b/src/Entity/Tag.php @@ -7,6 +7,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; +use Gedmo\Mapping\Annotation\Slug; #[ORM\Entity(repositoryClass: TagRepository::class)] class Tag @@ -21,6 +22,10 @@ class Tag #[ORM\Column(length: 255)] private string $title; + #[ORM\Column(length: 255, unique: true)] + #[Slug(fields: ['title'])] + private string $slug; + #[ORM\ManyToMany(targetEntity: BlogPost::class, mappedBy: 'tags')] private Collection $blogPosts; @@ -47,6 +52,18 @@ public function setTitle(string $title): static return $this; } + public function getSlug(): string + { + return $this->slug; + } + + public function setSlug(string $slug): static + { + $this->slug = $slug; + + return $this; + } + /** * @return Collection */ diff --git a/templates/tag/index.html.twig b/templates/tag/index.html.twig index c652ecb..bbf3efd 100644 --- a/templates/tag/index.html.twig +++ b/templates/tag/index.html.twig @@ -23,7 +23,7 @@ {{ tag.createdAt ? tag.createdAt|date('Y-m-d H:i:s') : '' }} {{ tag.updatedAt ? tag.updatedAt|date('Y-m-d H:i:s') : '' }} - show + show edit