1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys BlogBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\BlogBundle\Controller
;
14 use Symfony\Component\HttpFoundation\Request
;
15 use Symfony\Component\HttpFoundation\Response
;
17 use Rapsys\BlogBundle\Entity\Article
;
18 use Rapsys\BlogBundle\Entity\Keyword
;
23 class ArticleController
extends AbstractController
{
29 * @param Request $request The request instance
30 * @return Response The rendered view
32 public function index(Request
$request): Response
{
33 //With not enough articles
34 if (($this->count
= $this->doctrine
->getRepository(Article
::class)->findCountAsInt()) < $this->page
* $this->limit
) {
36 throw $this->createNotFoundException($this->translator
->trans('Unable to find articles'));
40 if ($this->context
['articles'] = $this->doctrine
->getRepository(Article
::class)->findAllAsArray($this->page
, $this->limit
)) {
42 $this->modified
= max(array_map(function ($v) { return $v
['modified']; }, $this->context
['articles']));
46 $this->modified
= new \
DateTime('-1 year');
50 $response = new Response();
53 if ($this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
55 $response->setLastModified(new \
DateTime('-1 year'));
58 $response->setPrivate();
62 //XXX: only for public to force revalidation by last modified
63 $response->setEtag(md5(serialize($this->context
['articles'])));
66 $response->setLastModified($this->modified
);
69 $response->setPublic();
71 //Without role and modification
72 if ($response->isNotModified($request)) {
79 $this->context
['head']['keywords'] = implode(
81 //Use closure to extract each unique article keywords sorted
89 if (!empty($a['keywords'])) {
91 foreach($a['keywords'] as $k) {
93 $r[$k['title']] = $k['title'];
103 })($this->context
['articles'])
107 $this->context
['title'] = $this->translator
->trans('Articles list');
110 $this->context
['description'] = $this->translator
->trans('Welcome to raphaël\'s developer diary article listing');
113 return $this->render('@RapsysBlog/article/index.html.twig', $this->context
, $response);
119 * Display article and keywords
121 * @param Request $request The request instance
122 * @param integer $id The article id
123 * @param ?string $slug The article slug
124 * @return Response The rendered view
126 public function view(Request
$request, int $id, ?string $slug): Response
{
128 if (empty($this->context
['article'] = $this->doctrine
->getRepository(Article
::class)->findByIdAsArray($id))) {
130 throw $this->createNotFoundException($this->translator
->trans('Unable to find article: %id%', ['%id%' => $id]));
134 if ($slug !== $this->context
['article']['slug']) {
135 //Redirect on correctly spelled article
136 return $this->redirectToRoute('rapsys_blog_article_view', ['id' => $this->context
['article']['id'], 'slug' => $this->context
['article']['slug']], Response
::HTTP_MOVED_PERMANENTLY
);
140 $this->modified
= $this->context
['article']['modified'];
143 $response = new Response();
146 if ($this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
148 $response->setLastModified(new \
DateTime('-1 year'));
151 $response->setPrivate();
152 //Without logged user
155 //XXX: only for public to force revalidation by last modified
156 $response->setEtag(md5(serialize($this->context
['article'])));
159 $response->setLastModified($this->modified
);
162 $response->setPublic();
164 //Without role and modification
165 if ($response->isNotModified($request)) {
166 //Return 304 response
172 $this->context
['head']['keywords'] = implode(
174 //Use closure to extract each unique article keywords sorted
180 if (!empty($a['keywords'])) {
181 //Iterate on keywords
182 foreach($a['keywords'] as $k) {
184 $r[$k['title']] = $k['title'];
193 })($this->context
['article'])
197 $this->context
['title'] = $this->context
['article']['title'];
200 $this->context
['description'] = $this->context
['article']['description'];
203 return $this->render('@RapsysBlog/article/view.html.twig', $this->context
, $response);