- 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:
```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');
@@ -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;
@@ -1,7 +1,7 @@
<?php
namespace Pecee\Router;
class SimpleRouter {
class Router {
protected static $instance;
@@ -11,6 +11,7 @@ class SimpleRouter {
protected $requestUri;
protected $requestMethod;
protected $loadedClass;
protected $defaultControllerNamespace;
public function __construct() {
$this->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();
}
@@ -3,7 +3,7 @@
namespace Pecee\Router;
use Pecee\Registry;
use Pecee\SimpleRouter;
use Pecee\Router;
class RouterRoute extends RouterEntry {