Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/Pecee/SimpleRouter/RouterBase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Pecee\SimpleRouter;

use Pecee\ArrayUtil;
use Pecee\Http\Request;
use Pecee\Url;

Expand Down Expand Up @@ -245,15 +246,13 @@ public function getRoute($controller = null, $parameters = null, $getParams = nu
}
}

// Nothing found - return current route
if($this->loadedRoute) {
$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));
}
$controller = ($controller === null) ? '/' : $controller;
$url = array($controller);

return '/';
if(is_array($parameters)) {
ArrayUtil::append($url, $parameters);
}
return join('/', $url);
}

public static function getInstance() {
Expand Down
12 changes: 12 additions & 0 deletions src/Pecee/SimpleRouter/RouterEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ abstract class RouterEntry {
protected $callback;
protected $parameters;
protected $parametersRegex;
protected $regexMatch;

public function __construct() {
$this->settings = array();
Expand Down Expand Up @@ -158,6 +159,17 @@ public function where(array $options) {
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.
*
Expand Down
73 changes: 45 additions & 28 deletions src/Pecee/SimpleRouter/RouterRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class RouterRoute extends RouterEntry {

const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?)}';

protected $url;
protected $requestTypes;

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

protected function parseParameters($url, $multiple = false) {
protected function parseParameters($url, $multiple = false, $regex = self::PARAMETERS_REGEX_MATCH) {
$parameters = array();

if($multiple) {
preg_match_all('/{([A-Za-z\-\_]*?)}/is', $url, $parameters);
preg_match_all('/'.$regex.'/is', $url, $parameters);
} else {
preg_match('/{([A-Za-z\-\_]*?)}/is', $url, $parameters);
preg_match('/'.$regex.'/is', $url, $parameters);
}

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

$url = explode('/', trim($url, '/'));
$route = explode('/', trim($this->url, '/'));
$route = $this->url;

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

$parameters = array();
// Check if url parameter count matches
if(stripos($url, $routeMatch) === 0) {

$matches = true;

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

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

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

if($regex !== null) {
// Use the regular expression rule provided to filter the value
$matches = array();
preg_match('/'.$regex.'/is', $url[$i], $matches);
$url = explode('/', $url);
$route = explode('/', $route);

if(count($matches)) {
$parameterValue = $matches[0];
}
$parameters = array();

// Check if url matches
foreach ($route as $i => $path) {
$parameter = $this->parseParameters($path, false);

// Check if parameter of path matches, otherwise quit..
if (is_null($parameter) && strtolower($path) != strtolower($url[$i])) {
$matches = false;
break;
}

// Add parameter value
$parameters[$parameter] = $parameterValue;
// 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;
}
}
}

Expand Down