浏览代码

Use the url_generator service and define a custom service class.

Frederic G. MARAND 6 年之前
父节点
当前提交
f4a10e93ee
共有 7 个文件被更改,包括 69 次插入15 次删除
  1. 0 2
      .idea/silex-book.iml
  2. 2 1
      src/app.php
  3. 3 1
      src/demo/Controllers/HomeController.php
  4. 43 0
      src/demo/Services/NavBuilder.php
  5. 2 11
      templates/layout.html.twig
  6. 6 0
      templates/nav.html.twig
  7. 13 0
      web/js/ga.js

+ 0 - 2
.idea/silex-book.iml

@@ -152,7 +152,6 @@
           <root url="file://$MODULE_DIR$/vendor/symfony/polyfill-mbstring" />
           <root url="file://$MODULE_DIR$/vendor/symfony/polyfill-php56" />
           <root url="file://$MODULE_DIR$/vendor/symfony/polyfill-php70" />
-          <root url="file://$MODULE_DIR$/vendor/symfony/polyfill-php72" />
           <root url="file://$MODULE_DIR$/vendor/symfony/polyfill-util" />
           <root url="file://$MODULE_DIR$/vendor/symfony/process" />
           <root url="file://$MODULE_DIR$/vendor/symfony/property-access" />
@@ -233,7 +232,6 @@
           <root url="file://$MODULE_DIR$/vendor/symfony/polyfill-mbstring" />
           <root url="file://$MODULE_DIR$/vendor/symfony/polyfill-php56" />
           <root url="file://$MODULE_DIR$/vendor/symfony/polyfill-php70" />
-          <root url="file://$MODULE_DIR$/vendor/symfony/polyfill-php72" />
           <root url="file://$MODULE_DIR$/vendor/symfony/polyfill-util" />
           <root url="file://$MODULE_DIR$/vendor/symfony/process" />
           <root url="file://$MODULE_DIR$/vendor/symfony/property-access" />

+ 2 - 1
src/app.php

@@ -2,6 +2,7 @@
 
 use demo\AppRoute;
 use demo\Logger;
+use demo\Services\NavBuilder;
 use demo\Services\Timer;
 use Demo\UserConverter;
 use Silex\Application;
@@ -19,7 +20,7 @@ $app['timer'] = Timer::create();
 
 // Closures are the basic form of Pimple services.
 $app['logger'] = function () { return new Logger(); };
-
+$app['nav_builder'] = function ($app) { return NavBuilder::create($app); };
 $app->register(new ServiceControllerServiceProvider());
 $app->register(new AssetServiceProvider());
 $app->register(new TwigServiceProvider());

+ 3 - 1
src/demo/Controllers/HomeController.php

@@ -12,7 +12,9 @@ use Silex\Application;
 class HomeController {
 
   public function home(Application $app) {
-   return $app['twig']->render('index.html.twig', []);
+   return $app['twig']->render('index.html.twig', [
+     'nav' => $app['nav_builder']->build(),
+   ]);
   }
 
 }

+ 43 - 0
src/demo/Services/NavBuilder.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace demo\Services;
+
+use Silex\Application;
+use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
+
+class NavBuilder {
+
+  /**
+   * @var \Twig_Environment
+   */
+  protected $twig;
+
+  /**
+   * @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface
+   */
+  protected $urlGenerator;
+
+  public function __construct(
+    UrlGeneratorInterface $urlGenerator,
+    \Twig_Environment $twig
+  ) {
+    $this->twig = $twig;
+    $this->urlGenerator = $urlGenerator;
+  }
+
+  public static function create(Application $app) {
+    $urlGenerator = $app['url_generator'];
+    $twig = $app['twig'];
+    return new static($urlGenerator, $twig);
+  }
+
+  public function build() {
+    $home = $this->urlGenerator->generate('homepage');
+    $blog = $this->urlGenerator->generate('blog_list');
+    $result =  [
+      'home' => ['url' => $home, 'text' => 'Accueil'],
+      'blog' => ['url' => $blog, 'text' => 'Blogs'],
+    ];
+    return $result;
+  }
+}

+ 2 - 11
templates/layout.html.twig

@@ -5,19 +5,10 @@
 
         <link href="{{ asset('css/main.css') }}" rel="stylesheet" type="text/css" />
 
-        <script type="text/javascript">
-            var _gaq = _gaq || [];
-            _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
-            _gaq.push(['_trackPageview']);
-
-            (function() {
-                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
-                ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
-                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
-            })();
-        </script>
+        <script type="text/javascript" src="{{ asset('js/ga.js') }}"></script>
     </head>
     <body>
         {% block content %}{% endblock %}
+        {% include 'nav.html.twig' with { 'nav': nav } %}
     </body>
 </html>

+ 6 - 0
templates/nav.html.twig

@@ -0,0 +1,6 @@
+<nav>
+    <ul>
+        <li><a href="{{ nav.home.url }}">{{ nav.home.text }}</a></li>
+        <li><a href="{{ nav.blog.url }}">{{ nav.blog.text }}</a></li>
+    </ul>
+</nav>

+ 13 - 0
web/js/ga.js

@@ -0,0 +1,13 @@
+const _gaq = [
+  ["_setAccount", "UA-XXXXXXXX-X"],
+  ["_trackPageview"]
+];
+
+(function () {
+  const ga = document.createElement("script");
+  ga.type = "text/javascript";
+  ga.async = true;
+  ga.src = (document.location.protocol === "https:" ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";
+  const s = document.getElementsByTagName("script")[0];
+  s.parentNode.insertBefore(ga, s);
+}());