- Renamed to avoid conflicts with other routers.
- Updated documentation.
This commit is contained in:
Simon Sessingø
2015-09-18 20:16:59 +02:00
parent 05a89c368d
commit c9f3cc9329
7 changed files with 92 additions and 26 deletions
+14 -12
View File
@@ -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: This is an example of a basic ```index.php``` file:
```php ```php
use \Pecee\SimpleRouter;
require_once 'routes.php'; // change this to whatever makes sense in your project require_once 'routes.php'; // change this to whatever makes sense in your project
// Initialise the router // The apps default namespace (so we don't have to specify it each time we use MyController@home)
$router = \Pecee\SimpleRouter::GetInstance(); $defaultControllerNamespace = 'MyWebsite\\Controller';
// Do the actual routing // Do the routing
$router->routeRequest() SimpleRouter::init($defaultControllerNamespace);
``` ```
## Adding routes ## Adding routes
@@ -39,7 +41,7 @@ This router is heavily inspired by the Laravel 5.* router, so anything you find
### Basic example ### Basic example
```php ```php
using \Pecee\Router; using \Pecee\SimpleRouter;
/* /*
* This route will match the url /v1/services/answers/1/ * 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. * 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 ```php
use \Pecee\SimpleRouter; use \Pecee\SimpleRouter;
use \Pecee\Router\RouterRoute; use \Pecee\SimpleRouter\Router;
$router = SimpleRouter::GetInstance(); $router = Router::getInstance();
$route = new RouterRoute('/answer/1', function() { $route = new RouterRoute('/answer/1', function() {
die('this callback will match /answer/1'); die('this callback will match /answer/1');
@@ -7,23 +7,25 @@
* This class is added so calls can be made staticly like Router::get() making the code look more pretty. * 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\RouterGroup;
use Pecee\Router\RouterRoute; use Pecee\Router\RouterRoute;
use Pecee\Router\SimpleRouter; use Pecee\Router\Router;
class Router { class SimpleRouter {
public static function init() { public static function init($defaultNamespace = null) {
SimpleRouter::GetInstance()->routeRequest(); $router = Router::GetInstance();
$router->setDefaultControllerNamespace($defaultNamespace);
$router->routeRequest();
} }
public static function get($url, $callback) { public static function get($url, $callback) {
$route = new RouterRoute($url, $callback); $route = new RouterRoute($url, $callback);
$route->addRequestType(RouterRoute::REQUEST_TYPE_GET); $route->addRequestType(RouterRoute::REQUEST_TYPE_GET);
$router = SimpleRouter::GetInstance(); $router = Router::GetInstance();
$router->addRoute($route); $router->addRoute($route);
return $route; return $route;
@@ -33,7 +35,7 @@ class Router {
$route = new RouterRoute($url, $callback); $route = new RouterRoute($url, $callback);
$route->addRequestType(RouterRoute::REQUEST_TYPE_POST); $route->addRequestType(RouterRoute::REQUEST_TYPE_POST);
$router = SimpleRouter::GetInstance(); $router = Router::GetInstance();
$router->addRoute($route); $router->addRoute($route);
return $route; return $route;
@@ -43,7 +45,7 @@ class Router {
$route = new RouterRoute($url, $callback); $route = new RouterRoute($url, $callback);
$route->addRequestType(RouterRoute::REQUEST_TYPE_PUT); $route->addRequestType(RouterRoute::REQUEST_TYPE_PUT);
$router = SimpleRouter::GetInstance(); $router = Router::GetInstance();
$router->addRoute($route); $router->addRoute($route);
return $route; return $route;
@@ -53,7 +55,7 @@ class Router {
$route = new RouterRoute($url, $callback); $route = new RouterRoute($url, $callback);
$route->addRequestType(RouterRoute::REQUEST_TYPE_DELETE); $route->addRequestType(RouterRoute::REQUEST_TYPE_DELETE);
$router = SimpleRouter::GetInstance(); $router = Router::GetInstance();
$router->addRoute($route); $router->addRoute($route);
return $route; return $route;
@@ -67,7 +69,7 @@ class Router {
$group->setSettings($settings); $group->setSettings($settings);
} }
$router = SimpleRouter::GetInstance(); $router = Router::GetInstance();
$router->addRoute($group); $router->addRoute($group);
return $group; return $group;
@@ -79,7 +81,7 @@ class Router {
$route->addRequestType($requestType); $route->addRequestType($requestType);
} }
$router = SimpleRouter::GetInstance(); $router = Router::GetInstance();
$router->addRoute($route); $router->addRoute($route);
return $route; return $route;
@@ -1,7 +1,7 @@
<?php <?php
namespace Pecee\Router; namespace Pecee\Router;
class SimpleRouter { class Router {
protected static $instance; protected static $instance;
@@ -11,6 +11,7 @@ class SimpleRouter {
protected $requestUri; protected $requestUri;
protected $requestMethod; protected $requestMethod;
protected $loadedClass; protected $loadedClass;
protected $defaultControllerNamespace;
public function __construct() { public function __construct() {
$this->routes = array(); $this->routes = array();
@@ -20,6 +21,11 @@ class SimpleRouter {
} }
public function addRoute(RouterEntry $route) { public function addRoute(RouterEntry $route) {
// Add default namespace
if(!$route->getNamespace() && $this->defaultControllerNamespace !== null) {
$route->setNamespace($this->defaultControllerNamespace);
}
if($this->currentRoute !== null) { if($this->currentRoute !== null) {
$this->backstack[] = $route; $this->backstack[] = $route;
} else { } 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) { if(self::$instance === null) {
self::$instance = new self(); self::$instance = new self();
} }
@@ -3,7 +3,7 @@
namespace Pecee\Router; namespace Pecee\Router;
use Pecee\Registry; use Pecee\Registry;
use Pecee\SimpleRouter; use Pecee\Router;
class RouterRoute extends RouterEntry { class RouterRoute extends RouterEntry {