LuckyController.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Controller;
  3. use Psr\Log\LoggerInterface;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. class LuckyController extends Controller {
  9. /**
  10. * @Route(
  11. * name="lucky-number",
  12. * path="/lucky/number"
  13. * )
  14. *
  15. * @see config/services.yaml
  16. *
  17. * Notice how we overrode auto-wiring to receive an instance of App\Logger.
  18. */
  19. public function number(LoggerInterface $logger, Request $request) {
  20. $number = $request->query->has('value')
  21. ? (int) $request->query->get('value')
  22. : mt_rand(0, 100);
  23. $logger->notice("We sent $number");
  24. return $this->render("lucky/number.html.twig", [
  25. "number" => $number,
  26. ]);
  27. }
  28. /**
  29. * @\Symfony\Component\Routing\Annotation\Route(
  30. * name = "lucky-not-found",
  31. * path = "/lucky/not-found"
  32. * )
  33. */
  34. public function notFound() {
  35. $notFound = $this->createNotFoundException();
  36. throw $this->createAccessDeniedException("Denied because it was not found", $notFound);
  37. }
  38. /**
  39. * @\Symfony\Component\Routing\Annotation\Route(
  40. * name = "lucky-denied",
  41. * path = "/lucky/denied"
  42. * )
  43. */
  44. public function denied() {
  45. $denied = $this->createAccessDeniedException();
  46. throw $this->createNotFoundException("Not found because it was denied", $denied);
  47. }
  48. }