Tai yra demo puslapis sukurtas su Symfony Framework parodydant tinkamą Symfony panaudojimą.
Norėdami gauti daugiau informacijos, peržiūrėkite Symfony dokumentciją.
Paspauskite mygtuką, kad peržiūrėti Controller ir šabloną panaudoti atvaizduojant šį puslapį.
public function search(Request $request, PostRepository $posts): Response
{
$query = $request->query->get('q', '');
$limit = $request->query->get('l', 10);
if (!$request->isXmlHttpRequest()) {
return $this->render('blog/search.html.twig', ['query' => $query]);
}
$foundPosts = $posts->findBySearchQuery($query, $limit);
$results = [];
foreach ($foundPosts as $post) {
$results[] = [
'title' => htmlspecialchars($post->getTitle(), \ENT_COMPAT | \ENT_HTML5),
'date' => $post->getPublishedAt()->format('M d, Y'),
'author' => htmlspecialchars($post->getAuthor()->getFullName(), \ENT_COMPAT | \ENT_HTML5),
'summary' => htmlspecialchars($post->getSummary(), \ENT_COMPAT | \ENT_HTML5),
'url' => $this->generateUrl('blog_post', ['slug' => $post->getSlug()]),
];
}
return $this->json($results);
}
{% extends 'base.html.twig' %}
{% block javascripts %}
{{ parent() }}
{{ encore_entry_script_tags('search') }}
{% endblock %}
{% block body_id 'blog_search' %}
{% block main %}
<form action="{{ path('blog_search') }}" method="get">
<div class="form-group">
<input name="q"
class="form-control search-field"
placeholder="{{ 'post.search_for'|trans }}"
autocomplete="off"
value="{{ query }}"
autofocus
data-no-results-message="{{ 'post.search_no_results'|trans }}"
>
</div>
</form>
<div id="results">
</div>
{% endblock %}
{% block sidebar %}
{{ parent() }}
{{ show_source_code(_self) }}
{% endblock %}