From f1e401537f5c2ee7620bea224b5037d845670373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0zni=20Burak=20Demirta=C5=9F?= Date: Mon, 1 Jul 2019 22:21:54 +0300 Subject: [PATCH] Added new patterns for Route params. --- src/Router.php | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Router.php b/src/Router.php index ad5662e..6918bd5 100644 --- a/src/Router.php +++ b/src/Router.php @@ -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\-_]+)', ]; /** @@ -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 != '') { @@ -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.'); } @@ -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); }