DefaultController.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Controller;
  3. use App\GreetingGenerator;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Response;
  7. class DefaultController extends AbstractController {
  8. /**
  9. * @return \Symfony\Component\HttpFoundation\Response
  10. *
  11. * @Route("/",
  12. * name="front"
  13. * )
  14. * @Route("/hello",
  15. * name="front_hello"
  16. * )
  17. */
  18. public function index() {
  19. return $this->render('hello.html.twig', ["message" => "Hello!"]);
  20. }
  21. /**
  22. * @return \Symfony\Component\HttpFoundation\RedirectResponse
  23. *
  24. * @\Symfony\Component\Routing\Annotation\Route(
  25. * name = "front_too",
  26. * path = "/front"
  27. * )
  28. */
  29. public function frontToo() {
  30. // As per RFC 7231 § 7.1.2, relative URLs are allowed. Try /front#foo !
  31. // Never use a user-supplied URL without integrity checks:
  32. // https://www.owasp.org/index.php/Open_redirect
  33. // That's why Drupal has TrustedRedirectResponse and checks.
  34. return $this->redirect('/');
  35. }
  36. /**
  37. * @return \Symfony\Component\HttpFoundation\RedirectResponse
  38. *
  39. * @\Symfony\Component\Routing\Annotation\Route(
  40. * name = "blog_42",
  41. * path = "/question"
  42. * )
  43. */
  44. public function blog42() {
  45. // Notice how the matching route will actually be blog_list, because once
  46. // the redirect is sent, the browser has no notion of the initially
  47. // requested route name and parameters, and routing just receives an URL,
  48. // matching it without knowledge of the initial URL generation choices.
  49. return $this->redirectToRoute('blog_show', ['slug' => 42]);
  50. }
  51. /**
  52. * @param $name
  53. * @param \App\GreetingGenerator $generator
  54. *
  55. * @return \Symfony\Component\HttpFoundation\Response
  56. *
  57. * @Route(
  58. * name="hello_html",
  59. * path="/hello/{name}"
  60. * )
  61. */
  62. public function hello($name, GreetingGenerator $generator) {
  63. $greeting = $generator->getRandomGreeting();
  64. return new Response("$greeting $name");
  65. }
  66. /**
  67. * @param $name
  68. *
  69. * @return \Symfony\Component\HttpFoundation\JsonResponse
  70. *
  71. * @Route("/json/{name}",
  72. * name="hello_json",
  73. * )
  74. */
  75. public function helloJson($name) {
  76. return $this->json([
  77. 'name' => $name,
  78. ]);
  79. }
  80. }