Skip to content

Commit c60d7d8

Browse files
committed
Merge pull request #23 from skipperbent/development
[BUGFIX] Optimised getRoute for custom urls.
2 parents 0df4691 + 637b998 commit c60d7d8

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/Pecee/Http/Request.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,27 @@
33

44
class Request {
55

6+
protected static $instance;
7+
8+
protected $data;
69
protected $uri;
710
protected $host;
811
protected $method;
912
protected $headers;
1013

14+
/**
15+
* Return new instance
16+
* @return static
17+
*/
18+
public static function getInstance() {
19+
if(self::$instance === null) {
20+
self::$instance = new static();
21+
}
22+
return self::$instance;
23+
}
24+
1125
public function __construct() {
26+
$this->data = array();
1227
$this->host = $_SERVER['HTTP_HOST'];
1328
$this->uri = $_SERVER['REQUEST_URI'];
1429
$this->method = (isset($_POST['_method'])) ? strtolower($_POST['_method']) : strtolower($_SERVER['REQUEST_METHOD']);
@@ -103,4 +118,12 @@ public function getInput($name, $defaultValue) {
103118
return (isset($_REQUEST[$name]) ? $_REQUEST[$name] : $defaultValue);
104119
}
105120

121+
public function __set($name, $value = null) {
122+
$this->data[$name] = $value;
123+
}
124+
125+
public function __get($name) {
126+
return isset($this->data[$name]) ? $this->data[$name] : null;
127+
}
128+
106129
}

src/Pecee/SimpleRouter/RouterBase.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct() {
2727
$this->routes = array();
2828
$this->backstack = array();
2929
$this->controllerUrlMap = array();
30-
$this->request = new Request();
30+
$this->request = Request::getInstance();
3131
$this->baseCsrfVerifier = new BaseCsrfVerifier();
3232
}
3333

@@ -253,7 +253,7 @@ public function getRoute($controller = null, $parameters = null, $getParams = nu
253253
throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null');
254254
}
255255

256-
if($controller === null && $parameters === null && $this->loadedRoute !== null) {
256+
if($controller === null && $parameters === null) {
257257
return $this->processUrl($this->loadedRoute, null, $getParams);
258258
}
259259

@@ -295,7 +295,7 @@ public function getRoute($controller = null, $parameters = null, $getParams = nu
295295
$method = $tmp[1];
296296
}
297297

298-
if($controller === $c && $route !== null) {
298+
if($controller === $c) {
299299
return $this->processUrl($route, $method, $parameters, $getParams);
300300
}
301301
}
@@ -307,7 +307,13 @@ public function getRoute($controller = null, $parameters = null, $getParams = nu
307307
ArrayUtil::append($url, $parameters);
308308
}
309309

310-
return '/' . join('/', $url);
310+
$url = '/' . trim(join('/', $url), '/') . '/';
311+
312+
if(is_array($getParams)) {
313+
$url .= '?' . Url::arrayToParams($getParams);
314+
}
315+
316+
return $url;
311317
}
312318

313319
public static function getInstance() {

0 commit comments

Comments
 (0)