This repository was archived by the owner on Nov 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouter.php
More file actions
39 lines (33 loc) · 1.37 KB
/
Router.php
File metadata and controls
39 lines (33 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php namespace Brill;
class Router extends \Slim\Router
{
/**
* Get URL for named route
* @param string $name The name of the route
* @param array $params Associative array of URL parameter names and replacement values. Unmatched parameters will be used to build the query string.
* @return string The URL for the given route populated with provided replacement values
* @throws \RuntimeException If named route not found
* @api
*/
public function urlFor($name, $params = array())
{
if (!$this->hasNamedRoute($name)) {
throw new \RuntimeException('Named route not found for name: ' . $name);
}
$url = $this->getNamedRoute($name)->getPattern();
foreach ($params as $key => $value) {
$search = '#:' . preg_quote($key, '#') . '\+?(?!\w)#';
if (preg_match($search, $url)) {
$url = preg_replace($search, $value, $url);
unset($params[$key]);
}
}
//Remove remnants of unpopulated, trailing optional pattern segments, escaped special characters
$url = preg_replace('#\(/?:.+\)|\(|\)|\\\\#', '', $url);
// Leftovers are added as url query string
if ($params) {
$url .= '?' . http_build_query($params);
}
return $url;
}
}