Compare commits

...

3 Commits

Author SHA1 Message Date
Simon Sessingø b6f0f6899a [TASK] Moved parameter stuff to RouterEntry class. 2015-10-14 20:48:17 +02:00
Simon Sessingø 93d8c26416 [BUGFIX] Fixed router for controller and ressources not matching /something? 2015-10-14 19:55:05 +02:00
Simon Sessingø aec1f5f10c [FEATURE] Features and optimisations
- Added where method to RouterRoute to support custom regular expression matches on routes.
- Moved parameters property to RouterRoute class.
- Added IRouteEntry class.
2015-10-14 13:54:53 +02:00
5 changed files with 53 additions and 21 deletions
+7
View File
@@ -0,0 +1,7 @@
<?php
namespace Pecee\SimpleRouter;
interface IRouteEntry {
}
+1 -2
View File
@@ -16,9 +16,8 @@ class RouterController extends RouterEntry {
}
public function matchRoute($requestMethod, $url) {
$url = parse_url($url);
$url = $url['path'];
$url = rtrim($url['path'], '/') . '/';
if(strtolower($url) == strtolower($this->url) || stripos($url, $this->url) === 0) {
+29 -16
View File
@@ -19,10 +19,12 @@ abstract class RouterEntry {
protected $settings;
protected $callback;
protected $parameters;
protected $parametersRegex;
public function __construct() {
$this->settings = array();
$this->parameters = array();
$this->parametersRegex = array();
}
protected function loadClass($name) {
@@ -63,22 +65,6 @@ abstract class RouterEntry {
return $this->callback;
}
/**
* @return mixed
*/
public function getParameters(){
return $this->parameters;
}
/**
* @param mixed $parameters
* @return self
*/
public function setParameters($parameters) {
$this->parameters = $parameters;
return $this;
}
/**
* @param string $prefix
* @return self
@@ -134,6 +120,33 @@ abstract class RouterEntry {
return $this->settings;
}
/**
* @return mixed
*/
public function getParameters(){
return $this->parameters;
}
/**
* @param mixed $parameters
* @return self
*/
public function setParameters($parameters) {
$this->parameters = $parameters;
return $this;
}
/**
* Add regular expression parameter match
*
* @param array $options
* @return self
*/
public function where(array $options) {
$this->parametersRegex = array_merge($this->parametersRegex, $options);
return $this;
}
/**
* Get settings that are allowed to be inherited by child routes.
*
+1 -1
View File
@@ -53,7 +53,7 @@ class RouterRessource extends RouterEntry {
public function matchRoute($requestMethod, $url) {
$url = parse_url($url);
$url = $url['path'];
$url = rtrim($url['path'], '/') . '/';
if(strtolower($url) == strtolower($this->url) || stripos($url, $this->url) === 0) {
$url = rtrim($url, '/');
+15 -2
View File
@@ -73,7 +73,21 @@ class RouterRoute extends RouterEntry {
// Save parameter if we have one
if($parameter) {
$parameters[$parameter] = $url[$i];
$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;
}
}
@@ -82,7 +96,6 @@ class RouterRoute extends RouterEntry {
$this->parameters = $parameters;
return $this;
}
}
}