post
This commit is contained in:
@@ -6,15 +6,21 @@ use App\Repository\PostRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class PostController extends AbstractController
|
||||
{
|
||||
#[Route('/', name: 'app_posts')]
|
||||
public function index(PostRepository $repository): Response
|
||||
public function index(PostRepository $repository, Request $request): Response
|
||||
{
|
||||
$posts = $repository->findAll();
|
||||
$posts = $repository->findPaginatedPosts($request->query->getInt('page',1), 10);
|
||||
return $this->render('post/index.html.twig', [
|
||||
'posts' => $posts,
|
||||
'maxPosts' => $request->query->getInt('page',1) * 10,
|
||||
'currentPage' => $request->query->getInt('maxPosts',1) / 10
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -5,6 +5,7 @@ namespace App\Repository;
|
||||
use App\Entity\Post;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
use Doctrine\ORM\Tools\Pagination\Paginator;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Post>
|
||||
@@ -16,6 +17,14 @@ class PostRepository extends ServiceEntityRepository
|
||||
parent::__construct($registry, Post::class);
|
||||
}
|
||||
|
||||
public function findPaginatedPosts(int $page, int $limit): Paginator
|
||||
{
|
||||
$query = $this->createQueryBuilder('p')
|
||||
->setFirstResult(($page - 1) * $limit)
|
||||
->setMaxResults($limit);
|
||||
|
||||
return new Paginator($query, fetchJoinCollection: false);
|
||||
}
|
||||
// /**
|
||||
// * @return Post[] Returns an array of Post objects
|
||||
// */
|
||||
|
Reference in New Issue
Block a user