mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 12:22:13 +00:00
[TASK] Updated
This commit is contained in:
@@ -41,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\SimpleRouter;
|
use Pecee\SimpleRouter\SimpleRouter;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This route will match the url /v1/services/answers/1/
|
* This route will match the url /v1/services/answers/1/
|
||||||
@@ -63,13 +63,14 @@ SimpleRouter::group(['prefix' => 'v1', 'middleware' => '\MyWebsite\Middleware\So
|
|||||||
|
|
||||||
### Doing it the object oriented (hardcore) way
|
### Doing it the object oriented (hardcore) way
|
||||||
|
|
||||||
The ```Router``` class is just a simple helper class that knows how to communicate with the ```SimpleRouter``` class. If you are up for a challenge, want the full control or simply just want to create your own ```Router``` helper class, this example is for you.
|
The ```SimpleRouter``` class referenced in the previous example, is just a simple helper class that knows how to communicate with the ```RouterBase``` class.
|
||||||
|
If you are up for a challenge, want the full control or simply just want to create your own ```Router``` helper class, this example is for you.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
use \Pecee\SimpleRouter;
|
use \Pecee\SimpleRouter\RouterBase;
|
||||||
use \Pecee\SimpleRouter\Router;
|
use \Pecee\SimpleRouter\RouterRoute;
|
||||||
|
|
||||||
$router = Router::getInstance();
|
$router = RouterBase::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');
|
||||||
@@ -83,6 +84,78 @@ $route->setPrefix('v1');
|
|||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
This is a simple example of an integration into a framework.
|
||||||
|
|
||||||
|
The framework has it's own ```Router``` class which inherits from the ```SimpleRouter``` class. This allows the framework to add custom functionality.
|
||||||
|
|
||||||
|
```php
|
||||||
|
namespace MyProject;
|
||||||
|
|
||||||
|
use Pecee\Handler\ExceptionHandler;
|
||||||
|
use Pecee\SimpleRouter\SimpleRouter;
|
||||||
|
|
||||||
|
class Router extends SimpleRouter {
|
||||||
|
|
||||||
|
protected static $exceptionHandlers = array();
|
||||||
|
|
||||||
|
public static function start() {
|
||||||
|
|
||||||
|
Debug::getInstance()->add('Router initialised.');
|
||||||
|
|
||||||
|
// Load routes.php
|
||||||
|
$file = $_ENV['basePath'] . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'routes.php';
|
||||||
|
if(file_exists($file)) {
|
||||||
|
require_once $file;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init locale settings
|
||||||
|
Locale::getInstance();
|
||||||
|
|
||||||
|
// Set default namespace
|
||||||
|
$defaultNamespace = '\\'.Registry::getInstance()->get('AppName') . '\\Controller';
|
||||||
|
|
||||||
|
// Handle exceptions
|
||||||
|
try {
|
||||||
|
parent::start($defaultNamespace); // TODO: Change the autogenerated stub
|
||||||
|
} catch(\Exception $e) {
|
||||||
|
/* @var $handler ExceptionHandler */
|
||||||
|
foreach(self::$exceptionHandlers as $handler) {
|
||||||
|
$class = new $handler();
|
||||||
|
$class->handleError($e);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function addExceptionHandler($handler) {
|
||||||
|
self::$exceptionHandlers[] = $handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
This is a basic example of a ```helper.php``` for generating urls.
|
||||||
|
|
||||||
|
```php
|
||||||
|
use Pecee\SimpleRouter\RouterBase;
|
||||||
|
function url($controller, $parameters = null, $getParams = null) {
|
||||||
|
RouterBase::getInstance()->getRoute($controller, $parameters, $getParams);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
In ```routes.php``` we have added this route:
|
||||||
|
|
||||||
|
```Router::get('/item/{id}', 'myController@show');```
|
||||||
|
|
||||||
|
In the template we call:
|
||||||
|
|
||||||
|
```url('myController@show', ['id' => 22], ['category' => 'shoes']);```
|
||||||
|
|
||||||
|
Result url is:
|
||||||
|
|
||||||
|
```/item/22?category=shoes```
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
While I work on a better documentation, please refer to the Laravel 5 routing documentation here:
|
While I work on a better documentation, please refer to the Laravel 5 routing documentation here:
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Pecee\SimpleRouter;
|
namespace Pecee\SimpleRouter;
|
||||||
|
|
||||||
class Router {
|
use Pecee\Url;
|
||||||
|
|
||||||
|
class RouterBase {
|
||||||
|
|
||||||
protected static $instance;
|
protected static $instance;
|
||||||
|
|
||||||
protected $currentRoute;
|
protected $currentRoute;
|
||||||
protected $routes;
|
protected $routes;
|
||||||
|
protected $controllerUrlMap;
|
||||||
protected $backstack;
|
protected $backstack;
|
||||||
protected $requestUri;
|
protected $requestUri;
|
||||||
protected $requestMethod;
|
protected $requestMethod;
|
||||||
@@ -16,16 +19,12 @@ class Router {
|
|||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->routes = array();
|
$this->routes = array();
|
||||||
$this->backstack = array();
|
$this->backstack = array();
|
||||||
|
$this->controllerUrlMap = array();
|
||||||
$this->requestUri = rtrim($_SERVER['REQUEST_URI'], '/');
|
$this->requestUri = rtrim($_SERVER['REQUEST_URI'], '/');
|
||||||
$this->requestMethod = strtolower(isset($_GET['_method']) ? $_GET['_method'] : $_SERVER['REQUEST_METHOD']);
|
$this->requestMethod = strtolower(isset($_GET['_method']) ? $_GET['_method'] : $_SERVER['REQUEST_METHOD']);
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
@@ -33,12 +32,6 @@ class Router {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function route($url, $callback) {
|
|
||||||
$route = new RouterRoute($url, $callback);
|
|
||||||
$this->addRoute($route);
|
|
||||||
return $route;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function loadClass($name) {
|
protected function loadClass($name) {
|
||||||
if(!class_exists($name)) {
|
if(!class_exists($name)) {
|
||||||
throw new RouterException(sprintf('Class %s does not exist', $name));
|
throw new RouterException(sprintf('Class %s does not exist', $name));
|
||||||
@@ -55,6 +48,11 @@ class Router {
|
|||||||
$this->loadClass($route->getMiddleware());
|
$this->loadClass($route->getMiddleware());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add default namespace
|
||||||
|
if(!$route->getNamespace() && $this->defaultControllerNamespace !== null) {
|
||||||
|
$route->setNamespace($this->defaultControllerNamespace);
|
||||||
|
}
|
||||||
|
|
||||||
if(is_object($route->getCallback()) && is_callable($route->getCallback())) {
|
if(is_object($route->getCallback()) && is_callable($route->getCallback())) {
|
||||||
|
|
||||||
// When the callback is a function
|
// When the callback is a function
|
||||||
@@ -64,6 +62,7 @@ class Router {
|
|||||||
// When the callback is a method
|
// When the callback is a method
|
||||||
|
|
||||||
$controller = explode('@', $route->getCallback());
|
$controller = explode('@', $route->getCallback());
|
||||||
|
|
||||||
$class = $route->getNamespace() . '\\' . $controller[0];
|
$class = $route->getNamespace() . '\\' . $controller[0];
|
||||||
|
|
||||||
$class = $this->loadClass($class);
|
$class = $this->loadClass($class);
|
||||||
@@ -80,7 +79,7 @@ class Router {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function renderBackstack(array $routes, &$settings, &$prefixes) {
|
protected function processRoutes(array $routes, array &$settings = array(), array &$prefixes = array()) {
|
||||||
// Loop through each route-request
|
// Loop through each route-request
|
||||||
/* @var $route RouterEntry */
|
/* @var $route RouterEntry */
|
||||||
foreach($routes as $route) {
|
foreach($routes as $route) {
|
||||||
@@ -90,10 +89,16 @@ class Router {
|
|||||||
array_push($prefixes, $route->getPrefix());
|
array_push($prefixes, $route->getPrefix());
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the route is a group
|
$route->setSettings($settings);
|
||||||
|
|
||||||
if($route instanceof RouterRoute) {
|
if($route instanceof RouterRoute) {
|
||||||
$route->setSettings($settings);
|
if(is_array($prefixes) && count($prefixes)) {
|
||||||
$route->setUrl( '/' . join('/', $prefixes) . $route->getUrl() );
|
$route->setUrl( '/' . join('/', $prefixes) . $route->getUrl() );
|
||||||
|
}
|
||||||
|
|
||||||
|
if(stripos($route->getCallback(), '@') !== false) {
|
||||||
|
$this->controllerUrlMap[$route->getCallback()] = $route;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop if the route matches
|
// Stop if the route matches
|
||||||
@@ -102,38 +107,20 @@ class Router {
|
|||||||
$this->renderRoute($route);
|
$this->renderRoute($route);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove itself from backstack
|
if(count($this->backstack)) {
|
||||||
array_shift($this->backstack);
|
// Remove itself from backstack
|
||||||
|
array_shift($this->backstack);
|
||||||
|
|
||||||
// Route any routes added to the backstack
|
// Route any routes added to the backstack
|
||||||
$this->renderBackstack($this->backstack, $settings, $prefixes);
|
$this->processRoutes($this->backstack, $settings, $prefixes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function routeRequest() {
|
public function routeRequest() {
|
||||||
// Loop through each route-request
|
// Loop through each route-request
|
||||||
/* @var $route RouterEntry */
|
|
||||||
foreach($this->routes as $route) {
|
|
||||||
|
|
||||||
// Reset variables
|
$this->processRoutes($this->routes);
|
||||||
$settings = array();
|
|
||||||
$prefixes = array();
|
|
||||||
|
|
||||||
$settings = array_merge($settings, $route->getMergeableSettings());
|
|
||||||
|
|
||||||
if($route->getPrefix()) {
|
|
||||||
array_push($prefixes, $route->getPrefix());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop if the route matches
|
|
||||||
$route = $route->getRoute($this->requestMethod, $this->requestUri);
|
|
||||||
if($route) {
|
|
||||||
$this->renderRoute($route);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Route any routes added to the backstack
|
|
||||||
$this->renderBackstack($this->backstack, $settings, $prefixes);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -192,6 +179,36 @@ class Router {
|
|||||||
return $this->routes;
|
return $this->routes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getRoute($controller, $parameters = null, $getParams = null) {
|
||||||
|
/* @var $route RouterRoute */
|
||||||
|
foreach($this->controllerUrlMap as $c => $route) {
|
||||||
|
$params = $route->getParameters();
|
||||||
|
|
||||||
|
if(strtolower($c) === strtolower($controller)) {
|
||||||
|
|
||||||
|
$url = $route->getUrl();
|
||||||
|
|
||||||
|
$i = 0;
|
||||||
|
foreach($params as $param => $value) {
|
||||||
|
$value = (isset($parameters[$param])) ? $parameters[$param] : $value;
|
||||||
|
$url = str_ireplace('{' . $param. '}', $value, $route->getUrl());
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$p = '';
|
||||||
|
if($getParams !== null) {
|
||||||
|
$p = '?'.Url::arrayToParams($getParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
$url .= $p;
|
||||||
|
|
||||||
|
return $url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return '/';
|
||||||
|
}
|
||||||
|
|
||||||
public static function getInstance() {
|
public static function getInstance() {
|
||||||
if(self::$instance === null) {
|
if(self::$instance === null) {
|
||||||
self::$instance = new self();
|
self::$instance = new self();
|
||||||
@@ -27,18 +27,6 @@ abstract class RouterEntry {
|
|||||||
$this->parameters = array();
|
$this->parameters = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function parseParameter($path) {
|
|
||||||
$parameters = array();
|
|
||||||
|
|
||||||
preg_match('/{([A-Za-z\-\_]*?)}/is', $path, $parameters);
|
|
||||||
|
|
||||||
if(isset($parameters[1]) && count($parameters[1]) > 0) {
|
|
||||||
return $parameters[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $callback
|
* @param string $callback
|
||||||
* @return self;
|
* @return self;
|
||||||
|
|||||||
@@ -11,12 +11,36 @@ class RouterRoute extends RouterEntry {
|
|||||||
|
|
||||||
public function __construct($url, $callback) {
|
public function __construct($url, $callback) {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->url = $url;
|
$this->setUrl($url);
|
||||||
$this->callback = $callback;
|
$this->setCallback($callback);
|
||||||
|
|
||||||
$this->settings['aliases'] = array();
|
$this->settings['aliases'] = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function parseParameters($url) {
|
||||||
|
$parameters = array();
|
||||||
|
|
||||||
|
preg_match_all('/{([A-Za-z\-\_]*?)}/is', $url, $parameters);
|
||||||
|
|
||||||
|
if(isset($parameters[1]) && count($parameters[1]) > 0) {
|
||||||
|
return $parameters[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function parseParameter($path) {
|
||||||
|
$parameters = array();
|
||||||
|
|
||||||
|
preg_match('/{([A-Za-z\-\_]*?)}/is', $path, $parameters);
|
||||||
|
|
||||||
|
if(isset($parameters[1]) && count($parameters[1]) > 0) {
|
||||||
|
return $parameters[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public function getRoute($requestMethod, &$url) {
|
public function getRoute($requestMethod, &$url) {
|
||||||
|
|
||||||
// Check if request method is allowed
|
// Check if request method is allowed
|
||||||
@@ -73,6 +97,15 @@ class RouterRoute extends RouterEntry {
|
|||||||
* @return self
|
* @return self
|
||||||
*/
|
*/
|
||||||
public function setUrl($url) {
|
public function setUrl($url) {
|
||||||
|
|
||||||
|
$parameters = $this->parseParameters($url);
|
||||||
|
|
||||||
|
if($parameters !== null) {
|
||||||
|
foreach($parameters as $param) {
|
||||||
|
$this->parameters[$param] = '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$this->url = $url;
|
$this->url = $url;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,16 +7,12 @@
|
|||||||
* 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\SimpleRouter\RouterGroup;
|
|
||||||
use Pecee\SimpleRouter\RouterRoute;
|
|
||||||
use Pecee\SimpleRouter\Router;
|
|
||||||
|
|
||||||
class SimpleRouter {
|
class SimpleRouter {
|
||||||
|
|
||||||
public static function init($defaultNamespace = null) {
|
public static function start($defaultNamespace = null) {
|
||||||
$router = Router::GetInstance();
|
$router = RouterBase::GetInstance();
|
||||||
$router->setDefaultControllerNamespace($defaultNamespace);
|
$router->setDefaultControllerNamespace($defaultNamespace);
|
||||||
$router->routeRequest();
|
$router->routeRequest();
|
||||||
}
|
}
|
||||||
@@ -25,7 +21,7 @@ class SimpleRouter {
|
|||||||
$route = new RouterRoute($url, $callback);
|
$route = new RouterRoute($url, $callback);
|
||||||
$route->addRequestType(RouterRoute::REQUEST_TYPE_GET);
|
$route->addRequestType(RouterRoute::REQUEST_TYPE_GET);
|
||||||
|
|
||||||
$router = Router::GetInstance();
|
$router = RouterBase::getInstance();
|
||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
@@ -35,7 +31,7 @@ class SimpleRouter {
|
|||||||
$route = new RouterRoute($url, $callback);
|
$route = new RouterRoute($url, $callback);
|
||||||
$route->addRequestType(RouterRoute::REQUEST_TYPE_POST);
|
$route->addRequestType(RouterRoute::REQUEST_TYPE_POST);
|
||||||
|
|
||||||
$router = Router::GetInstance();
|
$router = RouterBase::getInstance();
|
||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
@@ -45,7 +41,7 @@ class SimpleRouter {
|
|||||||
$route = new RouterRoute($url, $callback);
|
$route = new RouterRoute($url, $callback);
|
||||||
$route->addRequestType(RouterRoute::REQUEST_TYPE_PUT);
|
$route->addRequestType(RouterRoute::REQUEST_TYPE_PUT);
|
||||||
|
|
||||||
$router = Router::GetInstance();
|
$router = RouterBase::getInstance();
|
||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
@@ -55,7 +51,7 @@ class SimpleRouter {
|
|||||||
$route = new RouterRoute($url, $callback);
|
$route = new RouterRoute($url, $callback);
|
||||||
$route->addRequestType(RouterRoute::REQUEST_TYPE_DELETE);
|
$route->addRequestType(RouterRoute::REQUEST_TYPE_DELETE);
|
||||||
|
|
||||||
$router = Router::GetInstance();
|
$router = RouterBase::getInstance();
|
||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
@@ -69,7 +65,7 @@ class SimpleRouter {
|
|||||||
$group->setSettings($settings);
|
$group->setSettings($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
$router = Router::GetInstance();
|
$router = RouterBase::getInstance();
|
||||||
$router->addRoute($group);
|
$router->addRoute($group);
|
||||||
|
|
||||||
return $group;
|
return $group;
|
||||||
@@ -81,10 +77,14 @@ class SimpleRouter {
|
|||||||
$route->addRequestType($requestType);
|
$route->addRequestType($requestType);
|
||||||
}
|
}
|
||||||
|
|
||||||
$router = Router::GetInstance();
|
$router = RouterBase::getInstance();
|
||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function ressource($controller, $settings = array()) {
|
||||||
|
// not yet implemented
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user