<?php
namespace App\Controller\Front;
use App\Entity\Post;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use DateTime;
class BlogController extends AbstractController
{
/**
* @Route("/blog", name="page_blog")
* Method ({"GET", "POST"})
*/
public function index(Request $request): Response
{
$em = $this->getDoctrine()->getManager();
$page = $request->get('page', 1);
$limit = $request->get('limit', 9);
$keywords = $request->get('keywords', '');
$date = ($request->get('date', null)) ? DateTime::createFromFormat('d/m/Y', $request->get('date', null))->setTime(0, 0, 0) : null;
$posts = $em->getRepository(Post::class)->search($page, $limit, $keywords, $date);
return $this->render('front/blog/index.html.twig', [
'posts' => $posts,
'page' => ($page+1),
'limit' => $limit
]);
}
/**
* @Route("/blog-paginate", name="page_blog_paginate", options={"expose"=true})
* Method ({"GET", "POST"})
*/
public function paginate(Request $request): Response
{
$em = $this->getDoctrine()->getManager();
$page = $request->get('page', 1);
$limit = $request->get('limit', 3);
$keywords = $request->get('keywords', '');
$date = ($request->get('date', null)) ? DateTime::createFromFormat('d/m/Y', $request->get('date', null))->setTime(0, 0, 0) : null;
$posts = $em->getRepository(Post::class)->search($page, $limit, $keywords, $date);
return $this->render('front/blog/blog_ajax.html.twig', [
'posts' => $posts
]);
}
/**
* @Route("/blog/{slug}", name="page_blog_single")
* Method ({"GET", "POST"})
*/
public function show($slug): Response
{
$em = $this->getDoctrine()->getManager();
$post = $em->getRepository(Post::class)->findOneBySlug($slug);
return $this->render('front/blog/show.html.twig', [
'post' => $post
]);
}
}