mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
@@ -23,7 +23,7 @@ Add the latest version pf Simple PHP Router to your ```composer.json```
|
|||||||
- Named Routes
|
- Named Routes
|
||||||
- Sub-Domain Routing
|
- Sub-Domain Routing
|
||||||
- CSRF Protection
|
- CSRF Protection
|
||||||
- Optinal/required parameters
|
- Optional/required parameters
|
||||||
|
|
||||||
## Initialising the router
|
## Initialising the router
|
||||||
|
|
||||||
@@ -44,7 +44,7 @@ SimpleRouter::init($defaultControllerNamespace);
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Adding routes
|
## Adding routes
|
||||||
Remember the ```routes.php``` file you required in your ```index.php```? This file will contain all your custom rules for routing.
|
Remember the ```routes.php``` file you required in your ```index.php```? This file will contain all your custom rules for routing.
|
||||||
This router is heavily inspired by the Laravel 5.* router, so anything you find in the Laravel documentation should work here as well.
|
This router is heavily inspired by the Laravel 5.* router, so anything you find in the Laravel documentation should work here as well.
|
||||||
|
|
||||||
### Basic example
|
### Basic example
|
||||||
@@ -54,8 +54,8 @@ use Pecee\SimpleRouter\SimpleRouter;
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* This route will match the url /v1/services/answers/1/
|
* This route will match the url /v1/services/answers/1/
|
||||||
|
|
||||||
* The middleware is just a class that renders before the
|
* The middleware is just a class that renders before the
|
||||||
* Controller or callback is loaded. This is useful for stopping
|
* Controller or callback is loaded. This is useful for stopping
|
||||||
* the request, for instance if a user is not authenticated.
|
* the request, for instance if a user is not authenticated.
|
||||||
*/
|
*/
|
||||||
@@ -66,19 +66,19 @@ SimpleRouter::group(['prefix' => 'v1', 'middleware' => '\MyWebsite\Middleware\So
|
|||||||
|
|
||||||
SimpleRouter::get('/answers/{id}', 'ControllerAnswers@show')
|
SimpleRouter::get('/answers/{id}', 'ControllerAnswers@show')
|
||||||
->where(['id' => '[0-9]+');
|
->where(['id' => '[0-9]+');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This example will route url when matching the regular expression to the method.
|
* This example will route url when matching the regular expression to the method.
|
||||||
* For example route: /ajax/music/world -> ControllerAjax@process (parameter: music/world)
|
* For example route: /ajax/music/world -> ControllerAjax@process (parameter: music/world)
|
||||||
*/
|
*/
|
||||||
SimpleRouter::all('/ajax', 'ControllerAjax@process')->match('ajax\\/([A-Za-z0-9\\/]+)');
|
SimpleRouter::all('/ajax', 'ControllerAjax@process')->match('ajax\\/([A-Za-z0-9\\/]+)');
|
||||||
|
|
||||||
// Resetful ressource
|
// Resetful resource
|
||||||
SimpleRouter::ressource('/rest', 'ControllerRessource');
|
SimpleRouter::resource('/rest', 'ControllerRessource');
|
||||||
|
|
||||||
// Load the entire controller (where url matches method names - getIndex(), postIndex() etc)
|
// Load the entire controller (where url matches method names - getIndex(), postIndex() etc)
|
||||||
SimpleRouter::controller('/controller', 'ControllerDefault');
|
SimpleRouter::controller('/controller', 'ControllerDefault');
|
||||||
|
|
||||||
// Example of providing callback instead of Controller
|
// Example of providing callback instead of Controller
|
||||||
SimpleRouter::get('/something', function() {
|
SimpleRouter::get('/something', function() {
|
||||||
die('Callback example');
|
die('Callback example');
|
||||||
@@ -90,7 +90,7 @@ SimpleRouter::group(['prefix' => 'v1', 'middleware' => '\MyWebsite\Middleware\So
|
|||||||
|
|
||||||
### Doing it the object oriented (hardcore) way
|
### Doing it the object oriented (hardcore) way
|
||||||
|
|
||||||
The ```SimpleRouter``` class referenced in the previous example, is just a simple helper class that knows how to communicate with the ```RouterBase``` class.
|
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.
|
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
|
||||||
@@ -177,7 +177,7 @@ In ```routes.php``` we have added this route:
|
|||||||
|
|
||||||
In the template we then call:
|
In the template we then call:
|
||||||
|
|
||||||
```url('myController@show', ['id' => 22], ['category' => 'shoes']);```
|
```url('myController@show', ['id' => 22], ['category' => 'shoes']);```
|
||||||
|
|
||||||
Result url is:
|
Result url is:
|
||||||
|
|
||||||
@@ -189,11 +189,11 @@ While I work on a better documentation, please refer to the Laravel 5 routing do
|
|||||||
http://laravel.com/docs/5.1/routing
|
http://laravel.com/docs/5.1/routing
|
||||||
|
|
||||||
## Easily extendable
|
## Easily extendable
|
||||||
The router can be easily extended to customize your needs.
|
The router can be easily extended to customize your needs.
|
||||||
|
|
||||||
## The MIT License (MIT)
|
## The MIT License (MIT)
|
||||||
|
|
||||||
Copyright (c) 2015 Simon Sessingø / simple-php-router
|
Copyright (c) 2015 Simon Sessing� / simple-php-router
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@@ -211,4 +211,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
@@ -22,6 +22,11 @@ class Response {
|
|||||||
*/
|
*/
|
||||||
public function redirect($url) {
|
public function redirect($url) {
|
||||||
header('location: ' . $url);
|
header('location: ' . $url);
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function refresh() {
|
||||||
|
$this->redirect(url());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -94,16 +94,32 @@ class RouterBase {
|
|||||||
return strcmp($b->getUrl(), $a->getUrl());
|
return strcmp($b->getUrl(), $a->getUrl());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$routeNotAllowed = false;
|
||||||
|
|
||||||
|
/* @var $route RouterEntry */
|
||||||
foreach($this->controllerUrlMap as $route) {
|
foreach($this->controllerUrlMap as $route) {
|
||||||
$routeMatch = $route->matchRoute($this->request);
|
$routeMatch = $route->matchRoute($this->request);
|
||||||
|
|
||||||
|
|
||||||
if($routeMatch && !($routeMatch instanceof RouterGroup)) {
|
if($routeMatch && !($routeMatch instanceof RouterGroup)) {
|
||||||
|
|
||||||
|
if(count($route->getRequestMethods()) && !in_array($this->request->getMethod(), $route->getRequestMethods())) {
|
||||||
|
$routeNotAllowed = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$routeNotAllowed = false;
|
||||||
|
|
||||||
$this->loadedRoute = $routeMatch;
|
$this->loadedRoute = $routeMatch;
|
||||||
$routeMatch->renderRoute($this->request);
|
$routeMatch->renderRoute($this->request);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if($routeNotAllowed) {
|
||||||
|
throw new RouterException('Route or method not allowed', 403);
|
||||||
|
}
|
||||||
|
|
||||||
if(!$this->loadedRoute) {
|
if(!$this->loadedRoute) {
|
||||||
throw new RouterException(sprintf('Route not found: %s', $this->request->getUri()), 404);
|
throw new RouterException(sprintf('Route not found: %s', $this->request->getUri()), 404);
|
||||||
}
|
}
|
||||||
@@ -165,13 +181,13 @@ class RouterBase {
|
|||||||
|
|
||||||
protected function processUrl($route, $method = null, $parameters = null, $getParams = null) {
|
protected function processUrl($route, $method = null, $parameters = null, $getParams = null) {
|
||||||
|
|
||||||
$url = rtrim($route->getUrl(), '/') . '/';
|
$url = $route->getUrl();
|
||||||
|
|
||||||
if(($route instanceof RouterController || $route instanceof RouterRessource) && $method !== null) {
|
if(($route instanceof RouterController || $route instanceof RouterResource) && $method !== null) {
|
||||||
$url .= $method . '/';
|
$url .= $method;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($route instanceof RouterController || $route instanceof RouterRessource) {
|
if($route instanceof RouterController || $route instanceof RouterResource) {
|
||||||
if(count($parameters)) {
|
if(count($parameters)) {
|
||||||
$url .= join('/', $parameters);
|
$url .= join('/', $parameters);
|
||||||
}
|
}
|
||||||
@@ -182,18 +198,17 @@ class RouterBase {
|
|||||||
$i = 0;
|
$i = 0;
|
||||||
foreach($params as $param => $value) {
|
foreach($params as $param => $value) {
|
||||||
$value = (isset($parameters[$param])) ? $parameters[$param] : $value;
|
$value = (isset($parameters[$param])) ? $parameters[$param] : $value;
|
||||||
$url = str_ireplace('{' . $param. '}', $value, $route->getUrl());
|
$url = str_ireplace('{' . $param. '}', $value, $url);
|
||||||
$i++;
|
$i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$p = '';
|
$url = rtrim($url, '/') . '/';
|
||||||
if($getParams !== null && count($getParams)) {
|
|
||||||
$p = '?'.Url::arrayToParams($getParams);
|
|
||||||
}
|
|
||||||
|
|
||||||
$url .= $p;
|
if($getParams !== null && count($getParams)) {
|
||||||
|
$url .= '?'.Url::arrayToParams($getParams);
|
||||||
|
}
|
||||||
|
|
||||||
return $url;
|
return $url;
|
||||||
}
|
}
|
||||||
@@ -208,15 +223,24 @@ class RouterBase {
|
|||||||
throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null');
|
throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if($controller === null && $parameters === null) {
|
||||||
|
return $this->processUrl($this->loadedRoute, null, $getParams);
|
||||||
|
}
|
||||||
|
|
||||||
$c = '';
|
$c = '';
|
||||||
$method = null;
|
$method = null;
|
||||||
|
|
||||||
/* @var $route RouterRoute */
|
/* @var $route RouterRoute */
|
||||||
foreach($this->controllerUrlMap as $route) {
|
foreach($this->controllerUrlMap as $route) {
|
||||||
|
|
||||||
|
// Check an alias exist, if the matches - use it
|
||||||
|
if($route instanceof RouterRoute && strtolower($route->getAlias()) === strtolower($controller)) {
|
||||||
|
return $this->processUrl($route, $route->getMethod(), $parameters, $getParams);
|
||||||
|
}
|
||||||
|
|
||||||
if($route instanceof RouterRoute && !is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) {
|
if($route instanceof RouterRoute && !is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) {
|
||||||
$c = $route->getCallback();
|
$c = $route->getCallback();
|
||||||
} else if($route instanceof RouterController || $route instanceof RouterRessource) {
|
} else if($route instanceof RouterController || $route instanceof RouterResource) {
|
||||||
$c = $route->getController();
|
$c = $route->getController();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,7 +255,7 @@ class RouterBase {
|
|||||||
foreach($this->controllerUrlMap as $route) {
|
foreach($this->controllerUrlMap as $route) {
|
||||||
if($route instanceof RouterRoute && !is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) {
|
if($route instanceof RouterRoute && !is_callable($route->getCallback()) && stripos($route->getCallback(), '@') !== false) {
|
||||||
$c = $route->getClass();
|
$c = $route->getClass();
|
||||||
} else if($route instanceof RouterController || $route instanceof RouterRessource) {
|
} else if($route instanceof RouterController || $route instanceof RouterResource) {
|
||||||
$c = $route->getController();
|
$c = $route->getController();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,6 +276,7 @@ class RouterBase {
|
|||||||
if(is_array($parameters)) {
|
if(is_array($parameters)) {
|
||||||
ArrayUtil::append($url, $parameters);
|
ArrayUtil::append($url, $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
return join('/', $url);
|
return join('/', $url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ abstract class RouterEntry {
|
|||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->settings = array();
|
$this->settings = array();
|
||||||
|
$this->settings['requestMethods'] = array();
|
||||||
$this->parameters = array();
|
$this->parameters = array();
|
||||||
$this->parametersRegex = array();
|
$this->parametersRegex = array();
|
||||||
}
|
}
|
||||||
@@ -193,8 +194,10 @@ abstract class RouterEntry {
|
|||||||
* @param array $settings
|
* @param array $settings
|
||||||
* @return self
|
* @return self
|
||||||
*/
|
*/
|
||||||
public function addSettings(array $settings) {
|
public function addSettings(array $settings = null) {
|
||||||
$this->settings = array_merge($this->settings, $settings);
|
if(is_array($settings)) {
|
||||||
|
$this->settings = array_merge($this->settings, $settings);
|
||||||
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -280,6 +283,30 @@ abstract class RouterEntry {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set allowed request methods
|
||||||
|
*
|
||||||
|
* @param array $methods
|
||||||
|
* @return self $this
|
||||||
|
*/
|
||||||
|
public function setRequestMethods(array $methods) {
|
||||||
|
$this->settings['requestMethods'] = $methods;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get allowed requeset methods
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getRequestMethods() {
|
||||||
|
if(!isset($this->settings['requestMethods']) || isset($this->settings['requestMethods']) && !is_array($this->settings['requestMethods'])) {
|
||||||
|
$value = isset($this->settings['requestMethods']) ? $this->settings['requestMethods'] : null;
|
||||||
|
return array($value);
|
||||||
|
}
|
||||||
|
return $this->settings['requestMethods'];
|
||||||
|
}
|
||||||
|
|
||||||
abstract function matchRoute(Request $request);
|
abstract function matchRoute(Request $request);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -19,9 +19,9 @@ class RouterGroup extends RouterEntry {
|
|||||||
|
|
||||||
if($this->method) {
|
if($this->method) {
|
||||||
if(is_array($this->method)) {
|
if(is_array($this->method)) {
|
||||||
$hasAccess = (in_array($request->getMethod(), $this->method));
|
$hasAccess = (in_array($request->getMethod(), $this->getRequestMethods()));
|
||||||
} else {
|
} else {
|
||||||
$hasAccess = strtolower($this->method) == strtolower($request->getMethod());
|
$hasAccess = strtolower($this->getRequestMethods()) == strtolower($request->getMethod());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -3,7 +3,7 @@ namespace Pecee\SimpleRouter;
|
|||||||
|
|
||||||
use Pecee\Http\Request;
|
use Pecee\Http\Request;
|
||||||
|
|
||||||
class RouterRessource extends RouterEntry {
|
class RouterResource extends RouterEntry {
|
||||||
|
|
||||||
const DEFAULT_METHOD = 'index';
|
const DEFAULT_METHOD = 'index';
|
||||||
|
|
||||||
@@ -9,7 +9,6 @@ class RouterRoute extends RouterEntry {
|
|||||||
const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?)}';
|
const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?)}';
|
||||||
|
|
||||||
protected $url;
|
protected $url;
|
||||||
protected $requestTypes;
|
|
||||||
|
|
||||||
public function __construct($url, $callback) {
|
public function __construct($url, $callback) {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
@@ -17,7 +16,6 @@ class RouterRoute extends RouterEntry {
|
|||||||
$this->setCallback($callback);
|
$this->setCallback($callback);
|
||||||
|
|
||||||
$this->settings['aliases'] = array();
|
$this->settings['aliases'] = array();
|
||||||
$this->requestTypes = array();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function parseParameters($url, $multiple = false, $regex = self::PARAMETERS_REGEX_MATCH) {
|
protected function parseParameters($url, $multiple = false, $regex = self::PARAMETERS_REGEX_MATCH) {
|
||||||
@@ -39,71 +37,69 @@ class RouterRoute extends RouterEntry {
|
|||||||
public function matchRoute(Request $request) {
|
public function matchRoute(Request $request) {
|
||||||
|
|
||||||
// Check if request method is allowed
|
// Check if request method is allowed
|
||||||
if(count($this->requestTypes) === 0 || in_array($request->getMethod(), $this->requestTypes)) {
|
|
||||||
|
|
||||||
$url = parse_url($request->getUri());
|
$url = parse_url($request->getUri());
|
||||||
$url = $url['path'];
|
$url = $url['path'];
|
||||||
|
|
||||||
$route = $this->url;
|
$route = $this->url;
|
||||||
|
|
||||||
$routeMatch = preg_replace('/'.self::PARAMETERS_REGEX_MATCH.'/is', '', $route);
|
$routeMatch = preg_replace('/'.self::PARAMETERS_REGEX_MATCH.'/is', '', $route);
|
||||||
|
|
||||||
// Check if url parameter count matches
|
// Check if url parameter count matches
|
||||||
if(stripos($url, $routeMatch) === 0) {
|
if(stripos($url, $routeMatch) === 0) {
|
||||||
|
|
||||||
$matches = true;
|
$matches = true;
|
||||||
|
|
||||||
if($this->regexMatch) {
|
if($this->regexMatch) {
|
||||||
$parameters = $this->parseParameters($url, true, $this->regexMatch);
|
$parameters = $this->parseParameters($url, true, $this->regexMatch);
|
||||||
|
|
||||||
// If regex doesn't match, make sure to return an array
|
|
||||||
if(!is_array($parameters)) {
|
|
||||||
$parameters = array();
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
$url = explode('/', $url);
|
|
||||||
$route = explode('/', $route);
|
|
||||||
|
|
||||||
|
// If regex doesn't match, make sure to return an array
|
||||||
|
if(!is_array($parameters)) {
|
||||||
$parameters = array();
|
$parameters = array();
|
||||||
|
}
|
||||||
|
|
||||||
// Check if url matches
|
} else {
|
||||||
foreach ($route as $i => $path) {
|
|
||||||
$parameter = $this->parseParameters($path, false);
|
|
||||||
|
|
||||||
// Check if parameter of path matches, otherwise quit..
|
$url = explode('/', $url);
|
||||||
if (is_null($parameter) && strtolower($path) != strtolower($url[$i])) {
|
$route = explode('/', $route);
|
||||||
$matches = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save parameter if we have one
|
$parameters = array();
|
||||||
if ($parameter) {
|
|
||||||
$parameterValue = $url[$i];
|
|
||||||
$regex = (isset($this->parametersRegex[$parameter]) ? $this->parametersRegex[$parameter] : null);
|
|
||||||
|
|
||||||
if ($regex !== null) {
|
// Check if url matches
|
||||||
// Use the regular expression rule provided to filter the value
|
foreach ($route as $i => $path) {
|
||||||
$matches = array();
|
$parameter = $this->parseParameters($path, false);
|
||||||
preg_match('/' . $regex . '/is', $url[$i], $matches);
|
|
||||||
|
|
||||||
if (count($matches)) {
|
// Check if parameter of path matches, otherwise quit..
|
||||||
$parameterValue = $matches[0];
|
if (is_null($parameter) && strtolower($path) != strtolower($url[$i])) {
|
||||||
}
|
$matches = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save parameter if we have one
|
||||||
|
if ($parameter) {
|
||||||
|
$parameterValue = $url[$i];
|
||||||
|
$regex = (isset($this->parametersRegex[$parameter]) ? $this->parametersRegex[$parameter] : null);
|
||||||
|
|
||||||
|
if ($regex !== null) {
|
||||||
|
// Use the regular expression rule provided to filter the value
|
||||||
|
$matches = array();
|
||||||
|
preg_match('/' . $regex . '/is', $url[$i], $matches);
|
||||||
|
|
||||||
|
if (count($matches)) {
|
||||||
|
$parameterValue = $matches[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add parameter value
|
|
||||||
$parameters[$parameter] = $parameterValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add parameter value
|
||||||
|
$parameters[$parameter] = $parameterValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// This route matches
|
// This route matches
|
||||||
if($matches) {
|
if($matches) {
|
||||||
$this->parameters = $parameters;
|
$this->parameters = $parameters;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,51 +133,32 @@ class RouterRoute extends RouterEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $aliases
|
* Get alias for the url which can be used when getting the url route.
|
||||||
* @return self
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function setAliases(array $aliases) {
|
public function getAlias(){
|
||||||
$this->aliases = $aliases;
|
return $this->alias;
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add alias
|
* Set the url alias for easier getting the url route.
|
||||||
*
|
* @param string $alias
|
||||||
* @param $alias
|
|
||||||
* @return self
|
* @return self
|
||||||
*/
|
*/
|
||||||
public function addAlias($alias) {
|
public function setAlias($alias){
|
||||||
$arr = $this->aliases;
|
$this->alias = $alias;
|
||||||
$arr[] = $alias;
|
|
||||||
$this->aliases = $arr;
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAliases() {
|
public function setSettings($settings) {
|
||||||
$this->aliases;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// Change as to alias
|
||||||
* Add request type
|
if(isset($settings{'as'})) {
|
||||||
*
|
$this->setAlias($settings['as']);
|
||||||
* @param $type
|
|
||||||
* @return self
|
|
||||||
* @throws RouterException
|
|
||||||
*/
|
|
||||||
public function addRequestType($type) {
|
|
||||||
if(!in_array($type, self::$allowedRequestTypes)) {
|
|
||||||
throw new RouterException('Invalid request method: ' . $type);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->requestTypes[] = $type;
|
return parent::setSettings($settings);
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getRequestTypes() {
|
|
||||||
return $this->requestTypes;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -17,9 +17,10 @@ class SimpleRouter {
|
|||||||
$router->routeRequest();
|
$router->routeRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function get($url, $callback) {
|
public static function get($url, $callback, array $settings = null) {
|
||||||
$route = new RouterRoute($url, $callback);
|
$route = new RouterRoute($url, $callback);
|
||||||
$route->addRequestType(RouterRoute::REQUEST_TYPE_GET);
|
$route->addSettings($settings);
|
||||||
|
$route->setRequestMethods(array(RouterRoute::REQUEST_TYPE_GET));
|
||||||
|
|
||||||
$router = RouterBase::getInstance();
|
$router = RouterBase::getInstance();
|
||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
@@ -27,9 +28,10 @@ class SimpleRouter {
|
|||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function post($url, $callback) {
|
public static function post($url, $callback, array $settings = null) {
|
||||||
$route = new RouterRoute($url, $callback);
|
$route = new RouterRoute($url, $callback);
|
||||||
$route->addRequestType(RouterRoute::REQUEST_TYPE_POST);
|
$route->addSettings($settings);
|
||||||
|
$route->setRequestMethods(array(RouterRoute::REQUEST_TYPE_POST));
|
||||||
|
|
||||||
$router = RouterBase::getInstance();
|
$router = RouterBase::getInstance();
|
||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
@@ -37,9 +39,10 @@ class SimpleRouter {
|
|||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function put($url, $callback) {
|
public static function put($url, $callback, array $settings = null) {
|
||||||
$route = new RouterRoute($url, $callback);
|
$route = new RouterRoute($url, $callback);
|
||||||
$route->addRequestType(RouterRoute::REQUEST_TYPE_PUT);
|
$route->addSettings($settings);
|
||||||
|
$route->setRequestMethods(array(RouterRoute::REQUEST_TYPE_PUT));
|
||||||
|
|
||||||
$router = RouterBase::getInstance();
|
$router = RouterBase::getInstance();
|
||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
@@ -47,9 +50,10 @@ class SimpleRouter {
|
|||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function delete($url, $callback) {
|
public static function delete($url, $callback, array $settings = null) {
|
||||||
$route = new RouterRoute($url, $callback);
|
$route = new RouterRoute($url, $callback);
|
||||||
$route->addRequestType(RouterRoute::REQUEST_TYPE_DELETE);
|
$route->addSettings($settings);
|
||||||
|
$route->setRequestMethods(array(RouterRoute::REQUEST_TYPE_DELETE));
|
||||||
|
|
||||||
$router = RouterBase::getInstance();
|
$router = RouterBase::getInstance();
|
||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
@@ -71,11 +75,10 @@ class SimpleRouter {
|
|||||||
return $group;
|
return $group;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function match(array $requestTypes, $url, $callback) {
|
public static function match(array $requestMethods, $url, $callback, array $settings = null) {
|
||||||
$route = new RouterRoute($url, $callback);
|
$route = new RouterRoute($url, $callback);
|
||||||
foreach($requestTypes as $requestType) {
|
$route->setRequestMethods($requestMethods);
|
||||||
$route->addRequestType($requestType);
|
$route->addSettings($settings);
|
||||||
}
|
|
||||||
|
|
||||||
$router = RouterBase::getInstance();
|
$router = RouterBase::getInstance();
|
||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
@@ -83,24 +86,27 @@ class SimpleRouter {
|
|||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function all($url, $callback) {
|
public static function all($url, $callback, array $settings = null) {
|
||||||
$route = new RouterRoute($url, $callback);
|
$route = new RouterRoute($url, $callback);
|
||||||
|
$route->addSettings($settings);
|
||||||
$router = RouterBase::getInstance();
|
$router = RouterBase::getInstance();
|
||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function controller($url, $controller) {
|
public static function controller($url, $controller, array $settings = null) {
|
||||||
$route = new RouterController($url, $controller);
|
$route = new RouterController($url, $controller);
|
||||||
|
$route->addSettings($settings);
|
||||||
$router = RouterBase::getInstance();
|
$router = RouterBase::getInstance();
|
||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function ressource($url, $controller) {
|
public static function resource($url, $controller, array $settings = null) {
|
||||||
$route = new RouterRessource($url, $controller);
|
$route = new RouterResource($url, $controller);
|
||||||
|
$route->addSettings($settings);
|
||||||
$router = RouterBase::getInstance();
|
$router = RouterBase::getInstance();
|
||||||
$router->addRoute($route);
|
$router->addRoute($route);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user