Better route format
authorCameron Ball <cameron@getapproved.com.au>
Mon, 29 Dec 2014 06:37:03 +0000 (14:37 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Mon, 29 Dec 2014 06:37:03 +0000 (14:37 +0800)
Services/Routing/IRoute.php
Services/Routing/Route.php
Services/Routing/Router.php
config/Routes.php

index e06d005..2cb5af7 100644 (file)
@@ -7,6 +7,6 @@ interface IRoute
     public function matches($uri);
     public function supports($method);
     public function getControllerName();
-    public function getActionName();
+    public function getActionName($method);
     public function getActionArgs();
 }
index 9495bbc..a4f968b 100644 (file)
@@ -13,12 +13,12 @@ class Route implements IRoute
     private $_argNames;
     private $_argValues;
     
-    public function __construct($pattern, array $methods, $controllerName, $actionName = null)
+    public function __construct($pattern, array $actions, $controllerName)
     {
         $this->_controllerName = $controllerName;
-        $this->_actionName = $actionName;
+        $this->_actions = $actions;
+        $this->_methods = array_keys($actions);
         $this->_pattern = $pattern;
-        $this->_methods = $methods;
     }
     
     public function matches($path) {
@@ -62,9 +62,9 @@ class Route implements IRoute
         return $this->_controllerName;
     }
     
-    public function getActionName()
+    public function getActionName($method)
     {
-        return $this->_actionName;
+        return $this->_actions[$method];
     }
     
     public function getActionArgs()
index 333f683..52978e7 100644 (file)
@@ -19,12 +19,12 @@ class Router implements IRouter
         
         foreach($this->_maps as $pattern => $routeInfo)
         {
-            $methods = isset($routeInfo['methods']) ? $routeInfo['methods'] : array('GET');
+            //$methods = isset($routeInfo['methods']) ? $routeInfo['methods'] : array('GET');
             $controller = isset($routeInfo['controller']) ? $routeInfo['controller'] : 'index';
-            $action = isset($routeInfo['action']) ? $routeInfo['action'] : 'index';
+            $actions = isset($routeInfo['actions']) ? $routeInfo['actions'] : array('GET' => 'index');
             
             //TODO: really I should be using a builder or a factory with DI for this but yolo.
-            $this->_routes[] = new Route($pattern, $methods, $controller, $action);
+            $this->_routes[] = new Route($pattern, $actions, $controller);
         }
     }
     
@@ -37,7 +37,7 @@ class Router implements IRouter
     public function getActionName()
     {
         $matchedRoute = $this->findMatch();
-        return $matchedRoute ? $matchedRoute->getActionName() : 'index';
+        return $matchedRoute ? $matchedRoute->getActionName($this->_request->getMethod()) : 'index';
     }
     
     public function getActionArgs()
index f178297..afa2366 100644 (file)
@@ -2,72 +2,81 @@
 \r
 return [\r
     '/simfiles' => [\r
-        'methods' => ['GET'],\r
         'controller' => 'Simfile',\r
-        'action' => 'list'\r
+        'actions' => [\r
+            'GET'=> 'list'\r
+        ]\r
     ],\r
     \r
     '/simfiles/latest/simfile' => [\r
-        'methods' => ['GET'],\r
         'controller' => 'Simfile',\r
-        'action' => 'latestSimfile'\r
+        'actions' => [\r
+            'GET' => 'latestSimfile'\r
+        ]\r
     ],\r
     \r
     '/simfiles/latest/pack' => [\r
-        'methods' => ['GET'],\r
         'controller' => 'Simfile',\r
-        'action' => 'latestPack'\r
+        'actions' => [\r
+            'GET' => 'latestPack'\r
+        ]\r
     ],\r
     \r
     '/simfiles/popular' => [\r
-        'methods' => ['GET'],\r
         'controller' => 'Simfile',\r
-        'action' => 'popular'\r
+        'actions' => [\r
+            'GET' => 'popular'\r
+        ]\r
     ],\r
     \r
     '/simfiles/upload' => [\r
-        'methods' => ['POST'],\r
         'controller' => 'Simfile',\r
-        'action' => 'upload'\r
+        'actions' => [\r
+            'POST' => 'upload'\r
+        ]\r
     ],\r
     \r
     '/cache/update' => [\r
-        'methods' => ['GET'],\r
         'controller' => 'SimfileCache',\r
     ],\r
     \r
-    '/simfiles/argTest/:testarg' => [\r
-        'methods' => ['GET'],\r
-        'controller' => 'Simfile',\r
-        'action' => 'test'\r
-    ],\r
-    \r
     '/user/auth' => [\r
-        'method' => ['GET'],\r
         'controller' => 'UserAuth'\r
     ],\r
     \r
     '/user/:facebookId' => [\r
-        'method' => ['GET'],\r
         'controller' => 'User',\r
-        'action' => 'getUser'\r
+        'actions' => [\r
+            'GET' => 'getUser'\r
+         ]\r
     ],\r
     \r
+    '/user' => [\r
+        'controller' => 'User',\r
+        'actions' => [\r
+            'POST' => 'update',\r
+        ]\r
+    ],\r
+\r
     '/files/banner/:hash' => [\r
-        'method' => ['GET'],\r
         'controller' => 'File',\r
-        'action' => 'serveBanner'\r
+        'actions' => [\r
+            'GET' => 'serveBanner'\r
+        ]\r
     ],\r
     \r
     '/files/pack/:hash' => [\r
-        'method' => ['GET'],\r
         'controller' => 'File',\r
-        'action' => 'serveSimfileOrPack'\r
+        'actions' => [\r
+            'GET' => 'serveSimfileOrPack'\r
+         ]\r
     ],\r
     \r
     '/files/simfile/:hash' => [\r
-        'method' => ['GET'],\r
+        'methods' => ['GET'],\r
         'controller' => 'File',\r
-        'action' => 'serveSimfileOrPack'\r
+        'actions' => [\r
+            'GET' => 'serveSimfileOrPack'\r
+        ]\r
     ]\r
 ];\r