Browse Source

Ch. 4: controller tricks

Frederic G. MARAND 6 years ago
parent
commit
bdfe6c050b

+ 16 - 0
.idea/sf4-book.iml

@@ -79,11 +79,19 @@
           <root url="file://$MODULE_DIR$/vendor/symfony/http-foundation" />
           <root url="file://$MODULE_DIR$/vendor/symfony/http-kernel" />
           <root url="file://$MODULE_DIR$/vendor/symfony/polyfill-mbstring" />
+          <root url="file://$MODULE_DIR$/vendor/symfony/polyfill-php72" />
           <root url="file://$MODULE_DIR$/vendor/symfony/process" />
+          <root url="file://$MODULE_DIR$/vendor/symfony/profiler-pack" />
           <root url="file://$MODULE_DIR$/vendor/symfony/requirements-checker" />
           <root url="file://$MODULE_DIR$/vendor/symfony/routing" />
+          <root url="file://$MODULE_DIR$/vendor/symfony/stopwatch" />
+          <root url="file://$MODULE_DIR$/vendor/symfony/twig-bridge" />
+          <root url="file://$MODULE_DIR$/vendor/symfony/twig-bundle" />
+          <root url="file://$MODULE_DIR$/vendor/symfony/var-dumper" />
+          <root url="file://$MODULE_DIR$/vendor/symfony/web-profiler-bundle" />
           <root url="file://$MODULE_DIR$/vendor/symfony/web-server-bundle" />
           <root url="file://$MODULE_DIR$/vendor/symfony/yaml" />
+          <root url="file://$MODULE_DIR$/vendor/twig/twig" />
         </CLASSES>
         <SOURCES>
           <root url="file://$MODULE_DIR$/vendor/composer" />
@@ -113,11 +121,19 @@
           <root url="file://$MODULE_DIR$/vendor/symfony/http-foundation" />
           <root url="file://$MODULE_DIR$/vendor/symfony/http-kernel" />
           <root url="file://$MODULE_DIR$/vendor/symfony/polyfill-mbstring" />
+          <root url="file://$MODULE_DIR$/vendor/symfony/polyfill-php72" />
           <root url="file://$MODULE_DIR$/vendor/symfony/process" />
+          <root url="file://$MODULE_DIR$/vendor/symfony/profiler-pack" />
           <root url="file://$MODULE_DIR$/vendor/symfony/requirements-checker" />
           <root url="file://$MODULE_DIR$/vendor/symfony/routing" />
+          <root url="file://$MODULE_DIR$/vendor/symfony/stopwatch" />
+          <root url="file://$MODULE_DIR$/vendor/symfony/twig-bridge" />
+          <root url="file://$MODULE_DIR$/vendor/symfony/twig-bundle" />
+          <root url="file://$MODULE_DIR$/vendor/symfony/var-dumper" />
+          <root url="file://$MODULE_DIR$/vendor/symfony/web-profiler-bundle" />
           <root url="file://$MODULE_DIR$/vendor/symfony/web-server-bundle" />
           <root url="file://$MODULE_DIR$/vendor/symfony/yaml" />
+          <root url="file://$MODULE_DIR$/vendor/twig/twig" />
         </SOURCES>
       </library>
     </orderEntry>

+ 4 - 1
src/Controller/ArticleController.php

@@ -29,11 +29,14 @@ class ArticleController {
    * - Special placeholders:
    *   - _format is a special parameter used for the request format and content
    *     type negotiation
-   *   - _controller defines the controller for the route as a callable
+   *   - _controller defines the controller for the route as a PHP callable,
+   *      including class names if they implement __invoke(); or the service
+   *      notation service:method.
    *   - _fragment is the URI fragment (not sent by user agents over HTTP)
    *   - _locale is used to set the locale on the request
    */
   public function show($_locale, $year, $slug) {
 
   }
+
 }

+ 1 - 2
src/Controller/BlogController.php

@@ -40,8 +40,7 @@ class BlogController extends Controller {
       'page' => 2,
       // Non-placeholder: fit in the query
       'foo' => 'bar',
-    ],
-      UrlGeneratorInterface::ABSOLUTE_PATH);
+    ], UrlGeneratorInterface::ABSOLUTE_PATH);
     return new Response("Show $slug. Back to page 2: $path");
   }
 

+ 35 - 3
src/Controller/DefaultController.php

@@ -16,13 +16,45 @@ class DefaultController extends AbstractController {
    *   name="front"
    * )
    * @Route("/hello",
-   *   name="front-hello"
+   *   name="front_hello"
    * )
    */
   public function index() {
     return new Response("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
@@ -30,7 +62,7 @@ class DefaultController extends AbstractController {
    * @return \Symfony\Component\HttpFoundation\Response
    *
    * @Route(
-   *  name="hello-html",
+   *  name="hello_html",
    *  path="/hello/{name}"
    * )
    */
@@ -45,7 +77,7 @@ class DefaultController extends AbstractController {
    * @return \Symfony\Component\HttpFoundation\JsonResponse
    *
    * @Route("/json/{name}",
-   *   name="hello-json",
+   *   name="hello_json",
    * )
    */
   public function helloJson($name) {

+ 3 - 1
src/Controller/LuckyController.php

@@ -3,6 +3,7 @@
 namespace App\Controller;
 
 
+use Psr\Log\LoggerInterface;
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 
@@ -14,8 +15,9 @@ class LuckyController extends Controller {
    *   path="/lucky/number"
    * )
    */
-  public function number() {
+  public function number(LoggerInterface $logger) {
     $number = mt_rand(0, 100);
+    $logger->notice("We sent $number");
     return $this->render("lucky/number.html.twig", [
       "number" => $number,
     ]);