LuckyController.php 1.4 KB

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