From 7ee42c98a7642b520bfa39abb3db3f6d7d784c0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 7 Nov 2016 04:45:18 +0100 Subject: [PATCH 1/5] Updated documentaiton --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5c6e043..991b518 100644 --- a/README.md +++ b/README.md @@ -204,8 +204,8 @@ class Router extends SimpleRouter { } ``` -#### Helper functions examples -**This is a basic example of a helper function for generating urls.** +### Helper functions +**This is a basic example of a helper functions.** ```php use Pecee\SimpleRouter\SimpleRouter; From 28c3370b676f429904b7261ace56f81c9e418b08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 7 Nov 2016 04:55:43 +0100 Subject: [PATCH 2/5] Fixed group not adding multiple middlewares --- src/Pecee/SimpleRouter/RouterGroup.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Pecee/SimpleRouter/RouterGroup.php b/src/Pecee/SimpleRouter/RouterGroup.php index 35683b7..11863b0 100644 --- a/src/Pecee/SimpleRouter/RouterGroup.php +++ b/src/Pecee/SimpleRouter/RouterGroup.php @@ -97,9 +97,12 @@ class RouterGroup extends RouterEntry { if($this->getMiddleware() !== null && isset($settings['middleware'])) { if(!is_array($this->getMiddleware())) { - $middlewares = [$this->getMiddleware(), $settings['middleware']]; + $middlewares = [ + $this->getMiddleware(), + $settings['middleware'] + ]; } else { - $middlewares = array_push($settings['middleware']); + $middlewares = array_push($settings['middleware'], $this->getMiddleware()); } $settings['middleware'] = array_unique(array_reverse($middlewares)); From 48317ded7ab9128163e74412c1b8bfcb80dcb018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 7 Nov 2016 05:40:32 +0100 Subject: [PATCH 3/5] Updated documentation --- README.md | 83 +++++++++++++++++++++++-------------------------------- 1 file changed, 34 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 991b518..5fc4cef 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ The goal of this project is to create a router that is 100% compatible with the ### Features -- Basic routing (get, post, put, delete) with support for custom multiple verbs. +- Basic routing (`GET`, `POST`, `PUT`, `DELETE`) with support for custom multiple verbs. - Regular Expression Constraints for parameters. - Named routes. - Generating url to routes. @@ -204,42 +204,6 @@ class Router extends SimpleRouter { } ``` -### Helper functions -**This is a basic example of a helper functions.** - -```php -use Pecee\SimpleRouter\SimpleRouter; - -function url($controller, $parameters = null, $getParams = null) { - SimpleRouter::getRoute($controller, $parameters, $getParams); -} - -/** - * Get current csrf-token - * @return null|string - */ -function csrf_token() { - $token = new \Pecee\CsrfToken(); - return $token->getToken(); -} - -/** - * Get request object - * @return \Pecee\Http\Request - */ -function request() { - return SimpleRouter::request(); -} - -/** - * Get response object - * @return \Pecee\Http\Response - */ -function response() { - return SimpleRouter::response(); -} -``` - ## Getting urls **In ```routes.php``` we have added this route:** @@ -327,16 +291,6 @@ By doing this the route will now load the url ```/article/view/1``` instead of ` The last thing we need to do, is to add our custom boot-manager to the ```routes.php``` file. You can create as many bootmanagers as you like and easily add them in your ```routes.php``` file. -**routes.php example:** - -```php -// Add new bootmanager -SimpleRouter::addBootManager(new CustomRouterRules()); - -// This rule is what our custom bootmanager will use. -SimpleRouter::get('/article/view/{id}', 'ControllerArticle@view'); -``` - ## Easily overwrite route about to be loaded Sometimes it can be useful to manipulate the route that's about to be loaded, for instance if a user is not authenticated or if an error occurred within your Middleware that requires some other route to be initialised. Simple PHP Router allows you to easily change the route about to be executed. All information about the current route is stored in @@ -404,12 +358,43 @@ All object inherits from `InputItem` class and will always contain these methods - `getError()` - get file upload error. -### Easy access your input +### Easy access to methods Create a helper function to easily get access to the input elements. -Example: +**This is a basic example of a helper functions.** ```php +use Pecee\SimpleRouter\SimpleRouter; + +function url($controller, $parameters = null, $getParams = null) { + SimpleRouter::getRoute($controller, $parameters, $getParams); +} + +/** + * Get current csrf-token + * @return null|string + */ +function csrf_token() { + $token = new \Pecee\CsrfToken(); + return $token->getToken(); +} + +/** + * Get request object + * @return \Pecee\Http\Request + */ +function request() { + return SimpleRouter::request(); +} + +/** + * Get response object + * @return \Pecee\Http\Response + */ +function response() { + return SimpleRouter::response(); +} + /** * Get input class * @return \Pecee\Http\Input\Input From 540ebb31acca879fbdb151bd260262fc67e98a04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 7 Nov 2016 05:48:02 +0100 Subject: [PATCH 4/5] Updated documentation --- README.md | 91 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 46 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 5fc4cef..6250a0a 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,51 @@ class Router extends SimpleRouter { } ``` +## Helper functions + +To simplify to use of simple-router functionality, we recommend you add these helper functions to your project. + +```php +use Pecee\SimpleRouter\SimpleRouter; + +function url($controller, $parameters = null, $getParams = null) { + SimpleRouter::getRoute($controller, $parameters, $getParams); +} + +/** + * Get current csrf-token + * @return null|string + */ +function csrf_token() { + $token = new \Pecee\CsrfToken(); + return $token->getToken(); +} + +/** + * Get request object + * @return \Pecee\Http\Request + */ +function request() { + return SimpleRouter::request(); +} + +/** + * Get response object + * @return \Pecee\Http\Response + */ +function response() { + return SimpleRouter::response(); +} + +/** + * Get input class + * @return \Pecee\Http\Input\Input + */ +function input() { + return SimpleRouter::request()->getInput(); +} +``` + ## Getting urls **In ```routes.php``` we have added this route:** @@ -359,52 +404,8 @@ All object inherits from `InputItem` class and will always contain these methods ### Easy access to methods -Create a helper function to easily get access to the input elements. -**This is a basic example of a helper functions.** - -```php -use Pecee\SimpleRouter\SimpleRouter; - -function url($controller, $parameters = null, $getParams = null) { - SimpleRouter::getRoute($controller, $parameters, $getParams); -} - -/** - * Get current csrf-token - * @return null|string - */ -function csrf_token() { - $token = new \Pecee\CsrfToken(); - return $token->getToken(); -} - -/** - * Get request object - * @return \Pecee\Http\Request - */ -function request() { - return SimpleRouter::request(); -} - -/** - * Get response object - * @return \Pecee\Http\Response - */ -function response() { - return SimpleRouter::response(); -} - -/** - * Get input class - * @return \Pecee\Http\Input\Input - */ -function input() { - return SimpleRouter::request()->getInput(); -} -``` - -Then you can easily do something like this in your controller: +Below example requires you to have the helper functions added. Please refer to the helper functions section in the documentation. ```php // Get parameter site_id or default-value 2 From 28ffa30d3ea4a32fe0450c8109abbb90f0cdbff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 8 Nov 2016 18:21:21 +0200 Subject: [PATCH 5/5] Development - all() in Input class now returns correct array. - all() now supports json data. - Minor bugfixes. --- src/Pecee/Http/Input/Input.php | 52 +++++++++++++++++++++------------- src/Pecee/Http/Request.php | 13 +++++++++ 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/Pecee/Http/Input/Input.php b/src/Pecee/Http/Input/Input.php index 4d4fc8d..4820588 100644 --- a/src/Pecee/Http/Input/Input.php +++ b/src/Pecee/Http/Input/Input.php @@ -38,17 +38,31 @@ class Input { * @return array */ public function all(array $filter = null) { - $output = $this->get->getData(); - $output = array_merge($output, $this->post->getData()); - if($filter !== null) { - $tmp = array(); - foreach($output as $key => $val) { - if(in_array($key, $filter)) { - $tmp[$key] = $val; + $output = $_POST; + + if($this->request->getMethod() === 'post') { + + $contents = file_get_contents('php://input'); + + if (stripos(trim($contents), '{') === 0) { + $output = json_decode($contents, true); + if($output === false) { + $output = array(); } } - return $tmp; + } + + $output = array_merge($_GET, $output); + + if($filter !== null) { + $output = array_filter($output, function ($key) use ($filter) { + if (in_array($key, $filter)) { + return true; + } + + return false; + }, ARRAY_FILTER_USE_KEY); } return $output; @@ -93,7 +107,7 @@ class Input { if($item !== null) { - if(is_array($item) || $item instanceof InputFile) { + if($item instanceof InputCollection || $item instanceof InputFile) { return $item; } @@ -117,10 +131,10 @@ class Input { continue; } - $output = array(); + $output = new InputCollection(); foreach($get as $k => $g) { - $output[$k] = new InputItem($k, $g); + $output->{$k} = new InputItem($k, $g); } $this->get->{$key} = $output; @@ -131,12 +145,10 @@ class Input { public function setPost() { $this->post = new InputCollection(); - $postVars = array(); + $postVars = $_POST; - if(isset($_SERVER['REQUEST_METHOD']) && in_array($_SERVER['REQUEST_METHOD'], ['PUT', 'PATCH', 'DELETE'])) { + if(in_array($this->request->getMethod(), ['put', 'patch', 'delete'])) { parse_str(file_get_contents('php://input'), $postVars); - } else { - $postVars = $_POST; } if(count($postVars)) { @@ -147,10 +159,10 @@ class Input { continue; } - $output = array(); + $output = new InputCollection(); - foreach($post as $k=>$p) { - $output[$k] = new InputItem($k, $p); + foreach($post as $k => $p) { + $output->{$k} = new InputItem($k, $p); } $this->post->{strtolower($key)} = $output; @@ -178,7 +190,7 @@ class Input { continue; } - $output = array(); + $output = new InputCollection(); foreach($value['name'] as $k=>$val) { // Strip empty values @@ -189,7 +201,7 @@ class Input { $file->setType($value['type'][$k]); $file->setTmpName($value['tmp_name'][$k]); $file->setError($value['error'][$k]); - $output[$k] = $file; + $output->{$k} = $file; } } diff --git a/src/Pecee/Http/Request.php b/src/Pecee/Http/Request.php index c6cb59b..f077f49 100644 --- a/src/Pecee/Http/Request.php +++ b/src/Pecee/Http/Request.php @@ -5,8 +5,21 @@ use Pecee\Http\Input\Input; class Request { + protected static $instance; + protected $data; + /** + * 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 = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : array();