1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- namespace App\Controller;
- use Psr\Log\LoggerInterface;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Session\SessionInterface;
- class LuckyController extends Controller {
- /**
- * @Route(
- * name="lucky-number",
- * path="/lucky/number",
- * methods={"GET", "POST", "PUT", "PATCH"}
- * )
- *
- * @see config/services.yaml
- *
- * Notice how we overrode auto-wiring to receive an instance of App\Logger.
- */
- public function number(LoggerInterface $logger, Request $request) {
- $number = $request->query->has('value')
- ? (int) $request->query->get('value')
- : mt_rand(0, 100);
- /* Example features of the Request */
- assert(false === $request->isXmlHttpRequest());
- // Uses Accept-Language header.
- $preferred = $request->getPreferredLanguage(['en', 'fr']);
- // Empty on GET, valid on PATH, POST, PUT.
- $requestValue = $request->request->get('value');
- $logger->notice("We sent $number on GET, $requestValue on POST/PUT/PATCH");
- return $this->render("lucky/number.html.twig", [
- "number" => $number,
- // No need for cookies->all(): Twig can use {% for %} on iterators.
- "cookies" => $request->cookies,
- ]);
- }
- /**
- * @\Symfony\Component\Routing\Annotation\Route(
- * name = "lucky-not-found",
- * path = "/lucky/not-found"
- * )
- */
- public function notFound() {
- $notFound = $this->createNotFoundException();
- throw $this->createAccessDeniedException("Denied because it was not found", $notFound);
- }
- /**
- * @\Symfony\Component\Routing\Annotation\Route(
- * name = "lucky-denied",
- * path = "/lucky/denied"
- * )
- */
- public function denied() {
- $denied = $this->createAccessDeniedException();
- throw $this->createNotFoundException("Not found because it was denied", $denied);
- }
- }
|