From 5562e033a2ceb8c046bcb720339adc0f06b84968 Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Mon, 29 Dec 2014 14:37:03 +0800 Subject: [PATCH] Better route format --- Services/Routing/IRoute.php | 2 +- Services/Routing/Route.php | 10 ++++---- Services/Routing/Router.php | 8 +++--- config/Routes.php | 61 ++++++++++++++++++++++++++------------------- 4 files changed, 45 insertions(+), 36 deletions(-) diff --git a/Services/Routing/IRoute.php b/Services/Routing/IRoute.php index e06d005..2cb5af7 100644 --- a/Services/Routing/IRoute.php +++ b/Services/Routing/IRoute.php @@ -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(); } diff --git a/Services/Routing/Route.php b/Services/Routing/Route.php index 9495bbc..a4f968b 100644 --- a/Services/Routing/Route.php +++ b/Services/Routing/Route.php @@ -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() diff --git a/Services/Routing/Router.php b/Services/Routing/Router.php index 333f683..52978e7 100644 --- a/Services/Routing/Router.php +++ b/Services/Routing/Router.php @@ -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() diff --git a/config/Routes.php b/config/Routes.php index f178297..afa2366 100644 --- a/config/Routes.php +++ b/config/Routes.php @@ -2,72 +2,81 @@ return [ '/simfiles' => [ - 'methods' => ['GET'], 'controller' => 'Simfile', - 'action' => 'list' + 'actions' => [ + 'GET'=> 'list' + ] ], '/simfiles/latest/simfile' => [ - 'methods' => ['GET'], 'controller' => 'Simfile', - 'action' => 'latestSimfile' + 'actions' => [ + 'GET' => 'latestSimfile' + ] ], '/simfiles/latest/pack' => [ - 'methods' => ['GET'], 'controller' => 'Simfile', - 'action' => 'latestPack' + 'actions' => [ + 'GET' => 'latestPack' + ] ], '/simfiles/popular' => [ - 'methods' => ['GET'], 'controller' => 'Simfile', - 'action' => 'popular' + 'actions' => [ + 'GET' => 'popular' + ] ], '/simfiles/upload' => [ - 'methods' => ['POST'], 'controller' => 'Simfile', - 'action' => 'upload' + 'actions' => [ + 'POST' => 'upload' + ] ], '/cache/update' => [ - 'methods' => ['GET'], 'controller' => 'SimfileCache', ], - '/simfiles/argTest/:testarg' => [ - 'methods' => ['GET'], - 'controller' => 'Simfile', - 'action' => 'test' - ], - '/user/auth' => [ - 'method' => ['GET'], 'controller' => 'UserAuth' ], '/user/:facebookId' => [ - 'method' => ['GET'], 'controller' => 'User', - 'action' => 'getUser' + 'actions' => [ + 'GET' => 'getUser' + ] ], + '/user' => [ + 'controller' => 'User', + 'actions' => [ + 'POST' => 'update', + ] + ], + '/files/banner/:hash' => [ - 'method' => ['GET'], 'controller' => 'File', - 'action' => 'serveBanner' + 'actions' => [ + 'GET' => 'serveBanner' + ] ], '/files/pack/:hash' => [ - 'method' => ['GET'], 'controller' => 'File', - 'action' => 'serveSimfileOrPack' + 'actions' => [ + 'GET' => 'serveSimfileOrPack' + ] ], '/files/simfile/:hash' => [ - 'method' => ['GET'], + 'methods' => ['GET'], 'controller' => 'File', - 'action' => 'serveSimfileOrPack' + 'actions' => [ + 'GET' => 'serveSimfileOrPack' + ] ] ]; -- 2.11.0