Skip to content

Commit 4754bea

Browse files
committed
Use MapEntity for get Tag entity
1 parent 1eafe9a commit 4754bea

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/Controller/BlogController.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
use App\Entity\Comment;
1515
use App\Entity\Post;
16+
use App\Entity\Tag;
1617
use App\Entity\User;
1718
use App\Event\CommentCreatedEvent;
1819
use App\Form\CommentType;
1920
use App\Repository\PostRepository;
20-
use App\Repository\TagRepository;
2121
use Doctrine\ORM\EntityManagerInterface;
2222
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
2323
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -49,14 +49,13 @@ final class BlogController extends AbstractController
4949
#[Route('/rss.xml', name: 'blog_rss', defaults: ['page' => '1', '_format' => 'xml'], methods: ['GET'])]
5050
#[Route('/page/{page}', name: 'blog_index_paginated', defaults: ['_format' => 'html'], requirements: ['page' => Requirement::POSITIVE_INT], methods: ['GET'])]
5151
#[Cache(smaxage: 10)]
52-
public function index(Request $request, int $page, string $_format, PostRepository $posts, TagRepository $tags): Response
53-
{
54-
$tag = null;
55-
56-
if ($request->query->has('tag')) {
57-
$tag = $tags->findOneBy(['name' => $request->query->get('tag')]);
58-
}
59-
52+
public function index(
53+
int $page,
54+
string $_format,
55+
PostRepository $posts,
56+
#[MapEntity(expr: 'request.query.has("tag") ? repository.findOneBy({"name": request.query.get("tag")}) : null')]
57+
?Tag $tag,
58+
): Response {
6059
$latestPosts = $posts->findLatest($page, $tag);
6160

6261
// Every template name also has two extensions that specify the format and

0 commit comments

Comments
 (0)