Browse Source

Reorganize controllers to group blog controllers under a mount().

Frederic G. MARAND 6 years ago
parent
commit
cdc74218bd
4 changed files with 60 additions and 24 deletions
  1. 1 1
      .idea/php.xml
  2. 41 0
      src/controllers-blog.php
  3. 4 13
      src/controllers.php
  4. 14 10
      src/demo/Controllers/BlogController.php

+ 1 - 1
.idea/php.xml

@@ -83,7 +83,7 @@
       <path value="$PROJECT_DIR$/vendor/monolog/monolog" />
     </include_path>
   </component>
-  <component name="PhpProjectSharedConfiguration" php_language_level="5.5.0" />
+  <component name="PhpProjectSharedConfiguration" php_language_level="7.1" />
   <component name="PhpUnit">
     <phpunit_settings>
       <PhpUnitSettings load_method="CUSTOM_LOADER" custom_loader_path="$PROJECT_DIR$/vendor/autoload.php" />

+ 41 - 0
src/controllers-blog.php

@@ -0,0 +1,41 @@
+<?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);

+ 4 - 13
src/controllers.php

@@ -18,7 +18,6 @@
  * @see \Silex\Provider\HttpKernelServiceProvider::subscribe()
  */
 
-use demo\Controllers\BlogController;
 use demo\Controllers\DelegatingController;
 use demo\Controllers\ErrorController;
 use demo\Controllers\EscapeController;
@@ -104,18 +103,10 @@ $app->get('/home',              DelegatingController::class . '::redirectPath');
 $app->get('/blogz',             DelegatingController::class . '::forwardPath');
 $app->get('/all_blogs',         DelegatingController::class . '::forwardName');
 
-// Demo typical entity listing routes with their modifiers.
-$app->get('/blogs',             BlogController::class       . '::index')
-  ->bind('blog_list');
-$app->get('/blogs-json',        BlogController::class       . '::json');
-$app->get('/blogs-json-view',   BlogController::class       . '::jsonView');
-$app->get('/blog/{id}',         BlogController::class       . '::fifiAction')
-  ->assert('id', '\d+')
-  ->when("request.headers.get('User-Agent') matches '/firefox/i'");
-$app->get('/blog/{id}',         BlogController::class       . '::show')
-  ->assert('id', '\d+')
-  ->value('id', 1)
-  ->bind('blog_post');
+// 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);
 
 // Demo POST request handling.
 $app->post('/feedback',         FeedbackController::class   . '::feedbackAction');

+ 14 - 10
src/demo/Controllers/BlogController.php

@@ -30,8 +30,6 @@ class BlogController {
     ],
   ];
 
-
-  // Default: http://blog, not http://blog/
   public function fifiAction(Application $app, $id) {
     return "<h1>Fifi action {$id} </h1>\n";
   }
@@ -45,14 +43,9 @@ class BlogController {
     return $output;
   }
 
-  // Default: http://blog, not http://blog/
-  public function show(Application $app, $id) {
-    if (!isset (static::POSTS[$id])) {
-      // Will trigger the error() (or built-in) error handler.1
-      $app->abort(Response::HTTP_NOT_FOUND, "Post $id does not exist.");
-    }
-    $post = static::POSTS[$id];
-    return "<h1> {$post['title']}</h1>" . "<p> {$post['body']} </p>";
+  public function index2() {
+    return "<p>From mount point</p>\n"
+      . $this->index();
   }
 
   // Encode response manually.
@@ -64,4 +57,15 @@ class BlogController {
   public function jsonView() {
     return static::POSTS;
   }
+
+  // Default: http://blog, not http://blog/
+  public function show(Application $app, $id) {
+    if (!isset (static::POSTS[$id])) {
+      // Will trigger the error() (or built-in) error handler.1
+      $app->abort(Response::HTTP_NOT_FOUND, "Post $id does not exist.");
+    }
+    $post = static::POSTS[$id];
+    return "<h1> {$post['title']}</h1>" . "<p> {$post['body']} </p>";
+  }
+
 }