mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-25 12:39:16 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b6f0f6899a | |||
| 93d8c26416 | |||
| aec1f5f10c |
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Pecee\SimpleRouter;
|
||||
|
||||
interface IRouteEntry {
|
||||
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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, '/');
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user