Skip to content

Commit c6803f0

Browse files
committed
Filter posts by tag
1 parent 72eb366 commit c6803f0

File tree

5 files changed

+29
-20
lines changed

5 files changed

+29
-20
lines changed

src/Controller/BlogController.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use App\Events;
1717
use App\Form\CommentType;
1818
use App\Repository\PostRepository;
19+
use App\Repository\TagRepository;
1920
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
2021
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
2122
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
@@ -46,9 +47,13 @@ class BlogController extends AbstractController
4647
* Content-Type header for the response.
4748
* See https://symfony.com/doc/current/quick_tour/the_controller.html#using-formats
4849
*/
49-
public function index(int $page, string $_format, PostRepository $posts): Response
50+
public function index(Request $request, int $page, string $_format, PostRepository $posts, TagRepository $tags): Response
5051
{
51-
$latestPosts = $posts->findLatest($page);
52+
$tag = null;
53+
if ($request->query->has('tag')) {
54+
$tag = $tags->findOneBy(['name' => $request->query->get('tag')]);
55+
}
56+
$latestPosts = $posts->findLatest($page, $tag);
5257

5358
// Every template name also has two extensions that specify the format and
5459
// engine for that template.

src/Repository/PostRepository.php

+16-14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace App\Repository;
1313

1414
use App\Entity\Post;
15+
use App\Entity\Tag;
1516
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
1617
use Doctrine\Common\Persistence\ManagerRegistry;
1718
use Doctrine\ORM\Query;
@@ -35,21 +36,22 @@ public function __construct(ManagerRegistry $registry)
3536
parent::__construct($registry, Post::class);
3637
}
3738

38-
public function findLatest(int $page = 1): Pagerfanta
39+
public function findLatest(int $page = 1, Tag $tag = null): Pagerfanta
3940
{
40-
$query = $this->getEntityManager()
41-
->createQuery('
42-
SELECT p, a, t
43-
FROM App:Post p
44-
JOIN p.author a
45-
LEFT JOIN p.tags t
46-
WHERE p.publishedAt <= :now
47-
ORDER BY p.publishedAt DESC
48-
')
49-
->setParameter('now', new \DateTime())
50-
;
51-
52-
return $this->createPaginator($query, $page);
41+
$qb = $this->createQueryBuilder('p')
42+
->addSelect('a', 't')
43+
->innerJoin('p.author', 'a')
44+
->leftJoin('p.tags', 't')
45+
->where('p.publishedAt <= :now')
46+
->orderBy('p.publishedAt', 'DESC')
47+
->setParameter('now', new \DateTime());
48+
49+
if (null !== $tag) {
50+
$qb->andWhere(':tag MEMBER OF p.tags')
51+
->setParameter('tag', $tag);
52+
}
53+
54+
return $this->createPaginator($qb->getQuery(), $page);
5355
}
5456

5557
private function createPaginator(Query $query, int $page): Pagerfanta

templates/blog/_post_tags.html.twig

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{% if not post.tags.empty %}
22
<p class="post-tags">
33
{% for tag in post.tags %}
4-
<span class="label label-default">
4+
<a href="{{ path('blog_index', {'tag': tag.name}) }}"
5+
class="label label-{{ tag.name == app.request.query.get('tag') ? 'success' : 'default' }}"
6+
>
57
<i class="fa fa-tag"></i> {{ tag.name }}
6-
</span>
8+
</a>
79
{% endfor %}
810
</p>
911
{% endif %}

templates/blog/_rss.html.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="section rss">
2-
<a href="{{ path('blog_rss') }}">
2+
<a href="{{ path('blog_rss', app.request.query.all) }}">
33
<i class="fa fa-rss" aria-hidden="true"></i> {{ 'menu.rss'|trans }}
44
</a>
55
</div>

templates/blog/index.html.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
{% if posts.haveToPaginate %}
2828
<div class="navigation text-center">
29-
{{ pagerfanta(posts, 'twitter_bootstrap3_translated', {routeName: 'blog_index_paginated'}) }}
29+
{{ pagerfanta(posts, 'twitter_bootstrap3_translated', {routeName: 'blog_index_paginated', routeParams: app.request.query.all}) }}
3030
</div>
3131
{% endif %}
3232
{% endblock %}

0 commit comments

Comments
 (0)