* @return Response The rendered view
*/
public function index(Request $request): Response {
- //With articles
- if ($count = $this->doctrine->getRepository(Article::class)->findCountAsInt()) {
- //Negative page or over page
- if (($page = (int) $request->get('page', 0)) < 0 || $page > $count / $this->limit) {
- //Throw 404
- throw $this->createNotFoundException($this->translator->trans('Unable to find articles (page: %page%)', ['%page%' => $page]));
- }
-
- //Without articles
- if (empty($this->context['articles'] = $this->doctrine->getRepository(Article::class)->findAllAsArray($page, $this->limit))) {
- //Throw 404
- throw $this->createNotFoundException($this->translator->trans('Unable to find articles'));
- }
-
- //With prev link
- if ($page > 0) {
- //Set articles older
- $this->context['head']['prev'] = $this->context['articles_prev'] = $this->generateUrl($request->attributes->get('_route'), ['page' => $page - 1]+$request->attributes->get('_route_params'));
- }
-
- //With next link
- if ($count > ($page + 1) * $this->limit) {
- //Set articles newer
- $this->context['head']['next'] = $this->context['articles_next'] = $this->generateUrl($request->attributes->get('_route'), ['page' => $page + 1]+$request->attributes->get('_route_params'));
- }
+ //With not enough articles
+ if (($this->count = $this->doctrine->getRepository(Article::class)->findCountAsInt()) < $this->page * $this->limit) {
+ //Throw 404
+ throw $this->createNotFoundException($this->translator->trans('Unable to find articles'));
+ }
+ //Get articles
+ if ($this->context['articles'] = $this->doctrine->getRepository(Article::class)->findAllAsArray($this->page, $this->limit)) {
//Set modified
$this->modified = max(array_map(function ($v) { return $v['modified']; }, $this->context['articles']));
- //Without articles
+ //Without keywords
} else {
- //Set empty articles
- $this->context['articles'] = [];
-
//Set empty modified
$this->modified = new \DateTime('-1 year');
}
);
//Set title
- $this->context['title'] = $this->translator->trans('Articles list');
+ $this->context['title']['page'] = $this->translator->trans('Articles list');
//Set description
$this->context['description'] = $this->translator->trans('Welcome to raphaël\'s developer diary article listing');
//With invalid slug
if ($slug !== $this->context['article']['slug']) {
//Redirect on correctly spelled article
- return $this->redirectToRoute('rapsys_blog_article_view', ['id' => $this->context['article']['id'], 'slug' => $this->context['article']['slug']], Response::HTTP_MOVED_PERMANENTLY);
+ return $this->redirectToRoute('rapsysblog_article_view', ['id' => $this->context['article']['id'], 'slug' => $this->context['article']['slug']], Response::HTTP_MOVED_PERMANENTLY);
}
//Set modified
}
}
- //Set keywords
- $this->context['head']['keywords'] = implode(
- ', ',
- array_map(
- function ($v) {
- return $v['title'];
- },
- $this->context['article']['keywords']
- )
- );
-
//Set keywords
$this->context['head']['keywords'] = implode(
', ',
);
//Set title
- $this->context['title'] = $this->context['article']['title'];
+ $this->context['title']['page'] = $this->context['article']['title'];
//Set description
$this->context['description'] = $this->context['article']['description'];