Merge pull request #124 from skipperbent/v2-development

V2 development
This commit is contained in:
Simon Sessingø
2016-11-08 18:22:49 +02:00
committed by GitHub
4 changed files with 64 additions and 50 deletions
+14 -28
View File
@@ -18,7 +18,7 @@ The goal of this project is to create a router that is 100% compatible with the
### Features ### 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. - Regular Expression Constraints for parameters.
- Named routes. - Named routes.
- Generating url to routes. - Generating url to routes.
@@ -204,8 +204,9 @@ class Router extends SimpleRouter {
} }
``` ```
#### Helper functions examples ## Helper functions
**This is a basic example of a helper function for generating urls.**
To simplify to use of simple-router functionality, we recommend you add these helper functions to your project.
```php ```php
use Pecee\SimpleRouter\SimpleRouter; use Pecee\SimpleRouter\SimpleRouter;
@@ -238,6 +239,14 @@ function request() {
function response() { function response() {
return SimpleRouter::response(); return SimpleRouter::response();
} }
/**
* Get input class
* @return \Pecee\Http\Input\Input
*/
function input() {
return SimpleRouter::request()->getInput();
}
``` ```
## Getting urls ## Getting urls
@@ -327,16 +336,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. 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 ## 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 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 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,22 +403,9 @@ All object inherits from `InputItem` class and will always contain these methods
- `getError()` - get file upload error. - `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: Below example requires you to have the helper functions added. Please refer to the helper functions section in the documentation.
```php
/**
* 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:
```php ```php
// Get parameter site_id or default-value 2 // Get parameter site_id or default-value 2
+32 -20
View File
@@ -38,17 +38,31 @@ class Input {
* @return array * @return array
*/ */
public function all(array $filter = null) { public function all(array $filter = null) {
$output = $this->get->getData();
$output = array_merge($output, $this->post->getData());
if($filter !== null) { $output = $_POST;
$tmp = array();
foreach($output as $key => $val) { if($this->request->getMethod() === 'post') {
if(in_array($key, $filter)) {
$tmp[$key] = $val; $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; return $output;
@@ -93,7 +107,7 @@ class Input {
if($item !== null) { if($item !== null) {
if(is_array($item) || $item instanceof InputFile) { if($item instanceof InputCollection || $item instanceof InputFile) {
return $item; return $item;
} }
@@ -117,10 +131,10 @@ class Input {
continue; continue;
} }
$output = array(); $output = new InputCollection();
foreach($get as $k => $g) { foreach($get as $k => $g) {
$output[$k] = new InputItem($k, $g); $output->{$k} = new InputItem($k, $g);
} }
$this->get->{$key} = $output; $this->get->{$key} = $output;
@@ -131,12 +145,10 @@ class Input {
public function setPost() { public function setPost() {
$this->post = new InputCollection(); $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); parse_str(file_get_contents('php://input'), $postVars);
} else {
$postVars = $_POST;
} }
if(count($postVars)) { if(count($postVars)) {
@@ -147,10 +159,10 @@ class Input {
continue; continue;
} }
$output = array(); $output = new InputCollection();
foreach($post as $k=>$p) { foreach($post as $k => $p) {
$output[$k] = new InputItem($k, $p); $output->{$k} = new InputItem($k, $p);
} }
$this->post->{strtolower($key)} = $output; $this->post->{strtolower($key)} = $output;
@@ -178,7 +190,7 @@ class Input {
continue; continue;
} }
$output = array(); $output = new InputCollection();
foreach($value['name'] as $k=>$val) { foreach($value['name'] as $k=>$val) {
// Strip empty values // Strip empty values
@@ -189,7 +201,7 @@ class Input {
$file->setType($value['type'][$k]); $file->setType($value['type'][$k]);
$file->setTmpName($value['tmp_name'][$k]); $file->setTmpName($value['tmp_name'][$k]);
$file->setError($value['error'][$k]); $file->setError($value['error'][$k]);
$output[$k] = $file; $output->{$k} = $file;
} }
} }
+13
View File
@@ -5,8 +5,21 @@ use Pecee\Http\Input\Input;
class Request { class Request {
protected static $instance;
protected $data; 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() { public function __construct() {
$this->data = array(); $this->data = array();
$this->host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : array(); $this->host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : array();
+5 -2
View File
@@ -97,9 +97,12 @@ class RouterGroup extends RouterEntry {
if($this->getMiddleware() !== null && isset($settings['middleware'])) { if($this->getMiddleware() !== null && isset($settings['middleware'])) {
if(!is_array($this->getMiddleware())) { if(!is_array($this->getMiddleware())) {
$middlewares = [$this->getMiddleware(), $settings['middleware']]; $middlewares = [
$this->getMiddleware(),
$settings['middleware']
];
} else { } else {
$middlewares = array_push($settings['middleware']); $middlewares = array_push($settings['middleware'], $this->getMiddleware());
} }
$settings['middleware'] = array_unique(array_reverse($middlewares)); $settings['middleware'] = array_unique(array_reverse($middlewares));