Browse Source

Ch. 3: route defaults, requirements, url_generator

Frederic G. MARAND 6 years ago
parent
commit
d7bba5fb6f
3 changed files with 97 additions and 16 deletions
  1. 10 16
      .idea/sf4-book.iml
  2. 39 0
      src/Controller/ArticleController.php
  3. 48 0
      src/Controller/BlogController.php

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

@@ -79,19 +79,11 @@
           <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" />
@@ -121,19 +113,21 @@
           <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>
+    <orderEntry type="module-library">
+      <library name="PHARS">
+        <CLASSES>
+          <root url="phar://$MODULE_DIR$/vendor/symfony/dependency-injection/Tests/Fixtures/includes/ProjectWithXsdExtensionInPhar.phar/" />
+        </CLASSES>
+        <SOURCES>
+          <root url="phar://$MODULE_DIR$/vendor/symfony/dependency-injection/Tests/Fixtures/includes/ProjectWithXsdExtensionInPhar.phar/" />
         </SOURCES>
       </library>
     </orderEntry>

+ 39 - 0
src/Controller/ArticleController.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace App\Controller;
+
+use Symfony\Component\Routing\Annotation\Route;
+
+class ArticleController {
+
+  /**
+   * @param $_locale
+   * @param $year
+   * @param $slug
+   *
+   * @Route(
+   *   path = "/articles/{_locale}/{year}/{slug}.{_format}",
+   *   name = "article_show",
+   *   defaults = {
+   *     "_format": "html",
+   *   },
+   *   requirements = {
+   *     "_locale": "en|fr",
+   *     "_format": "html|rss",
+   *     "year": "\d+"
+   *   }
+   *
+   * )
+   *
+   * - placeholders cannot be more than 32 characters long
+   * - 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
+   *   - _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) {
+
+  }
+}

+ 48 - 0
src/Controller/BlogController.php

@@ -0,0 +1,48 @@
+<?php
+
+namespace App\Controller;
+
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Routing\Annotation\Route;
+use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
+
+class BlogController extends Controller {
+
+  /**
+   * @param $page
+   *
+   * @return Response
+   *
+   * @Route(
+   *   name ="blog_list",
+   *   path ="/blog/{page}",
+   *   requirements={"page" = "\d+"}
+   * )
+   */
+  public function list($page = 1) {
+    return new Response("Page $page of the blog list.");
+  }
+
+  /**
+   * @param $slug
+   *
+   * @return \Symfony\Component\HttpFoundation\Response
+   *
+   * @Route(
+   *   name = "blog_show",
+   *   path = "/blog/{slug}"
+   * )
+   */
+  public function show($slug) {
+    $path = $this->generateUrl('blog_list', [
+      // Placeholder: fit in the path.
+      'page' => 2,
+      // Non-placeholder: fit in the query
+      'foo' => 'bar',
+    ],
+      UrlGeneratorInterface::ABSOLUTE_PATH);
+    return new Response("Show $slug. Back to page 2: $path");
+  }
+
+}