Aquesta aplicació demo ha estat programada amb el framework Symfony per mostrar la forma recomenada de programar aplicacions Symfony.
Per a més informació, consulta la documentació de Symfony.
Prem aquest botó per veure el codi font del controlador i de la plantilla utilitzats per crear aquesta pàgina.
/**
* @Route("/search", name="blog_search")
* @Method("GET")
*
* @return Response|JsonResponse
*/
public function searchAction(Request $request)
{
if (!$request->isXmlHttpRequest()) {
return $this->render('blog/search.html.twig');
}
$query = $request->query->get('q', '');
$posts = $this->getDoctrine()->getRepository(Post::class)->findBySearchQuery($query);
$results = [];
foreach ($posts as $post) {
$results[] = [
'title' => htmlspecialchars($post->getTitle()),
'summary' => htmlspecialchars($post->getSummary()),
'url' => $this->generateUrl('blog_post', ['slug' => $post->getSlug()]),
];
}
return $this->json($results);
}