BlogController.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. ],
  14. 2 => [
  15. 'id' => '2',
  16. 'title' => 'Title 2',
  17. 'body' => 'Body 2',
  18. ],
  19. 3 => [
  20. 'id' => '3',
  21. 'title' => 'Title 3',
  22. 'body' => 'Body 3',
  23. ],
  24. ];
  25. /**
  26. * @param $page
  27. *
  28. * @return Response
  29. *
  30. * @Route(
  31. * name ="blog_list",
  32. * path ="/blog/{page}",
  33. * requirements={"page" = "\d+"}
  34. * )
  35. */
  36. public function list($page = 1) {
  37. return $this->render('blog/index.html.twig', [
  38. 'blog_entries' => static::POSTS,
  39. 'pager' => "Page $page of the blog list.",
  40. ]);
  41. }
  42. /**
  43. * @param $slug
  44. *
  45. * @return \Symfony\Component\HttpFoundation\Response
  46. *
  47. * @Route(
  48. * name = "blog_show",
  49. * path = "/blog/{slug}"
  50. * )
  51. */
  52. public function show($slug) {
  53. $path = $this->generateUrl('blog_list', [
  54. // Placeholder: fit in the path.
  55. 'page' => 2,
  56. // Non-placeholder: fit in the query
  57. 'foo' => 'bar',
  58. ], UrlGeneratorInterface::ABSOLUTE_PATH);
  59. return new Response("Show $slug. Back to page 2: $path");
  60. }
  61. }