@@ -160,31 +160,20 @@ public function commentFormAction(Post $post)
160
160
*/
161
161
public function searchAction (Request $ request )
162
162
{
163
- $ query = $ request ->query ->get ('q ' , '' );
164
-
165
- // Sanitizing the query: removes all non-alphanumeric characters except whitespaces
166
- $ query = preg_replace ('/[^[:alnum:] ]/ ' , '' , trim (preg_replace ('/[[:space:]]+/ ' , ' ' , $ query )));
167
-
168
- // Splits the query into terms and removes all terms which
169
- // length is less than 2
170
- $ terms = array_unique (explode (' ' , mb_strtolower ($ query )));
171
- $ terms = array_filter ($ terms , function ($ term ) {
172
- return 2 <= mb_strlen ($ term );
173
- });
174
-
175
- $ posts = [];
176
-
177
- if (!empty ($ terms )) {
178
- $ posts = $ this ->getDoctrine ()->getRepository (Post::class)->findByTerms ($ terms );
163
+ if (!$ request ->isXmlHttpRequest ()) {
164
+ return $ this ->render ('blog/search.html.twig ' );
179
165
}
180
166
181
- $ results = [];
167
+ $ query = $ request ->query ->get ('q ' , '' );
168
+ $ posts = $ this ->getDoctrine ()->getRepository (Post::class)->findBySearchQuery ($ query );
182
169
170
+ $ results = [];
183
171
foreach ($ posts as $ post ) {
184
- array_push ($ results , [
185
- 'result ' => htmlspecialchars ($ post ->getTitle ()),
172
+ $ results [] = [
173
+ 'title ' => htmlspecialchars ($ post ->getTitle ()),
174
+ 'summary ' => htmlspecialchars ($ post ->getSummary ()),
186
175
'url ' => $ this ->generateUrl ('blog_post ' , ['slug ' => $ post ->getSlug ()]),
187
- ]) ;
176
+ ];
188
177
}
189
178
190
179
return new JsonResponse ($ results );
0 commit comments