Skip to content

Commit

Permalink
Added new patterns for Route params.
Browse files Browse the repository at this point in the history
  • Loading branch information
izniburak committed Jul 1, 2019
1 parent cc75420 commit f1e4015
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@ class Router
*/
protected $patterns = [
'{a}' => '([^/]+)',
'{d}' => '([0-9]+)',
'{i}' => '([0-9]+)',
'{s}' => '([a-zA-Z]+)',
'{w}' => '([a-zA-Z0-9_]+)',
'{u}' => '([a-zA-Z0-9_-]+)',
'{*}' => '(.*)'
'{d}' => '(\d+)',
'{i}' => '(\d+)',
'{s}' => '(\w+)',
'{u}' => '([\w\-_]+)',
'{*}' => '(.*)',
':id' => '(\d+)',
':number' => '(\d+)',
':any' => '([^/]+)',
':all' => '(.*)',
':string' => '(\w+)',
':slug' => '([\w\-_]+)',
];

/**
Expand Down Expand Up @@ -188,7 +193,7 @@ public function __call($method, $params)
$callback = $params[2];
}

if (strstr($route, '{')) {
if (strstr($route, ':') || strstr($route, '{')) {
$route1 = $route2 = '';
foreach (explode('/', $route) as $key => $value) {
if ($value != '') {
Expand Down Expand Up @@ -263,15 +268,15 @@ public function pattern($pattern, $attr = null)
{
if (is_array($pattern)) {
foreach ($pattern as $key => $value) {
if (! in_array('{' . $key . '}', array_keys($this->patterns))) {
$this->patterns['{' . $key . '}'] = '(' . $value . ')';
if (! in_array($key, array_keys($this->patterns))) {
$this->patterns[$key] = '(' . $value . ')';
} else {
return $this->exception($key . ' pattern cannot be changed.');
}
}
} else {
if (! in_array('{' . $pattern . '}', array_keys($this->patterns))) {
$this->patterns['{' . $pattern . '}'] = '(' . $attr . ')';
if (! in_array($pattern, array_keys($this->patterns))) {
$this->patterns[$pattern] = '(' . $attr . ')';
} else {
return $this->exception($pattern . ' pattern cannot be changed.');
}
Expand Down Expand Up @@ -327,7 +332,7 @@ public function run()
foreach ($this->routes as $data) {
$route = $data['route'];

if (strpos($route, '{') !== false) {
if (strstr($route, ':') !== false || strpos($route, '{') !== false) {
$route = str_replace($searches, $replaces, $route);
}

Expand Down

0 comments on commit f1e4015

Please sign in to comment.