From cdd47e478c287d338e3dafcbd4418addc09a0288 Mon Sep 17 00:00:00 2001 From: Jefersson Nathan Date: Wed, 12 Feb 2014 16:11:19 -0200 Subject: [PATCH] Separate responsabilities of Classes - Refactor on PHPRouter\Route and PHPRouter\Router to accept a collection of routers. That is now more flexible and simple to manager yours routers. - Update README.md with new form to create Routers - Add "type" and PHPUnit like "require-dev" on composer.json file - Update example.php file with new syntax --- README.md | 44 ++++++++++++++++++------------ composer.json | 6 ++++- example.php | 39 ++++++++++++++++----------- src/PHPRouter/Route.php | 13 ++++++--- src/PHPRouter/RouteCollection.php | 57 +++++++++++++++++++++++++++++++++++++++ src/PHPRouter/Router.php | 52 ++++++++++++++++++++++------------- 6 files changed, 155 insertions(+), 56 deletions(-) mode change 100644 => 100755 example.php create mode 100755 src/PHPRouter/RouteCollection.php diff --git a/README.md b/README.md index 27fe907..0b7c885 100755 --- a/README.md +++ b/README.md @@ -7,31 +7,41 @@ A simple Rails inspired PHP router class. * Reversed routing using named routes * Dynamic URL's: use URL segments as parameters. +# Easy to install with **composer** + +```javascript +{ + "require": { + "dannyvankooten/php-router": "dev-master" + } +} +``` + ## Usage ```php setBasePath('/PHP-Router'); - -// defining routes can be as simple as this -$router->map('/', 'users#index'); +use PHPRouter\RouteCollection; +use PHPRouter\Router; +use PHPRouter\Route; -// or somewhat more complicated -$router->map('/users/:id/edit/', array('controller' => 'SomeController', 'action' => 'someAction'), array('methods' => 'GET,PUT', 'name' => 'users_edit', 'filters' => array('id' => '(\d+)'))); +$collection = new RouteCollection(); +$collection->add('users', new Route('/users/', array( + '_controller' => 'someController::users_create', + 'methods' => 'GET' +))); -// You can even specify closures as the Route's target -$router->map('/hello/:name', function($name) { echo "Hello $name."; }); +$collection->add('index', new Route('/', array( + '_controller' => 'someController::indexAction', + 'methods' => 'GET' +))); -// match current request URL & http method -$target = $router->matchCurrentRequest(); -var_dump($target); +$router = new Router($collection); +$router->setBasePath('/PHP-Router'); +$route = $router->matchCurrentRequest(); -// generate an URL -$router->generate('users_edit', array('id' => 5)); +var_dump($route); ``` ## More information diff --git a/composer.json b/composer.json index e4e4cc2..a4be28a 100755 --- a/composer.json +++ b/composer.json @@ -1,5 +1,6 @@ { "name": "dannyvankooten/php-router", + "type": "library", "description": "Simple PHP Router, supports REST and reverse routing.", "keywords": ["router", "routing", "php", "rest"], "homepage": "https://github.com/dannyvankooten/PHP-Router", @@ -16,7 +17,10 @@ } ], "require": { - "php": ">=5.3.0" + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "3.7.*" }, "autoload": { "psr-0": { diff --git a/example.php b/example.php old mode 100644 new mode 100755 index 6e8f71e..8e7cf03 --- a/example.php +++ b/example.php @@ -1,23 +1,30 @@ add('users', new Route('/users/', array( + '_controller' => 'someController::users_create', + 'methods' => 'GET' +))); + +$collection->add('index', new Route('/', array( + '_controller' => 'someController::indexAction', + 'methods' => 'GET' +))); + +$router = new Router($collection); $router->setBasePath('/PHP-Router'); - -$router->map('/', 'someController:indexAction', array('methods' => 'GET')); -$router->map('/users/','users#create', array('methods' => 'POST', 'name' => 'users_create')); -$router->map('/users/:id/edit/', 'users#edit', array('methods' => 'GET', 'name' => 'users_edit', 'filters' => array('id' => '(\d+)'))); -$router->map('/contact/',array('controller' => 'someController', 'action' => 'contactAction'), array('name' => 'contact')); - -$router->map('/blog/:slug', array('c' => 'BlogController', 'a' => 'showAction')); - -// capture rest of URL in "path" parameter (including forward slashes) -$router->map('/site-section/:path','some#target',array( 'filters' => array( 'path' => '(.*)') ) ); - $route = $router->matchCurrentRequest(); +var_dump($route); + ?>

Current URL & HTTP method would route to:

Target: diff --git a/src/PHPRouter/Route.php b/src/PHPRouter/Route.php index bcaab03..c300f0e 100755 --- a/src/PHPRouter/Route.php +++ b/src/PHPRouter/Route.php @@ -13,7 +13,7 @@ class Route * Accepted HTTP methods for this route * @var array */ - private $_methods = array('GET','POST','PUT','DELETE'); + private $_methods = array('GET', 'POST', 'PUT', 'DELETE'); /** * Target for this route, can be anything. @@ -39,6 +39,13 @@ class Route */ private $_parameters = array(); + public function __construct($resource, array $config) + { + $this->_url = $resource; + $this->_config = $config; + $this->_methods = isset($config['methods']) ? $config['methods'] : array(); + } + public function getUrl() { return $this->_url; @@ -91,7 +98,7 @@ class Route public function getRegex() { - return preg_replace_callback("/:(\w+)/", array(&$this, 'substituteFilter'), $this->_url); + return preg_replace_callback("/:(\w+)/", array(&$this, 'substituteFilter'), $this->_url); } private function substituteFilter($matches) @@ -112,4 +119,4 @@ class Route { $this->_parameters = $parameters; } -} +} \ No newline at end of file diff --git a/src/PHPRouter/RouteCollection.php b/src/PHPRouter/RouteCollection.php new file mode 100755 index 0000000..be42f15 --- /dev/null +++ b/src/PHPRouter/RouteCollection.php @@ -0,0 +1,57 @@ +_routes[$uri] = $action; + } + + public function remove($uri) + { + unset($this->_routes[$uri]); + } + + public function get($uri) + { + return $this->_routes[$uri]; + } + + public function current() + { + return current($this->_routes); + } + + public function next() + { + next($this->_routes); + } + + public function all() + { + return $this->_routes; + } + + public function key() + { + return key($this->_routes); + } + + public function valid() + { + if ($this->_routes) { + return true; + } + return false; + } + + public function rewind() + { + reset($this->_routes); + } +} \ No newline at end of file diff --git a/src/PHPRouter/Router.php b/src/PHPRouter/Router.php index 01e274b..1461f5c 100755 --- a/src/PHPRouter/Router.php +++ b/src/PHPRouter/Router.php @@ -1,6 +1,8 @@ _routes = $collection; + } + /** * Set the base _url - gets prepended to all route _url's. * @param string $base_url @@ -43,9 +51,11 @@ class Router */ public function map($routeUrl, $target = '', array $args = array()) { - $route = new Route(); + $route = new Route($this->_basePath . $routeUrl, array( + '_controller' => 'asd' + + )); - $route->setUrl($this->_basePath . $routeUrl); $route->setTarget($target); @@ -65,7 +75,7 @@ class Router } } - $this->_routes[] = $route; + $this->_routes->add($routeUrl ,$route); } /** @@ -85,28 +95,31 @@ class Router } /** - * Match given request _url and request method and see if a route has been defined for it - * If so, return route's target - * If called multiple times - */ + * Match given request _url and request method and see if a route has been defined for it + * If so, return route's target + * If called multiple times + * + * @param string $requestUrl + * @param string $requestMethod + * @return bool + */ public function match($requestUrl, $requestMethod = 'GET') { - - foreach ($this->_routes as $route) { - + foreach ($this->_routes->all() as $routes) { + // compare server request method with route's allowed http methods - if (! in_array($requestMethod, $route->getMethods())) { + if (! in_array($requestMethod, (array) $routes->getMethods())) { continue; } // check if request _url matches route regex. if not, return false. - if (! preg_match("@^".$route->getRegex()."*$@i", $requestUrl, $matches)) { + if (! preg_match("@^".$this->_basePath.$routes->getRegex()."*$@i", $requestUrl, $matches)) { continue; } $params = array(); - if (preg_match_all("/:([\w-]+)/", $route->getUrl(), $argument_keys)) { + if (preg_match_all("/:([\w-]+)/", $routes->getUrl(), $argument_keys)) { // grab array with matches $argument_keys = $argument_keys[1]; @@ -120,20 +133,21 @@ class Router } - $route->setParameters($params); + $routes->setParameters($params); - return $route; + return $routes; } return false; } - /** * Reverse route a named route - * - * @param string $route_name The name of the route to reverse route. + * + * @param $routeName * @param array $params Optional array of parameters to use in URL + * @throws Exception + * @internal param string $route_name The name of the route to reverse route. * @return string The url to the route */ public function generate($routeName, array $params = array()) -- 2.11.0