Skip to content

Commit f1e4015

Browse files
committed
Added new patterns for Route params.
1 parent cc75420 commit f1e4015

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/Router.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@ class Router
4040
*/
4141
protected $patterns = [
4242
'{a}' => '([^/]+)',
43-
'{d}' => '([0-9]+)',
44-
'{i}' => '([0-9]+)',
45-
'{s}' => '([a-zA-Z]+)',
46-
'{w}' => '([a-zA-Z0-9_]+)',
47-
'{u}' => '([a-zA-Z0-9_-]+)',
48-
'{*}' => '(.*)'
43+
'{d}' => '(\d+)',
44+
'{i}' => '(\d+)',
45+
'{s}' => '(\w+)',
46+
'{u}' => '([\w\-_]+)',
47+
'{*}' => '(.*)',
48+
':id' => '(\d+)',
49+
':number' => '(\d+)',
50+
':any' => '([^/]+)',
51+
':all' => '(.*)',
52+
':string' => '(\w+)',
53+
':slug' => '([\w\-_]+)',
4954
];
5055

5156
/**
@@ -188,7 +193,7 @@ public function __call($method, $params)
188193
$callback = $params[2];
189194
}
190195

191-
if (strstr($route, '{')) {
196+
if (strstr($route, ':') || strstr($route, '{')) {
192197
$route1 = $route2 = '';
193198
foreach (explode('/', $route) as $key => $value) {
194199
if ($value != '') {
@@ -263,15 +268,15 @@ public function pattern($pattern, $attr = null)
263268
{
264269
if (is_array($pattern)) {
265270
foreach ($pattern as $key => $value) {
266-
if (! in_array('{' . $key . '}', array_keys($this->patterns))) {
267-
$this->patterns['{' . $key . '}'] = '(' . $value . ')';
271+
if (! in_array($key, array_keys($this->patterns))) {
272+
$this->patterns[$key] = '(' . $value . ')';
268273
} else {
269274
return $this->exception($key . ' pattern cannot be changed.');
270275
}
271276
}
272277
} else {
273-
if (! in_array('{' . $pattern . '}', array_keys($this->patterns))) {
274-
$this->patterns['{' . $pattern . '}'] = '(' . $attr . ')';
278+
if (! in_array($pattern, array_keys($this->patterns))) {
279+
$this->patterns[$pattern] = '(' . $attr . ')';
275280
} else {
276281
return $this->exception($pattern . ' pattern cannot be changed.');
277282
}
@@ -327,7 +332,7 @@ public function run()
327332
foreach ($this->routes as $data) {
328333
$route = $data['route'];
329334

330-
if (strpos($route, '{') !== false) {
335+
if (strstr($route, ':') !== false || strpos($route, '{') !== false) {
331336
$route = str_replace($searches, $replaces, $route);
332337
}
333338

0 commit comments

Comments
 (0)