|
15 | 15 | use App\Entity\Post;
|
16 | 16 | use App\Events;
|
17 | 17 | use App\Form\CommentType;
|
| 18 | +use App\Repository\PostRepository; |
18 | 19 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
|
19 | 20 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
|
20 | 21 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
@@ -47,15 +48,14 @@ class BlogController extends AbstractController
|
47 | 48 | * Content-Type header for the response.
|
48 | 49 | * See https://symfony.com/doc/current/quick_tour/the_controller.html#using-formats
|
49 | 50 | */
|
50 |
| - public function index(int $page, string $_format): Response |
| 51 | + public function index(int $page, string $_format, PostRepository $posts): Response |
51 | 52 | {
|
52 |
| - $em = $this->getDoctrine()->getManager(); |
53 |
| - $posts = $em->getRepository(Post::class)->findLatest($page); |
| 53 | + $latestPosts = $posts->findLatest($page); |
54 | 54 |
|
55 | 55 | // Every template name also has two extensions that specify the format and
|
56 | 56 | // engine for that template.
|
57 | 57 | // See https://symfony.com/doc/current/templating.html#template-suffix
|
58 |
| - return $this->render('blog/index.'.$_format.'.twig', ['posts' => $posts]); |
| 58 | + return $this->render('blog/index.'.$_format.'.twig', ['posts' => $latestPosts]); |
59 | 59 | }
|
60 | 60 |
|
61 | 61 | /**
|
@@ -148,18 +148,18 @@ public function commentForm(Post $post): Response
|
148 | 148 | * @Route("/search", name="blog_search")
|
149 | 149 | * @Method("GET")
|
150 | 150 | */
|
151 |
| - public function search(Request $request): Response |
| 151 | + public function search(Request $request, PostRepository $posts): Response |
152 | 152 | {
|
153 | 153 | if (!$request->isXmlHttpRequest()) {
|
154 | 154 | return $this->render('blog/search.html.twig');
|
155 | 155 | }
|
156 | 156 |
|
157 | 157 | $query = $request->query->get('q', '');
|
158 | 158 | $limit = $request->query->get('l', 10);
|
159 |
| - $posts = $this->getDoctrine()->getRepository(Post::class)->findBySearchQuery($query, $limit); |
| 159 | + $foundPosts = $posts->findBySearchQuery($query, $limit); |
160 | 160 |
|
161 | 161 | $results = [];
|
162 |
| - foreach ($posts as $post) { |
| 162 | + foreach ($foundPosts as $post) { |
163 | 163 | $results[] = [
|
164 | 164 | 'title' => htmlspecialchars($post->getTitle()),
|
165 | 165 | 'date' => $post->getPublishedAt()->format('M d, Y'),
|
|
0 commit comments