mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 23:12:11 +00:00
Merge pull request #23 from skipperbent/development
[BUGFIX] Optimised getRoute for custom urls.
This commit is contained in:
@@ -3,12 +3,27 @@ namespace Pecee\Http;
|
|||||||
|
|
||||||
class Request {
|
class Request {
|
||||||
|
|
||||||
|
protected static $instance;
|
||||||
|
|
||||||
|
protected $data;
|
||||||
protected $uri;
|
protected $uri;
|
||||||
protected $host;
|
protected $host;
|
||||||
protected $method;
|
protected $method;
|
||||||
protected $headers;
|
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() {
|
public function __construct() {
|
||||||
|
$this->data = array();
|
||||||
$this->host = $_SERVER['HTTP_HOST'];
|
$this->host = $_SERVER['HTTP_HOST'];
|
||||||
$this->uri = $_SERVER['REQUEST_URI'];
|
$this->uri = $_SERVER['REQUEST_URI'];
|
||||||
$this->method = (isset($_POST['_method'])) ? strtolower($_POST['_method']) : strtolower($_SERVER['REQUEST_METHOD']);
|
$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);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -27,7 +27,7 @@ class RouterBase {
|
|||||||
$this->routes = array();
|
$this->routes = array();
|
||||||
$this->backstack = array();
|
$this->backstack = array();
|
||||||
$this->controllerUrlMap = array();
|
$this->controllerUrlMap = array();
|
||||||
$this->request = new Request();
|
$this->request = Request::getInstance();
|
||||||
$this->baseCsrfVerifier = new BaseCsrfVerifier();
|
$this->baseCsrfVerifier = new BaseCsrfVerifier();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,7 +253,7 @@ class RouterBase {
|
|||||||
throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null');
|
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);
|
return $this->processUrl($this->loadedRoute, null, $getParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,7 +295,7 @@ class RouterBase {
|
|||||||
$method = $tmp[1];
|
$method = $tmp[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
if($controller === $c && $route !== null) {
|
if($controller === $c) {
|
||||||
return $this->processUrl($route, $method, $parameters, $getParams);
|
return $this->processUrl($route, $method, $parameters, $getParams);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -307,7 +307,13 @@ class RouterBase {
|
|||||||
ArrayUtil::append($url, $parameters);
|
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() {
|
public static function getInstance() {
|
||||||
|
|||||||
Reference in New Issue
Block a user