mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-18 01:07:51 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8509062e00 | |||
| 523d49359b | |||
| 9617cacc31 | |||
| 7cc2652bcd | |||
| 99088719ed | |||
| e26d55a810 | |||
| 3e41ee28b6 | |||
| 8f3ce68a5e | |||
| 751b4444ae | |||
| d25351f4f9 |
@@ -107,48 +107,48 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
||||
*/
|
||||
public function findUrl($method = null, $parameters = null, $name = null)
|
||||
{
|
||||
$url = '';
|
||||
|
||||
$parameters = (array)$parameters;
|
||||
$url = $this->getUrl();
|
||||
|
||||
if ($this->getGroup() !== null && count($this->getGroup()->getDomains()) > 0) {
|
||||
$url .= '//' . $this->getGroup()->getDomains()[0];
|
||||
$url = '//' . $this->getGroup()->getDomains()[0] . $url;
|
||||
}
|
||||
|
||||
$url .= $this->getUrl();
|
||||
|
||||
$params = array_merge($this->getParameters(), $parameters);
|
||||
|
||||
/* Url that contains parameters that aren't recognized */
|
||||
/* Contains parameters that aren't recognized and will be appended at the end of the url */
|
||||
$unknownParams = [];
|
||||
|
||||
/* Create the param string - {} */
|
||||
/* Create the param string - {parameter} */
|
||||
$param1 = $this->paramModifiers[0] . '%s' . $this->paramModifiers[1];
|
||||
|
||||
/* Create the param string with the optional symbol - {?} */
|
||||
/* Create the param string with the optional symbol - {parameter?} */
|
||||
$param2 = $this->paramModifiers[0] . '%s' . $this->paramOptionalSymbol . $this->paramModifiers[1];
|
||||
|
||||
/* Let's parse the values of any {} parameter in the url */
|
||||
/* Replace any {parameter} in the url with the correct value */
|
||||
|
||||
$params = $this->getParameters();
|
||||
$max = count($params) - 1;
|
||||
$keys = array_keys($params);
|
||||
|
||||
for ($i = $max; $i >= 0; $i--) {
|
||||
$param = $keys[$i];
|
||||
$value = $params[$param];
|
||||
$value = $value = ($parameters !== null && array_key_exists($param, $parameters)) ? $parameters[$param] : $params[$param];
|
||||
|
||||
$value = isset($parameters[$param]) ? $parameters[$param] : $value;
|
||||
/* If parameter is specifically set to null - use the original-defined value */
|
||||
if ($value === null && isset($this->originalParameters[$param])) {
|
||||
$value = $this->originalParameters[$param];
|
||||
}
|
||||
|
||||
if (stripos($url, $param1) !== false || stripos($url, $param) !== false) {
|
||||
/* Add parameter to the correct position */
|
||||
$url = str_ireplace([sprintf($param1, $param), sprintf($param2, $param)], $value, $url);
|
||||
} else {
|
||||
$unknownParams[$param] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/** @noinspection AliasFunctionsUsageInspection */
|
||||
$url .= join('/', $unknownParams);
|
||||
|
||||
|
||||
|
||||
return rtrim($url, '/') . '/';
|
||||
}
|
||||
|
||||
|
||||
@@ -95,11 +95,7 @@ abstract class Route implements IRoute
|
||||
|
||||
if (preg_match_all('/' . $regex . '/is', $route, $parameters)) {
|
||||
|
||||
$parameterNamesRegex = [];
|
||||
$parameterNames = $parameters[1];
|
||||
$parameterRequired = [];
|
||||
|
||||
$urlParts = preg_split('/\{[^}]+\}/is', rtrim($route, '/'));
|
||||
$urlParts = preg_split('/((\-?\/?)\{[^}]+\})/is', rtrim($route, '/'));
|
||||
|
||||
foreach ($urlParts as $key => $t) {
|
||||
|
||||
@@ -109,7 +105,7 @@ abstract class Route implements IRoute
|
||||
|
||||
$name = $parameters[1][$key];
|
||||
$regex = isset($this->where[$name]) ? $this->where[$name] : $parameterRegex;
|
||||
$regex = sprintf('(?P<%s>%s)', $name, $regex) . $parameters[2][$key];
|
||||
$regex = sprintf('\-?\/?(?P<%s>%s)', $name, $regex) . $parameters[2][$key];
|
||||
|
||||
}
|
||||
|
||||
@@ -128,7 +124,7 @@ abstract class Route implements IRoute
|
||||
|
||||
/* Only take matched parameters with name */
|
||||
foreach ($parameters[1] as $name) {
|
||||
$values[$name] = $matches[$name];
|
||||
$values[$name] = isset($matches[$name]) ? $matches[$name] : null;
|
||||
}
|
||||
|
||||
return $values;
|
||||
@@ -406,7 +402,7 @@ abstract class Route implements IRoute
|
||||
*/
|
||||
public function where(array $options)
|
||||
{
|
||||
return $this->where($options);
|
||||
return $this->setWhere($options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -442,7 +438,7 @@ abstract class Route implements IRoute
|
||||
$this->originalParameters = $parameters;
|
||||
}
|
||||
|
||||
$this->parameters = $parameters;
|
||||
$this->parameters = array_merge($this->parameters, $parameters);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -99,4 +99,16 @@ class RouterUrlTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
}
|
||||
|
||||
public function testRegEx()
|
||||
{
|
||||
SimpleRouter::router()->reset();
|
||||
SimpleRouter::request()->setMethod('get');
|
||||
SimpleRouter::request()->setUri('/my/custom-path');
|
||||
|
||||
SimpleRouter::get('/my/{path}', 'DummyController@start')->where(['path' => '[a-zA-Z\-]+']);
|
||||
|
||||
SimpleRouter::start();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user