Skip to content

Commit 7692a91

Browse files
committed
added regex routes
fixes #40
1 parent fead6f6 commit 7692a91

2 files changed

Lines changed: 52 additions & 16 deletions

File tree

src/Route.php

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ public function __construct($args) {
1515
$this->_controller = $args['controller'];
1616
$this->_caseSensitive = $args['caseSensitive'] ? true : false;
1717
$this->_view = $args['view'] ? true : false;
18-
$this->_route = preg_replace('/^\/?(.*?)\/?$/i','\\1',$args['route']);
18+
19+
if ($args['route']{0} == '/' && !@preg_match($args['route'], null)) {
20+
$this->_route = $args['route'];
21+
$this->_regex = true;
22+
} else {
23+
$this->_route = preg_replace('/^\/?(.*?)\/?$/i','\\1', $args['route']);
24+
}
25+
1926
$this->_tipsy = $args['tipsy'];
2027
$this->_method = $args['method'] == 'all' ? '*' : $args['method'];
2128

@@ -40,31 +47,40 @@ public function match($page) {
4047
}
4148
}
4249

43-
$pathParams = [];
44-
$paths = explode('/',$this->_route);
45-
4650
// index page
4751
if (($this->_route === '' || $this->_route == '/') && ($page === '' || $page == '/')) {
4852
return $this;
4953
}
5054

51-
foreach ($paths as $key => $path) {
52-
if (strpos($path,':') === 0) {
53-
$pathParams[$key] = substr($path,1);
54-
}
55-
}
55+
$pathParams = [];
5656

57-
$r = preg_replace('/:[a-z]+/i','.*',$this->_route);
58-
$r = preg_replace('/\//','\/',$r);
57+
if ($this->_regex) {
58+
if (preg_match($this->_route, $page, $matches)) {
59+
$this->_routeParams = $matches;
60+
return $this;
61+
}
62+
} else {
5963

60-
if (preg_match('/^'.$r.'$/'.($this->_caseSensitive ? '' : 'i'),$page)) {
61-
$paths = explode('/',$page);
64+
$paths = explode('/',$this->_route);
6265

63-
foreach ($pathParams as $key => $path) {
64-
$this->_routeParams->{$path} = $paths[$key];
66+
foreach ($paths as $key => $path) {
67+
if (strpos($path,':') === 0) {
68+
$pathParams[$key] = substr($path,1);
69+
}
6570
}
6671

67-
return $this;
72+
$r = preg_replace('/:[a-z]+/i','.*',$this->_route);
73+
$r = preg_replace('/\//','\/',$r);
74+
75+
if (preg_match('/^'.$r.'$/'.($this->_caseSensitive ? '' : 'i'),$page)) {
76+
$paths = explode('/',$page);
77+
78+
foreach ($pathParams as $key => $path) {
79+
$this->_routeParams->{$path} = $paths[$key];
80+
}
81+
82+
return $this;
83+
}
6884
}
6985
return false;
7086
}

tests/RouterTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,4 +594,24 @@ public function testRouterShorthandOtherwise() {
594594
$this->tip->start();
595595
$this->assertTrue($res);
596596
}
597+
598+
public function testRegex() {
599+
$_REQUEST['__url'] = 'router/reg/ex';
600+
$_SERVER['REQUEST_METHOD'] = 'GET';
601+
$this->tip->when('/^router\/.*\/ex$/',function() use (&$res) {
602+
$res = true;
603+
});
604+
$this->tip->start();
605+
$this->assertTrue($res);
606+
}
607+
608+
public function testRegexSuffix() {
609+
$_REQUEST['__url'] = 'assets/app.scss';
610+
$_SERVER['REQUEST_METHOD'] = 'GET';
611+
$this->tip->get('/\.scss$/',function() use (&$res) {
612+
$res = true;
613+
});
614+
$this->tip->start();
615+
$this->assertTrue($res);
616+
}
597617
}

0 commit comments

Comments
 (0)