From 846c9e65842e6c48e458bef68f704e8816cd9daa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Sun, 1 Nov 2015 07:36:13 +0100 Subject: [PATCH 1/3] [BUGFIX] Optimised getRoute for custom urls. --- src/Pecee/SimpleRouter/RouterBase.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Pecee/SimpleRouter/RouterBase.php b/src/Pecee/SimpleRouter/RouterBase.php index 900d1a4..a8a13a0 100644 --- a/src/Pecee/SimpleRouter/RouterBase.php +++ b/src/Pecee/SimpleRouter/RouterBase.php @@ -253,7 +253,7 @@ class RouterBase { throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null'); } - if($controller === null && $parameters === null && $this->loadedRoute !== null) { + if($controller === null && $parameters === null) { return $this->processUrl($this->loadedRoute, null, $getParams); } @@ -295,7 +295,7 @@ class RouterBase { $method = $tmp[1]; } - if($controller === $c && $route !== null) { + if($controller === $c) { return $this->processUrl($route, $method, $parameters, $getParams); } } @@ -307,7 +307,13 @@ class RouterBase { ArrayUtil::append($url, $parameters); } - return '/' . join('/', $url); + $url = '/' . trim(join('/', $url), '/') . '/'; + + if(is_array($getParams)) { + $url .= '?' . Url::arrayToParams($getParams); + } + + return $url; } public static function getInstance() { From aca7d3d503d09302612616ca557f8c85561d0291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Sun, 1 Nov 2015 07:44:13 +0100 Subject: [PATCH 2/3] [FEATURE] Added magic method getters and setters, and made request a singleton applied configuration can be availible from everywhere. --- src/Pecee/Http/Request.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Pecee/Http/Request.php b/src/Pecee/Http/Request.php index 67aa88e..e4a9b59 100644 --- a/src/Pecee/Http/Request.php +++ b/src/Pecee/Http/Request.php @@ -3,12 +3,27 @@ namespace Pecee\Http; class Request { + protected static $instance; + + protected $data; protected $uri; protected $host; protected $method; protected $headers; + /** + * Return new instance + * @return static + */ + public static function getInstance() { + if(self::$instance === null) { + self::$instance = new static(); + } + return self::$instance; + } + public function __construct() { + $this->data = array(); $this->host = $_SERVER['HTTP_HOST']; $this->uri = $_SERVER['REQUEST_URI']; $this->method = (isset($_POST['_method'])) ? strtolower($_POST['_method']) : strtolower($_SERVER['REQUEST_METHOD']); @@ -103,4 +118,12 @@ class Request { return (isset($_REQUEST[$name]) ? $_REQUEST[$name] : $defaultValue); } + public function __set($name, $value = null) { + $this->data[$name] = $value; + } + + public function __get($name) { + return isset($this->data[$name]) ? $this->data[$name] : null; + } + } \ No newline at end of file From 637b998f023b25fe54ff4e2ab73518a202229a0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Sun, 1 Nov 2015 07:48:29 +0100 Subject: [PATCH 3/3] [TASK] Made RouterBase use singleton HttpRequest class. --- src/Pecee/SimpleRouter/RouterBase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Pecee/SimpleRouter/RouterBase.php b/src/Pecee/SimpleRouter/RouterBase.php index a8a13a0..db28f8a 100644 --- a/src/Pecee/SimpleRouter/RouterBase.php +++ b/src/Pecee/SimpleRouter/RouterBase.php @@ -27,7 +27,7 @@ class RouterBase { $this->routes = array(); $this->backstack = array(); $this->controllerUrlMap = array(); - $this->request = new Request(); + $this->request = Request::getInstance(); $this->baseCsrfVerifier = new BaseCsrfVerifier(); }