BlogController.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  7. class BlogController extends Controller {
  8. const POSTS = [
  9. 1 => [
  10. 'id' => '1',
  11. 'title' => 'Title 1',
  12. 'body' => 'Body 1',
  13. 'authorName' => 'Fred',
  14. ],
  15. 2 => [
  16. 'id' => '2',
  17. 'title' => 'Title 2',
  18. 'body' => 'Body 2',
  19. 'authorName' => 'Outi',
  20. ],
  21. 3 => [
  22. 'id' => '3',
  23. 'title' => 'Title 3',
  24. 'body' => 'Body 3',
  25. 'authorName' => 'François',
  26. ],
  27. ];
  28. /**
  29. * @param $page
  30. *
  31. * @return Response
  32. *
  33. * @Route(
  34. * name ="blog_list",
  35. * path ="/blog/{page}",
  36. * requirements={"page" = "\d+"}
  37. * )
  38. */
  39. public function list($page = 1) {
  40. return $this->render('blog/index.html.twig', [
  41. 'blog_entries' => static::POSTS,
  42. 'pager' => "Page $page of the blog list.",
  43. ]);
  44. }
  45. /**
  46. * @param $slug
  47. *
  48. * @return \Symfony\Component\HttpFoundation\Response
  49. *
  50. * @Route(
  51. * name = "blog_show",
  52. * path = "/blog/{slug}"
  53. * )
  54. */
  55. public function show($slug) {
  56. $path = $this->generateUrl('blog_list', [
  57. // Placeholder: fit in the path.
  58. 'page' => 2,
  59. // Non-placeholder: fit in the query
  60. 'foo' => 'bar',
  61. ], UrlGeneratorInterface::ABSOLUTE_PATH);
  62. return new Response("Show $slug. Back to page 2: $path");
  63. }
  64. }