Procházet zdrojové kódy

Replacing controllers-blog.php by a Controller Provider.

Frederic G. MARAND před 6 roky
rodič
revize
1bc24ac2be

+ 0 - 41
src/controllers-blog.php

@@ -1,41 +0,0 @@
-<?php
-
-/**
- * @file
- * Demo typical entity listing routes with their modifiers.
- */
-
-use demo\Controllers\BlogController;
-use Silex\Application;
-use Silex\ControllerCollection;
-
-$blogCollection = function (Application $app): ControllerCollection {
-  $MAGIC = 'xyzzy';
-
-  // This is a factory service, producing a new instance every time.
-  $blog = $app['controllers_factory'];
-
-  $blog->get('/',             BlogController::class       . '::index')
-    ->bind('blog_list');
-  $blog->get('/{id}',         BlogController::class       . '::index2')
-    ->assert('id', $MAGIC)
-    // Serve '(mount)/xyzzy' when asked for '(mount)'.
-    ->value('id', $MAGIC)
-    ->bind('blog_mount');
-
-  $blog->get('/json',         BlogController::class       . '::json');
-  $blog->get('/json-view',    BlogController::class       . '::jsonView');
-  $blog->get('/{id}',         BlogController::class       . '::fifiAction')
-    ->assert('id', '\d+')
-    // See Symfony expression language.
-    ->when("request.headers.get('User-Agent') matches '/firefox/i'");
-  $blog->get('/{id}',         BlogController::class       . '::show')
-    ->assert('id', '\d+')
-    // Serve '(mount)/1' when asked for '(mount)/'.
-    ->value('id', 1)
-    ->bind('blog_post');
-
-  return $blog;
-};
-
-return $blogCollection($app);

+ 3 - 4
src/controllers.php

@@ -18,6 +18,7 @@
  * @see \Silex\Provider\HttpKernelServiceProvider::subscribe()
  */
 
+use demo\Controllers\BlogControllerProvider;
 use demo\Controllers\DelegatingController;
 use demo\Controllers\ErrorController;
 use demo\Controllers\EscapeController;
@@ -103,10 +104,8 @@ $app->get('/home',              DelegatingController::class . '::redirectPath');
 $app->get('/blogz',             DelegatingController::class . '::forwardPath');
 $app->get('/all_blogs',         DelegatingController::class . '::forwardName');
 
-// Using a mounted  route collection
-$blog = require_once __DIR__ . '/controllers-blog.php';
-// This will handle /blogs/ and below, AND /blogs : see the 'blog_mount' route.
-$app->mount('/blogs', $blog);
+// Using a mounted  route controller provider
+$app->mount('/blogs', new BlogControllerProvider());
 
 // Demo POST request handling.
 $app->post('/feedback',         FeedbackController::class   . '::feedbackAction');

+ 49 - 0
src/demo/Controllers/BlogControllerProvider.php

@@ -0,0 +1,49 @@
+<?php
+
+namespace demo\Controllers;
+
+use Silex\Api\ControllerProviderInterface;
+use Silex\Application;
+use Silex\ControllerCollection;
+
+class BlogControllerProvider implements ControllerProviderInterface {
+  const MAGIC = 'xyzzy';
+
+  /**
+   * Returns routes to connect to the given application.
+   *
+   * @param Application $app An Application instance
+   *
+   * @return ControllerCollection A ControllerCollection instance
+   */
+  public function connect(Application $app): ControllerCollection {
+    // This is a factory service, producing a new instance every time.
+    $collection = $app['controllers_factory'];
+    $blog = BlogController::class;
+
+    $collection->get('/',             "$blog::index")
+      ->bind('blog_list');
+    $collection->get('/{id}',         "$blog::index2")
+      ->assert('id', static::MAGIC)
+      // Serve '(mount)/xyzzy' when asked for '(mount)'.
+      ->value('id', static::MAGIC)
+      ->bind('blog_mount');
+
+    $collection->get('/json',         "$blog::json");
+    $collection->get('/json-view',    "$blog::jsonView");
+
+    $collection->get('/{id}',         "$blog::fifiAction")
+      ->assert('id', '\d+')
+      // See Symfony expression language.
+      ->when("request.headers.get('User-Agent') matches '/firefox/i'");
+
+    $collection->get('/{id}',         "$blog::show")
+      ->assert('id', '\d+')
+      // Serve '(mount)/1' when asked for '(mount)/'.
+      ->value('id', 1)
+      ->bind('blog_post');
+
+    return $collection;
+  }
+
+}