Skip to content

Commit 37b8090

Browse files
committed
Merge pull request #3 from skipperbent/development
[FEATURE/BUGFIX] Improvements
2 parents b3b362a + c1a6c63 commit 37b8090

File tree

3 files changed

+64
-36
lines changed

3 files changed

+64
-36
lines changed

src/Pecee/SimpleRouter/RouterBase.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Pecee\SimpleRouter;
33

4+
use Pecee\ArrayUtil;
45
use Pecee\Http\Request;
56
use Pecee\Url;
67

@@ -245,15 +246,13 @@ public function getRoute($controller = null, $parameters = null, $getParams = nu
245246
}
246247
}
247248

248-
// Nothing found - return current route
249-
if($this->loadedRoute) {
250-
$getParams = ($getParams === null) ? array() : $getParams;
251-
$params = ($this->loadedRoute->getParameters() == null) ? array() : $this->loadedRoute->getParameters();
252-
$parameters = ($parameters === null) ? array() : $parameters;
253-
return $this->processUrl($this->loadedRoute, null, array_merge($params, $parameters), array_merge($_GET, $getParams));
254-
}
249+
$controller = ($controller === null) ? '/' : $controller;
250+
$url = array($controller);
255251

256-
return '/';
252+
if(is_array($parameters)) {
253+
ArrayUtil::append($url, $parameters);
254+
}
255+
return join('/', $url);
257256
}
258257

259258
public static function getInstance() {

src/Pecee/SimpleRouter/RouterEntry.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ abstract class RouterEntry {
2323
protected $callback;
2424
protected $parameters;
2525
protected $parametersRegex;
26+
protected $regexMatch;
2627

2728
public function __construct() {
2829
$this->settings = array();
@@ -158,6 +159,17 @@ public function where(array $options) {
158159
return $this;
159160
}
160161

162+
/**
163+
* Add regular expression match for url
164+
*
165+
* @param string $regex
166+
* @return self
167+
*/
168+
public function match($regex) {
169+
$this->regexMatch = $regex;
170+
return $this;
171+
}
172+
161173
/**
162174
* Get settings that are allowed to be inherited by child routes.
163175
*

src/Pecee/SimpleRouter/RouterRoute.php

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class RouterRoute extends RouterEntry {
88

9+
const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?)}';
10+
911
protected $url;
1012
protected $requestTypes;
1113

@@ -18,13 +20,13 @@ public function __construct($url, $callback) {
1820
$this->requestTypes = array();
1921
}
2022

21-
protected function parseParameters($url, $multiple = false) {
23+
protected function parseParameters($url, $multiple = false, $regex = self::PARAMETERS_REGEX_MATCH) {
2224
$parameters = array();
2325

2426
if($multiple) {
25-
preg_match_all('/{([A-Za-z\-\_]*?)}/is', $url, $parameters);
27+
preg_match_all('/'.$regex.'/is', $url, $parameters);
2628
} else {
27-
preg_match('/{([A-Za-z\-\_]*?)}/is', $url, $parameters);
29+
preg_match('/'.$regex.'/is', $url, $parameters);
2830
}
2931

3032
if(isset($parameters[1]) && count($parameters[1]) > 0) {
@@ -42,43 +44,58 @@ public function matchRoute(Request $request) {
4244
$url = parse_url($request->getUri());
4345
$url = $url['path'];
4446

45-
$url = explode('/', trim($url, '/'));
46-
$route = explode('/', trim($this->url, '/'));
47+
$route = $this->url;
4748

48-
// Check if url parameter count matches
49-
if(count($url) === count($route)) {
49+
$routeMatch = preg_replace('/'.self::PARAMETERS_REGEX_MATCH.'/is', '', $route);
5050

51-
$parameters = array();
51+
// Check if url parameter count matches
52+
if(stripos($url, $routeMatch) === 0) {
5253

5354
$matches = true;
5455

55-
// Check if url matches
56-
foreach($route as $i => $path) {
57-
$parameter = $this->parseParameters($path);
56+
if($this->regexMatch) {
57+
$parameters = $this->parseParameters($url, true, $this->regexMatch);
5858

59-
// Check if parameter of path matches, otherwise quit..
60-
if(is_null($parameter) && strtolower($path) != strtolower($url[$i])) {
61-
$matches = false;
62-
break;
59+
// If regex doesn't match, make sure to return an array
60+
if(!is_array($parameters)) {
61+
$parameters = array();
6362
}
6463

65-
// Save parameter if we have one
66-
if($parameter) {
67-
$parameterValue = $url[$i];
68-
$regex = (isset($this->parametersRegex[$parameter]) ? $this->parametersRegex[$parameter] : null);
64+
} else {
6965

70-
if($regex !== null) {
71-
// Use the regular expression rule provided to filter the value
72-
$matches = array();
73-
preg_match('/'.$regex.'/is', $url[$i], $matches);
66+
$url = explode('/', $url);
67+
$route = explode('/', $route);
7468

75-
if(count($matches)) {
76-
$parameterValue = $matches[0];
77-
}
69+
$parameters = array();
70+
71+
// Check if url matches
72+
foreach ($route as $i => $path) {
73+
$parameter = $this->parseParameters($path, false);
74+
75+
// Check if parameter of path matches, otherwise quit..
76+
if (is_null($parameter) && strtolower($path) != strtolower($url[$i])) {
77+
$matches = false;
78+
break;
7879
}
7980

80-
// Add parameter value
81-
$parameters[$parameter] = $parameterValue;
81+
// Save parameter if we have one
82+
if ($parameter) {
83+
$parameterValue = $url[$i];
84+
$regex = (isset($this->parametersRegex[$parameter]) ? $this->parametersRegex[$parameter] : null);
85+
86+
if ($regex !== null) {
87+
// Use the regular expression rule provided to filter the value
88+
$matches = array();
89+
preg_match('/' . $regex . '/is', $url[$i], $matches);
90+
91+
if (count($matches)) {
92+
$parameterValue = $matches[0];
93+
}
94+
}
95+
96+
// Add parameter value
97+
$parameters[$parameter] = $parameterValue;
98+
}
8299
}
83100
}
84101

0 commit comments

Comments
 (0)