From ba715dc577005f648afc69573c18d1417693050a Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 14 Jan 2021 13:24:19 +0100 Subject: [PATCH] Home page --- migrations/Version20210114120206.php | 31 ++++++++++++++++++++++++++++ src/Controller/HomeController.php | 2 ++ src/Controller/PostController.php | 8 +++++-- src/Entity/Post.php | 2 +- templates/base.html.twig | 14 +++++++++---- templates/home/index.html.twig | 27 ++++++++++++------------ templates/post/index.html.twig | 15 +------------- 7 files changed, 64 insertions(+), 35 deletions(-) create mode 100644 migrations/Version20210114120206.php diff --git a/migrations/Version20210114120206.php b/migrations/Version20210114120206.php new file mode 100644 index 0000000..a0a8732 --- /dev/null +++ b/migrations/Version20210114120206.php @@ -0,0 +1,31 @@ +addSql('CREATE UNIQUE INDEX UNIQ_5A8A6C8D989D9B62 ON post (slug)'); + } + + public function down(Schema $schema) : void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP INDEX UNIQ_5A8A6C8D989D9B62 ON post'); + } +} diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 365ae8e..0e56c10 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -2,6 +2,7 @@ namespace App\Controller; +use App\Entity\Post; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; @@ -15,6 +16,7 @@ class HomeController extends AbstractController { return $this->render('home/index.html.twig', [ 'controller_name' => 'HomeController', + 'posts' => $this->getDoctrine()->getRepository(Post::class)->findBy(array(), array('publishedAt' => 'DESC'), 5, 0) ]); } } diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index 7eab068..29241b2 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -2,6 +2,7 @@ namespace App\Controller; +use App\Entity\Post; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; @@ -9,12 +10,15 @@ use Symfony\Component\Routing\Annotation\Route; class PostController extends AbstractController { /** - * @Route("/post", name="post") + * @Route("/post/{slug}", name="post") + * @param string $slug + * @return Response */ - public function index(): Response + public function index(string $slug): Response { return $this->render('post/index.html.twig', [ 'controller_name' => 'PostController', + 'post' => $this->getDoctrine()->getRepository(Post::class)->findOneBy(array('slug' => $slug)) ]); } } diff --git a/src/Entity/Post.php b/src/Entity/Post.php index 97c634d..948b276 100644 --- a/src/Entity/Post.php +++ b/src/Entity/Post.php @@ -35,7 +35,7 @@ class Post private $content; /** - * @ORM\Column(type="string", length=255) + * @ORM\Column(type="string", length=255, unique=true) */ private $slug; diff --git a/templates/base.html.twig b/templates/base.html.twig index 92d8ae3..4f9afb3 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -6,12 +6,13 @@ {% block stylesheets %} {{ encore_entry_link_tags('app') }} {% endblock %} + {% set admin = false %}