1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace App\Controller;
- use App\GreetingGenerator;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- use Symfony\Component\HttpFoundation\Response;
- class DefaultController extends AbstractController {
- /**
- * @return \Symfony\Component\HttpFoundation\Response
- *
- * @Route("/",
- * name="front"
- * )
- * @Route("/hello",
- * name="front_hello"
- * )
- */
- public function index() {
- return $this->render('hello.html.twig', ["message" => "Hello!"]);
- }
- /**
- * @return \Symfony\Component\HttpFoundation\RedirectResponse
- *
- * @\Symfony\Component\Routing\Annotation\Route(
- * name = "front_too",
- * path = "/front"
- * )
- */
- public function frontToo() {
- // As per RFC 7231 § 7.1.2, relative URLs are allowed. Try /front#foo !
- // Never use a user-supplied URL without integrity checks:
- // https://www.owasp.org/index.php/Open_redirect
- // That's why Drupal has TrustedRedirectResponse and checks.
- return $this->redirect('/');
- }
- /**
- * @return \Symfony\Component\HttpFoundation\RedirectResponse
- *
- * @\Symfony\Component\Routing\Annotation\Route(
- * name = "blog_42",
- * path = "/question"
- * )
- */
- public function blog42() {
- // Notice how the matching route will actually be blog_list, because once
- // the redirect is sent, the browser has no notion of the initially
- // requested route name and parameters, and routing just receives an URL,
- // matching it without knowledge of the initial URL generation choices.
- return $this->redirectToRoute('blog_show', ['slug' => 42]);
- }
- /**
- * @param $name
- * @param \App\GreetingGenerator $generator
- *
- * @return \Symfony\Component\HttpFoundation\Response
- *
- * @Route(
- * name="hello_html",
- * path="/hello/{name}"
- * )
- */
- public function hello($name, GreetingGenerator $generator) {
- $greeting = $generator->getRandomGreeting();
- return new Response("$greeting $name");
- }
- /**
- * @param $name
- *
- * @return \Symfony\Component\HttpFoundation\JsonResponse
- *
- * @Route("/json/{name}",
- * name="hello_json",
- * )
- */
- public function helloJson($name) {
- return $this->json([
- 'name' => $name,
- ]);
- }
- }
|