diff --git a/README.md b/README.md index 23f2d5f..84dc879 100644 --- a/README.md +++ b/README.md @@ -22,13 +22,15 @@ In your ```index.php``` require your ```routes.php``` and call the ```routeReque This is an example of a basic ```index.php``` file: ```php +use \Pecee\SimpleRouter; + require_once 'routes.php'; // change this to whatever makes sense in your project - -// Initialise the router -$router = \Pecee\SimpleRouter::GetInstance(); - -// Do the actual routing -$router->routeRequest() + +// The apps default namespace (so we don't have to specify it each time we use MyController@home) +$defaultControllerNamespace = 'MyWebsite\\Controller'; + +// Do the routing +SimpleRouter::init($defaultControllerNamespace); ``` ## Adding routes @@ -39,7 +41,7 @@ This router is heavily inspired by the Laravel 5.* router, so anything you find ### Basic example ```php -using \Pecee\Router; +using \Pecee\SimpleRouter; /* * This route will match the url /v1/services/answers/1/ @@ -49,11 +51,11 @@ using \Pecee\Router; * the request, for instance if a user is not authenticated. */ -Router::group(['prefix' => 'v1', 'middleware' => '\MyWebsite\Middleware\SomeMiddlewareClass'], function() { +SimpleRouter::group(['prefix' => 'v1', 'middleware' => '\MyWebsite\Middleware\SomeMiddlewareClass'], function() { - Router::group(['prefix' => 'services'], function() { + SimpleRouter::group(['prefix' => 'services'], function() { - Router::get('/answers/{id}', 'ControllerAnswers@show'); + SimpleRouter::get('/answers/{id}', 'ControllerAnswers@show'); }); }); @@ -65,9 +67,9 @@ The ```Router``` class is just a simple helper class that knows how to communica ```php use \Pecee\SimpleRouter; -use \Pecee\Router\RouterRoute; +use \Pecee\SimpleRouter\Router; -$router = SimpleRouter::GetInstance(); +$router = Router::getInstance(); $route = new RouterRoute('/answer/1', function() { die('this callback will match /answer/1'); diff --git a/src/Pecee/Router.php b/src/Pecee/SimpleRouter.php similarity index 77% rename from src/Pecee/Router.php rename to src/Pecee/SimpleRouter.php index d3a5c72..3bb702b 100644 --- a/src/Pecee/Router.php +++ b/src/Pecee/SimpleRouter.php @@ -7,23 +7,25 @@ * This class is added so calls can be made staticly like Router::get() making the code look more pretty. */ -namespace Pecee; +namespace Pecee\SimpleRouter; use Pecee\Router\RouterGroup; use Pecee\Router\RouterRoute; -use Pecee\Router\SimpleRouter; +use Pecee\Router\Router; -class Router { +class SimpleRouter { - public static function init() { - SimpleRouter::GetInstance()->routeRequest(); + public static function init($defaultNamespace = null) { + $router = Router::GetInstance(); + $router->setDefaultControllerNamespace($defaultNamespace); + $router->routeRequest(); } public static function get($url, $callback) { $route = new RouterRoute($url, $callback); $route->addRequestType(RouterRoute::REQUEST_TYPE_GET); - $router = SimpleRouter::GetInstance(); + $router = Router::GetInstance(); $router->addRoute($route); return $route; @@ -33,7 +35,7 @@ class Router { $route = new RouterRoute($url, $callback); $route->addRequestType(RouterRoute::REQUEST_TYPE_POST); - $router = SimpleRouter::GetInstance(); + $router = Router::GetInstance(); $router->addRoute($route); return $route; @@ -43,7 +45,7 @@ class Router { $route = new RouterRoute($url, $callback); $route->addRequestType(RouterRoute::REQUEST_TYPE_PUT); - $router = SimpleRouter::GetInstance(); + $router = Router::GetInstance(); $router->addRoute($route); return $route; @@ -53,7 +55,7 @@ class Router { $route = new RouterRoute($url, $callback); $route->addRequestType(RouterRoute::REQUEST_TYPE_DELETE); - $router = SimpleRouter::GetInstance(); + $router = Router::GetInstance(); $router->addRoute($route); return $route; @@ -67,7 +69,7 @@ class Router { $group->setSettings($settings); } - $router = SimpleRouter::GetInstance(); + $router = Router::GetInstance(); $router->addRoute($group); return $group; @@ -79,7 +81,7 @@ class Router { $route->addRequestType($requestType); } - $router = SimpleRouter::GetInstance(); + $router = Router::GetInstance(); $router->addRoute($route); return $route; diff --git a/src/Pecee/Router/SimpleRouter.php b/src/Pecee/SimpleRouter/Router.php similarity index 75% rename from src/Pecee/Router/SimpleRouter.php rename to src/Pecee/SimpleRouter/Router.php index d1481e0..58c6170 100644 --- a/src/Pecee/Router/SimpleRouter.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -1,7 +1,7 @@ routes = array(); @@ -20,6 +21,11 @@ class SimpleRouter { } public function addRoute(RouterEntry $route) { + // Add default namespace + if(!$route->getNamespace() && $this->defaultControllerNamespace !== null) { + $route->setNamespace($this->defaultControllerNamespace); + } + if($this->currentRoute !== null) { $this->backstack[] = $route; } else { @@ -130,7 +136,63 @@ class SimpleRouter { } } - public static function GetInstance() { + /** + * @return string + */ + public function getDefaultControllerNamespace(){ + return $this->defaultControllerNamespace; + } + + /** + * @param string $defaultControllerNamespace + */ + public function setDefaultControllerNamespace($defaultControllerNamespace) { + $this->defaultControllerNamespace = $defaultControllerNamespace; + } + + /** + * @return RouterEntry + */ + public function getLoadedClass() { + return $this->loadedClass; + } + + /** + * @return string + */ + public function getRequestMethod() { + return $this->requestMethod; + } + + /** + * @return string + */ + public function getRequestUri() { + return $this->requestUri; + } + + /** + * @return array + */ + public function getBackstack() { + return $this->backstack; + } + + /** + * @return RouterEntry + */ + public function getCurrentRoute(){ + return $this->currentRoute; + } + + /** + * @return array + */ + public function getRoutes(){ + return $this->routes; + } + + public static function getInstance() { if(self::$instance === null) { self::$instance = new self(); } diff --git a/src/Pecee/Router/RouterEntry.php b/src/Pecee/SimpleRouter/RouterEntry.php similarity index 100% rename from src/Pecee/Router/RouterEntry.php rename to src/Pecee/SimpleRouter/RouterEntry.php diff --git a/src/Pecee/Router/RouterException.php b/src/Pecee/SimpleRouter/RouterException.php similarity index 100% rename from src/Pecee/Router/RouterException.php rename to src/Pecee/SimpleRouter/RouterException.php diff --git a/src/Pecee/Router/RouterGroup.php b/src/Pecee/SimpleRouter/RouterGroup.php similarity index 100% rename from src/Pecee/Router/RouterGroup.php rename to src/Pecee/SimpleRouter/RouterGroup.php diff --git a/src/Pecee/Router/RouterRoute.php b/src/Pecee/SimpleRouter/RouterRoute.php similarity index 98% rename from src/Pecee/Router/RouterRoute.php rename to src/Pecee/SimpleRouter/RouterRoute.php index 5766ef5..f9a09f3 100644 --- a/src/Pecee/Router/RouterRoute.php +++ b/src/Pecee/SimpleRouter/RouterRoute.php @@ -3,7 +3,7 @@ namespace Pecee\Router; use Pecee\Registry; -use Pecee\SimpleRouter; +use Pecee\Router; class RouterRoute extends RouterEntry {