new
new
Choose your language
Demo aplikazio hau Symfony frameworkarekin programatu da Symfony aplikazioak programatzeko modu gomendatua erakusteko.
Informazio gehiago nahi izanez gero, Symfonyren dokumentazioa kontsultatu.
Sakatu botoi hau kontrolatzailearen eta txantiloiaren iturburu-kodea ikusteko.
src/Controller/BlogController.php at line 51
/**
* NOTE: For standard formats, Symfony will also automatically choose the best
* Content-Type header for the response.
*
* See https://symfony.com/doc/current/routing.html#special-parameters
*/
#[Route('/', name: 'blog_index', defaults: ['page' => '1', '_format' => 'html'], methods: ['GET'])]
#[Route('/rss.xml', name: 'blog_rss', defaults: ['page' => '1', '_format' => 'xml'], methods: ['GET'])]
#[Route('/page/{page<[1-9]\d{0,8}>}', name: 'blog_index_paginated', defaults: ['_format' => 'html'], methods: ['GET'])]
#[Cache(smaxage: 10)]
public function index(Request $request, int $page, string $_format, PostRepository $posts, TagRepository $tags): Response
{
$tag = null;
if ($request->query->has('tag')) {
$tag = $tags->findOneBy(['name' => $request->query->get('tag')]);
}
$latestPosts = $posts->findLatest($page, $tag);
// Every template name also has two extensions that specify the format and
// engine for that template.
// See https://symfony.com/doc/current/templates.html#template-naming
return $this->render('blog/index.'.$_format.'.twig', [
'paginator' => $latestPosts,
'tagName' => $tag?->getName(),
]);
}
templates/blog/index.html.twig at line 1
{% extends 'base.html.twig' %}
{% block body_id 'blog_index' %}
{% block main %}
{% for post in paginator.results %}
{{ include('blog/_post.html.twig') }}
{% else %}
<div class="jumbotron">{{ 'post.no_posts_found'|trans }}</div>
{% endfor %}
{% if paginator.hasToPaginate %}
<div class="navigation text-center">
<ul class="pagination pagination-lg">
{% if paginator.hasPreviousPage %}
<li class="page-item">
<a class="page-link" href="{{ path('blog_index_paginated', {page: paginator.previousPage, tag: tagName}) }}" rel="previous">
<i class="fa fw fa-long-arrow-left"></i> {{ 'paginator.previous'|trans }}
</a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link"><i class="fa fw fa-arrow-left"></i> {{ 'paginator.previous'|trans }}</span>
</li>
{% endif %}
{% for i in 1..paginator.lastPage %}
{% if i == paginator.currentPage %}
<li class="page-item active">
<span class="page-link">{{ i }} <span class="sr-only">{{ 'paginator.current'|trans }}</span></span>
</li>
{% else %}
<li class="page-item"><a class="page-link" href="{{ path('blog_index_paginated', {page: i, tag: tagName}) }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if paginator.hasNextPage %}
<li class="page-item">
<a class="page-link" href="{{ path('blog_index_paginated', {page: paginator.nextPage, tag: tagName}) }}">
<span>{{ 'paginator.next'|trans }} <i class="fa fw fa-long-arrow-right"></i></span>
</a>
</li>
{% else %}
<li class="page-item disabled">
<span class="page-link">{{ 'paginator.next'|trans }} <i class="fa fw fa-long-arrow-right"></i></span>
</li>
{% endif %}
</ul>
</div>
{% endif %}
{% endblock %}
{% block sidebar %}
{{ parent() }}
{{ show_source_code(_self) }}
{{ include('blog/_rss.html.twig') }}
{% endblock %}