Compare commits

..

17 Commits

Author SHA1 Message Date
Simon Sessingø 66ecf0ee33 Merge pull request #293 from skipperbent/v3-development
Readded csrf_token helper function - don't run away this time.
2017-09-25 08:51:02 +02:00
Simon Sessingø 4ba15033d9 Readded csrf_token helper function - don't run away this time. 2017-09-09 01:40:44 +01:00
Simon Sessingø cfa18e520a Merge pull request #290 from skipperbent/v3-development
Version 3.4.5
2017-09-03 19:43:09 +02:00
Simon Sessingø 5e448f0835 Merge pull request #289 from skipperbent/v3-group-parameters
V3 partial group support
2017-09-03 19:37:51 +02:00
Simon Sessingø 98ad310404 Updated documentation. 2017-09-03 18:36:41 +01:00
Simon Sessingø a5aac57ce9 Added RoutePartialGroup support. 2017-09-03 18:24:51 +01:00
Simon Sessingø 7f924c7d0a Added RouterGroupTest unit-tests. 2017-09-03 16:49:15 +01:00
Simon Sessingø 3a90578351 Added parameter support for Group routes. 2017-09-03 16:37:20 +01:00
Simon Sessingø 52e0f5ef94 Removed link from documentation that has been moved. 2017-09-01 18:01:56 +01:00
Simon Sessingø 0090c167bb Merge pull request #287 from skipperbent/v3-development
Response->json now accepts either array or \JsonSerializable (issue: #284)
2017-08-31 13:05:14 +02:00
Simon Sessingø ae68598024 Response->json now accepts either array or \JsonSerializable (issue: 2017-08-31 12:03:17 +01:00
Simon Sessingø 7362b748af Merge pull request #282 from skipperbent/v3-development
V3 development
2017-08-24 16:53:20 +02:00
Simon Sessingø 4fe85d6568 Merge pull request #281 from skipperbent/v3-fix-urldecode
Urldecode is now used on all urls in Uri class (issue: #268)
2017-08-24 16:53:04 +02:00
Simon Sessingø c82d91375c Urldecode is now used on all urls in Uri class (issue: #268) 2017-08-24 15:50:13 +01:00
Simon Sessingø e774122b1e Merge pull request #279 from skipperbent/v3-development
V3 development
2017-08-24 03:12:57 +02:00
Simon Sessingø e8aceb291c Merge pull request #278 from skipperbent/v3-fix-iis-encoding
Ensured that Request class is using unencoded-url header to avoid encoding issues on IIS (issue: #268)
2017-08-24 03:12:48 +02:00
Simon Sessingø 957a382248 Ensured that Request class is using unencoded-url header to avoid encoding issues on IIS (issue: #268) 2017-08-24 02:05:18 +01:00
13 changed files with 219 additions and 48 deletions
+46 -9
View File
@@ -43,6 +43,7 @@ If you want a great new feature or experience any issues what-so-ever, please fe
- [Namespaces](#namespaces)
- [Subdomain-routing](#subdomain-routing)
- [Route prefixes](#route-prefixes)
- [Partial groups](#partial-groups)
- [Form Method Spoofing](#form-method-spoofing)
- [Accessing The Current Route](#accessing-the-current-route)
- [Other examples](#other-examples)
@@ -79,7 +80,6 @@ If you want a great new feature or experience any issues what-so-ever, please fe
- [Bootmanager: loading routes dynamically](#bootmanager-loading-routes-dynamically)
- [Adding routes manually](#adding-routes-manually)
- [Parameters](#parameters)
- [Custom default regex for matching parameters](#custom-default-regex-for-matching-parameters)
- [Extending](#extending)
- [Credits](#credits)
@@ -249,7 +249,8 @@ We recommend that you add these helper functions to your project. These will all
To implement the functions below, simply copy the code to a new file and require the file before initializing the router or copy the `helpers.php` we've included in this library.
```php
<?php
use Pecee\SimpleRouter\SimpleRouter as Router;
/**
* Get url for a route by using either name/alias, class or method name.
*
@@ -269,7 +270,7 @@ To implement the functions below, simply copy the code to a new file and require
*/
function url($name = null, $parameters = null, $getParams = null)
{
return SimpleRouter::getUrl($name, $parameters, $getParams);
return Router::getUrl($name, $parameters, $getParams);
}
/**
@@ -277,7 +278,7 @@ function url($name = null, $parameters = null, $getParams = null)
*/
function response()
{
return SimpleRouter::response();
return Router::response();
}
/**
@@ -285,7 +286,7 @@ function response()
*/
function request()
{
return SimpleRouter::request();
return Router::request();
}
/**
@@ -297,8 +298,7 @@ function request()
*/
function input($index = null, $defaultValue = null, $methods = null)
{
if($index !== null)
{
if ($index !== null) {
return request()->getInput()->get($index, $defaultValue, $methods);
}
@@ -313,6 +313,20 @@ function redirect($url, $code = null)
response()->redirect($url);
}
/**
* Get current csrf-token
* @return string|null
*/
function csrf_token()
{
$baseVerifier = Router::router()->getCsrfVerifier();
if ($baseVerifier !== null) {
return $baseVerifier->getToken();
}
return null;
}
```
---
@@ -358,7 +372,7 @@ SimpleRouter::any('foo', function() {
});
```
We've created a simple method which matches `GET` and `POST` which is most commenly used:
We've created a simple method which matches `GET` and `POST` which is most commonly used:
```php
SimpleRouter::form('foo', function() {
@@ -542,7 +556,7 @@ SimpleRouter::group(['namespace' => 'Admin'], function () {
});
```
### Subdomain-routing
### Sub domain-routing
Route groups may also be used to handle sub-domain routing. Sub-domains may be assigned route parameters just like route URIs, allowing you to capture a portion of the sub-domain for usage in your route or controller. The sub-domain may be specified using the `domain` key on the group attribute array:
@@ -566,6 +580,29 @@ SimpleRouter::group(['prefix' => '/admin'], function () {
});
```
## Partial groups
Partial router groups has the same benefits as a normal group, but supports parameters and are only rendered once the url has matched.
This can be extremely useful in situations, where you only want special routes to be added, when a certain criteria or logic has been met.
**NOTE:** Use partial groups with caution as routes added within are only rendered and available once the url of the partial-group has matched. This can cause `url()` not to find urls for the routes added within.
**Example:**
```php
SimpleRouter::partialGroup('/admin/{applicationId}', function ($applicationId) {
SimpleRouter::get('/', function($applicationId) {
// Matches The "/admin/applicationId" URL
});
});
```
## Form Method Spoofing
HTML forms do not support `PUT`, `PATCH` or `DELETE` actions. So, when defining `PUT`, `PATCH` or `DELETE` routes that are called from an HTML form, you will need to add a hidden `_method` field to the form. The value sent with the `_method` field will be used as the HTTP request method:
+14
View File
@@ -63,4 +63,18 @@ function redirect($url, $code = null)
}
response()->redirect($url);
}
/**
* Get current csrf-token
* @return string|null
*/
function csrf_token()
{
$baseVerifier = Router::router()->getCsrfVerifier();
if ($baseVerifier !== null) {
return $baseVerifier->getToken();
}
return null;
}
+5 -2
View File
@@ -30,7 +30,10 @@ class Request
{
$this->parseHeaders();
$this->setHost($this->getHeader('http-host'));
$this->setUri(new Uri($this->getHeader('request-uri')));
// Check if special IIS header exist, otherwise use default.
$this->setUri(new Uri($this->getHeader('unencoded-url', $this->getHeader('request-uri'))));
$this->input = new Input($this);
$this->method = strtolower($this->input->get('_method', $this->getHeader('request-method'), 'post'));
}
@@ -164,7 +167,7 @@ class Request
*/
public function getHeader($name, $defaultValue = null)
{
if (isset($this->headers[strtolower($name)])) {
if (array_key_exists(strtolower($name), $this->headers) === true) {
return $this->headers[strtolower($name)];
}
+11 -5
View File
@@ -1,4 +1,5 @@
<?php
namespace Pecee\Http;
class Response
@@ -82,14 +83,19 @@ class Response
}
/**
* Json encode array
* @param array $value
* Json encode
* @param array|\JsonSerializable $value
* @throws \InvalidArgumentException;
*/
public function json(array $value)
public function json($value)
{
$this->header('Content-Type: application/json');
if (($value instanceof \JsonSerializable) === false && is_array($value) === false) {
throw new \InvalidArgumentException('Invalid type for parameter "value". Must be of type array or object implementing the \JsonSerializable interface.');
}
$this->header('Content-type: application/json');
echo json_encode($value);
die();
exit(0);
}
/**
+1 -1
View File
@@ -19,7 +19,7 @@ class Uri
public function __construct($url)
{
$this->originalUrl = $url;
$this->data = array_merge($this->data, $this->parseUrl($url));
$this->data = array_merge($this->data, $this->parseUrl(urldecode($url)));
if (isset($this->data['path']) === true && $this->data['path'] !== '/') {
$this->data['path'] = rtrim($this->data['path'], '/') . '/';
@@ -0,0 +1,8 @@
<?php
namespace Pecee\SimpleRouter\Route;
interface IPartialGroupRoute
{
}
+1 -1
View File
@@ -21,7 +21,7 @@ interface IRoute
*
* @param Request $request
* @throws \Pecee\SimpleRouter\Exceptions\NotFoundHttpException
* @return void
* @return string
*/
public function renderRoute(Request $request);
+4 -3
View File
@@ -42,6 +42,7 @@ abstract class Route implements IRoute
protected $defaultParameterRegex;
protected $paramModifiers = '{}';
protected $paramOptionalSymbol = '?';
protected $urlRegex = '/^%s\/?$/u';
protected $group;
protected $parent;
protected $callback;
@@ -69,7 +70,7 @@ abstract class Route implements IRoute
$callback = $this->getCallback();
if ($callback === null) {
return;
return null;
}
/* Render callback function */
@@ -154,14 +155,14 @@ abstract class Route implements IRoute
$urlRegex = preg_quote($route, '/');
}
if (preg_match('/^' . $urlRegex . '\/?$/u', $url, $matches) > 0) {
if (preg_match(sprintf($this->urlRegex, $urlRegex), $url, $matches) > 0) {
$values = [];
if (isset($parameters[1]) === true) {
/* Only take matched parameters with name */
foreach ($parameters[1] as $name) {
foreach ((array)$parameters[1] as $name) {
$values[$name] = (isset($matches[$name]) && $matches[$name] !== '') ? $matches[$name] : null;
}
}
+2 -2
View File
@@ -150,11 +150,11 @@ class RouteGroup extends Route implements IGroupRoute
$this->setPrefix($values['prefix'] . $this->prefix);
}
if (isset($values['exceptionHandler'])) {
if ($merge === false && isset($values['exceptionHandler'])) {
$this->setExceptionHandlers((array)$values['exceptionHandler']);
}
if (isset($values['domain'])) {
if ($merge === false && isset($values['domain'])) {
$this->setDomains((array)$values['domain']);
}
@@ -0,0 +1,36 @@
<?php
namespace Pecee\SimpleRouter\Route;
use Pecee\Http\Request;
class RoutePartialGroup extends RouteGroup implements IPartialGroupRoute
{
protected $urlRegex = '/^%s\/?/u';
/**
* Method called to check if route matches
*
* @param string $url
* @param Request $request
* @return bool
*/
public function matchRoute($url, Request $request)
{
if ($this->prefix !== null) {
/* Parse parameters from current route */
$parameters = $this->parseParameters($this->prefix, $url);
/* If no custom regular expression or parameters was found on this route, we stop */
if ($parameters === null) {
return false;
}
/* Set the parameters */
$this->setParameters((array)$parameters);
}
return $this->matchDomain($request);
}
}
+34 -25
View File
@@ -10,6 +10,7 @@ use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
use Pecee\SimpleRouter\Route\IControllerRoute;
use Pecee\SimpleRouter\Route\IGroupRoute;
use Pecee\SimpleRouter\Route\ILoadableRoute;
use Pecee\SimpleRouter\Route\IPartialGroupRoute;
use Pecee\SimpleRouter\Route\IRoute;
class Router
@@ -129,15 +130,27 @@ class Router
$route = $routes[$i];
if ($parent !== null) {
/* Add the parent route */
$route->setParent($parent);
/* Add/merge parent settings with child */
$route->setSettings($parent->toArray(), true);
}
if ($group !== null) {
/* Add the parent group */
$route->setGroup($group);
}
/* @var $route IGroupRoute */
if ($route instanceof IGroupRoute) {
$group = $route;
$this->processingRoute = true;
$route->renderRoute($this->request);
$this->processingRoute = false;
if ($route->matchRoute($url, $this->request) === true) {
/* Add exception handlers */
@@ -146,23 +159,20 @@ class Router
$exceptionHandlers += $route->getExceptionHandlers();
}
/* Only render partial group if it matches */
if ($route instanceof IPartialGroupRoute) {
$this->processingRoute = true;
$route->renderRoute($this->request);
$this->processingRoute = false;
}
}
}
if ($group !== null) {
/* Add the parent group */
$route->setGroup($group);
}
if ($parent !== null) {
/* Add the parent route */
$route->setParent($parent);
/* Add/merge parent settings with child */
$route->setSettings($parent->toArray(), true);
if (($route instanceof IPartialGroupRoute) === false) {
$this->processingRoute = true;
$route->renderRoute($this->request);
$this->processingRoute = false;
}
}
if ($route instanceof ILoadableRoute) {
@@ -258,9 +268,8 @@ class Router
if ($rewriteUrl !== null && $rewriteUrl !== $url) {
unset($this->processedRoutes[$i]);
$this->processedRoutes = array_values($this->processedRoutes);
$this->routeRequest(true);
return;
return $this->routeRequest(true);
}
/* Render route */
@@ -268,8 +277,6 @@ class Router
$this->request->setLoadedRoute($route);
return $route->renderRoute($this->request);
break;
}
}
@@ -293,12 +300,15 @@ class Router
$this->handleException(new NotFoundHttpException($message, 404));
}
return null;
}
/**
* @param \Exception $e
* @throws HttpException
* @throws \Exception
* @return string
*/
protected function handleException(\Exception $e)
{
@@ -335,9 +345,8 @@ class Router
if ($rewriteUrl !== null && $rewriteUrl !== $url) {
unset($this->exceptionHandlers[$i]);
$this->exceptionHandlers = array_values($this->exceptionHandlers);
$this->routeRequest(true);
return;
return $this->routeRequest(true);
}
}
}
+28
View File
@@ -16,6 +16,7 @@ use Pecee\Http\Response;
use Pecee\SimpleRouter\Exceptions\HttpException;
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
use Pecee\SimpleRouter\Route\IRoute;
use Pecee\SimpleRouter\Route\RoutePartialGroup;
use Pecee\SimpleRouter\Route\RouteController;
use Pecee\SimpleRouter\Route\RouteGroup;
use Pecee\SimpleRouter\Route\RouteResource;
@@ -171,14 +172,41 @@ class SimpleRouter
*/
public static function group(array $settings = [], \Closure $callback)
{
if (is_callable($callback) === false) {
throw new \InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
}
$group = new RouteGroup();
$group->setCallback($callback);
$group->setSettings($settings);
static::router()->addRoute($group);
return $group;
}
/**
* Special group that has the same benefits as group but supports
* parameters and which are only rendered when the url matches.
*
* @param string $url
* @param array $settings
* @param \Closure $callback
* @throws \InvalidArgumentException
* @return RoutePartialGroup
*/
public static function partialGroup($url, \Closure $callback, array $settings = [])
{
if (is_callable($callback) === false) {
throw new \InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
}
$settings['prefix'] = $url;
$group = new RoutePartialGroup();
$group->setSettings($settings);
$group->setCallback($callback);
static::router()->addRoute($group);
return $group;
+29
View File
@@ -0,0 +1,29 @@
<?php
require_once 'Dummy/DummyMiddleware.php';
require_once 'Dummy/DummyController.php';
require_once 'Dummy/Handler/ExceptionHandler.php';
require_once 'Helpers/TestRouter.php';
class RouterPartialGroupTest extends PHPUnit_Framework_TestCase
{
public function testParameters()
{
$result1 = null;
$result2 = null;
TestRouter::partialGroup('{param1}/{param2}', function ($param1 = null, $param2 = null) use (&$result1, &$result2) {
$result1 = $param1;
$result2 = $param2;
TestRouter::get('/', 'DummyController@method1');
});
TestRouter::debug('/param1/param2', 'get');
$this->assertEquals('param1', $result1);
$this->assertEquals('param2', $result2);
}
}