[FEATURE/BUGFIX] Improvements

- Added match method to match specific regular expression.
- GetRoute now returns provided controller and method if no match is found.
- Bugfixes and other minor improvements.
This commit is contained in:
Simon Sessingø
2015-10-18 23:18:58 +02:00
parent b3b362a9e6
commit c1a6c63dc7
3 changed files with 64 additions and 36 deletions
+7 -8
View File
@@ -1,6 +1,7 @@
<?php <?php
namespace Pecee\SimpleRouter; namespace Pecee\SimpleRouter;
use Pecee\ArrayUtil;
use Pecee\Http\Request; use Pecee\Http\Request;
use Pecee\Url; use Pecee\Url;
@@ -245,15 +246,13 @@ class RouterBase {
} }
} }
// Nothing found - return current route $controller = ($controller === null) ? '/' : $controller;
if($this->loadedRoute) { $url = array($controller);
$getParams = ($getParams === null) ? array() : $getParams;
$params = ($this->loadedRoute->getParameters() == null) ? array() : $this->loadedRoute->getParameters();
$parameters = ($parameters === null) ? array() : $parameters;
return $this->processUrl($this->loadedRoute, null, array_merge($params, $parameters), array_merge($_GET, $getParams));
}
return '/'; if(is_array($parameters)) {
ArrayUtil::append($url, $parameters);
}
return join('/', $url);
} }
public static function getInstance() { public static function getInstance() {
+12
View File
@@ -23,6 +23,7 @@ abstract class RouterEntry {
protected $callback; protected $callback;
protected $parameters; protected $parameters;
protected $parametersRegex; protected $parametersRegex;
protected $regexMatch;
public function __construct() { public function __construct() {
$this->settings = array(); $this->settings = array();
@@ -158,6 +159,17 @@ abstract class RouterEntry {
return $this; return $this;
} }
/**
* Add regular expression match for url
*
* @param string $regex
* @return self
*/
public function match($regex) {
$this->regexMatch = $regex;
return $this;
}
/** /**
* Get settings that are allowed to be inherited by child routes. * Get settings that are allowed to be inherited by child routes.
* *
+32 -15
View File
@@ -6,6 +6,8 @@ use Pecee\Http\Request;
class RouterRoute extends RouterEntry { class RouterRoute extends RouterEntry {
const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?)}';
protected $url; protected $url;
protected $requestTypes; protected $requestTypes;
@@ -18,13 +20,13 @@ class RouterRoute extends RouterEntry {
$this->requestTypes = array(); $this->requestTypes = array();
} }
protected function parseParameters($url, $multiple = false) { protected function parseParameters($url, $multiple = false, $regex = self::PARAMETERS_REGEX_MATCH) {
$parameters = array(); $parameters = array();
if($multiple) { if($multiple) {
preg_match_all('/{([A-Za-z\-\_]*?)}/is', $url, $parameters); preg_match_all('/'.$regex.'/is', $url, $parameters);
} else { } else {
preg_match('/{([A-Za-z\-\_]*?)}/is', $url, $parameters); preg_match('/'.$regex.'/is', $url, $parameters);
} }
if(isset($parameters[1]) && count($parameters[1]) > 0) { if(isset($parameters[1]) && count($parameters[1]) > 0) {
@@ -42,37 +44,51 @@ class RouterRoute extends RouterEntry {
$url = parse_url($request->getUri()); $url = parse_url($request->getUri());
$url = $url['path']; $url = $url['path'];
$url = explode('/', trim($url, '/')); $route = $this->url;
$route = explode('/', trim($this->url, '/'));
$routeMatch = preg_replace('/'.self::PARAMETERS_REGEX_MATCH.'/is', '', $route);
// Check if url parameter count matches // Check if url parameter count matches
if(count($url) === count($route)) { if(stripos($url, $routeMatch) === 0) {
$parameters = array();
$matches = true; $matches = true;
if($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);
$parameters = array();
// Check if url matches // Check if url matches
foreach($route as $i => $path) { foreach ($route as $i => $path) {
$parameter = $this->parseParameters($path); $parameter = $this->parseParameters($path, false);
// Check if parameter of path matches, otherwise quit.. // Check if parameter of path matches, otherwise quit..
if(is_null($parameter) && strtolower($path) != strtolower($url[$i])) { if (is_null($parameter) && strtolower($path) != strtolower($url[$i])) {
$matches = false; $matches = false;
break; break;
} }
// Save parameter if we have one // Save parameter if we have one
if($parameter) { if ($parameter) {
$parameterValue = $url[$i]; $parameterValue = $url[$i];
$regex = (isset($this->parametersRegex[$parameter]) ? $this->parametersRegex[$parameter] : null); $regex = (isset($this->parametersRegex[$parameter]) ? $this->parametersRegex[$parameter] : null);
if($regex !== null) { if ($regex !== null) {
// Use the regular expression rule provided to filter the value // Use the regular expression rule provided to filter the value
$matches = array(); $matches = array();
preg_match('/'.$regex.'/is', $url[$i], $matches); preg_match('/' . $regex . '/is', $url[$i], $matches);
if(count($matches)) { if (count($matches)) {
$parameterValue = $matches[0]; $parameterValue = $matches[0];
} }
} }
@@ -81,6 +97,7 @@ class RouterRoute extends RouterEntry {
$parameters[$parameter] = $parameterValue; $parameters[$parameter] = $parameterValue;
} }
} }
}
// This route matches // This route matches
if($matches) { if($matches) {