Version 3

- Easier rewrite management.
- Optimisations.
- Updated documentation.
This commit is contained in:
Simon Sessingø
2017-02-09 13:18:05 +01:00
parent bf069c2d42
commit ce276bd5b7
19 changed files with 416 additions and 283 deletions
+240 -185
View File
@@ -15,71 +15,72 @@ If you want a great new feature or experience any issues what-so-ever, please fe
## Table of Contents
- Gettings started
- Requirements
- Notes
- Features
- Installation
- Setting up Apache
- Setting up Nginx
- Setting up simple-php-router
- Helper methods
- [Getting started](#getting-started)
- [Requirements](#requirements)
- [Notes](#notes-1)
- [Features](#features)
- [Installation](#installation)
- [Setting up Apache](#setting-up-apache)
- [Setting up Nginx](#setting-up-nginx)
- [Configuration](#configuration)
- [Helper functions](#helper-functions)
- Routes
- Basic routing
- Available methods
- Multiple HTTP-verbs
- Route parameters
- Required parameters
- Optional parameters
- Regular expression constraints
- Regular expression route-match
- Named routes
- Generating URLs To Named Routes
- Router groups
- Middleware
- Namespaces
- Subdomain-routing
- Route prefixes
- Form Method Spoofing
- Accessing The Current Route
- Other examples
- [Routes](#routes)
- [Basic routing](#basic-routing)
- [Available methods](#available-methods)
- [Multiple HTTP-verbs](#multiple-http-verbs)
- [Route parameters](#route-parameters)
- [Required parameters](#required-parameters)
- [Optional parameters](#optional-parameters)
- [Regular expression constraints](#regular-expression-constraints)
- [Regular expression route-match](#regular-expression-route-match)
- [Named routes](#named-routes)
- [Generating URLs To Named Routes](#generating-urls-to-named-routes)
- [Router groups](#router-groups)
- [Middleware](#middleware)
- [Namespaces](#namespaces)
- [Subdomain-routing](#subdomain-routing)
- [Route prefixes](#route-prefixes)
- [Form Method Spoofing](#form-method-spoofing)
- [Accessing The Current Route](#accessing-the-current-route)
- [Other examples](#other-examples)
- CSRF-protection
- Adding CSRF-verifier
- Getting CSRF-token
- [CSRF-protection](#csrf-protection)
- [Adding CSRF-verifier](#adding-csrf-verifier)
- [Getting CSRF-token](#getting-csrf-token)
- Middleware
- Example
- ExceptionHandler
- Example
- [Middlewares](#middlewares)
- [Example](#example)
- [ExceptionHandler](#exceptionhandler)
- [Example](#example-1)
- Urls
- Get by name (single route)
- Get by name (controller route)
- Get by class
- Get by custom names for methods on a controller/resource route
- Getting REST/resource controller urls
- [Urls](#urls)
- [Get by name (single route)](#get-by-name-single-route)
- [Get by name (controller route)](#get-by-name-controller-route)
- [Get by class](#get-by-class)
- [Get by custom names for methods on a controller/resource route](#using-custom-names-for-methods-on-a-controllerresource-route)
- [Getting REST/resource controller urls](#getting-restresource-controller-urls)
- [Get the current url](#get-the-current-url)
- Input & parameters
- Return single parameter value
- Return single parameter object
- Managing files
- Return all parameters
- [Input & parameters](#input--parameters)
- [Using the Input class to manage parameters](#using-the-input-class-to-manage-parameters)
- [Get single parameter value](#get-single-parameter-value)
- [Get parameter object](#get-parameter-object)
- [Managing files](#managing-files)
- [Get all parameters](#get-all-parameters)
- Advanced
- Bootmanager: loading routes dynamically
- Ovewrite route about to be loaded
- Examples
- Rewriting to new route
- Changing callback
- [Advanced](#advanced)
- [Bootmanager: loading routes dynamically](#bootmanager-loading-routes-dynamically)
- [Url rewriting](#url-rewriting)
- [Rewrite using callback](#rewrite-using-callback)
- [Rewrite using url](#rewrite-using-url)
- Adding routes manually
- Extending
- [Adding routes manually](#adding-routes-manually)
- [Extending](#extending)
- Credits
- Sites
- License
- [Credits](#credits)
- [Sites](#sites)
- [License](#license)
___
@@ -154,7 +155,7 @@ location / {
Nothing special is required for Apache to work. We've include the `.htaccess` file in the `public` folder. If rewriting is not working for you, please check that the `mod_rewrite` module (htaccess support) is enabled in the Apache configuration.
### Setting up simple-php-router
### Configuration
Create a new file, name it `routes.php` and place it in your library folder. This will be the file where you define all the routes for your project.
@@ -192,8 +193,6 @@ To implement the functions below, simply copy the code to a new file and require
```php
<?php
use Pecee\SimpleRouter\SimpleRouter;
/**
* Get url for a route by using either name/alias, class or method name.
*
@@ -211,41 +210,43 @@ use Pecee\SimpleRouter\SimpleRouter;
* @param array|null $getParams
* @return string
*/
function url($name = null, $parameters = null, $getParams = null) {
SimpleRouter::getUrl($name, $parameters, $getParams);
function url($name = null, $parameters = null, $getParams = null)
{
return SimpleRouter::getUrl($name, $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() {
function response()
{
return SimpleRouter::response();
}
/**
* @return \Pecee\Http\Request
*/
function request()
{
return SimpleRouter::request();
}
/**
* Get input class
* @return \Pecee\Http\Input\Input
*/
function input() {
return SimpleRouter::request()->getInput();
function input()
{
return request()->getInput();
}
function redirect($url, $code = null)
{
if ($code !== null) {
response()->httpCode($code);
}
response()->redirect($url);
}
```
@@ -479,7 +480,8 @@ HTML forms do not support `PUT`, `PATCH` or `DELETE` actions. So, when defining
You can access information about the current route loaded by using the following method:
```php
SimpleRouter::router()->getLoadedRoute();
SimpleRouter::request()->getLoadedRoute();
request()->getLoadedRoute();
```
## Other examples
@@ -584,13 +586,19 @@ namespace Demo\Middlewares;
use Pecee\Http\Middleware\IMiddleware;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Route\ILoadableRoute;
class CustomMiddleware implements Middleware {
public function handle(Request $request, ILoadableRoute &$route) {
public function handle(Request $request) {
$request->setUri(url('home'));
// Authenticate user, will be available using request()->user
$request->user = User::authenticate();
// If authentication failed, redirect request to user-login page.
if($request->user === null) {
$request->setRewriteUrl(url('user.login'));
return $request;
}
}
}
@@ -614,11 +622,10 @@ namespace Demo\Handlers;
use Pecee\Handlers\IExceptionHandler;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
use Pecee\SimpleRouter\Route\ILoadableRoute;
class CustomExceptionHandler implements IExceptionHandler
{
public function handleError(Request $request, ILoadableRoute &$route = null, \Exception $error)
public function handleError(Request $request, \Exception $error)
{
/* You can use the exception handler to format errors depending on the request and type. */
@@ -635,14 +642,9 @@ class CustomExceptionHandler implements IExceptionHandler
/* The router will throw the NotFoundHttpException on 404 */
if($error instanceof NotFoundHttpException) {
/*
* Render your own custom 404-view, rewrite the request to another route,
* or simply return the $request object to ignore the error and continue on rendering the route.
*
* The code below will make the router render our page.notfound route.
*/
// Render custom 404-page
$request->setUri(url('page.notfound'));
$request->setRewriteCallback('Demo\Controllers\PageController@notFound');
return $request;
}
@@ -660,7 +662,7 @@ class CustomExceptionHandler implements IExceptionHandler
By default all controller and resource routes will use a simplified version of their url as name.
### Get routes using custom name (single route)
### Get by name (single route)
```php
SimpleRouter::get('/product-view/{id}', 'ProductsController@show', ['as' => 'product']);
@@ -673,7 +675,7 @@ url('product', null, ['category' => 'shoes']);
# /product-view/?category=shoes
```
### Getting the url using the name (controller route)
### Get by name (controller route)
```php
SimpleRouter::controller('/images', 'ImagesController', ['as' => 'picture']);
@@ -688,7 +690,7 @@ url('picture', 'view');
# /images/view/
```
### Getting the url using class
### Get by class
```php
SimpleRouter::get('/product-view/{id}', 'ProductsController@show', ['as' => 'product']);
@@ -731,7 +733,7 @@ url('phones.edit');
# /phones/edit/
```
### Return the current url
### Get the current url
```php
url();
@@ -748,7 +750,7 @@ url(null, null, ['q' => 'cars']);
We've added the `Input` class to easy access and manage parameters from your Controller-classes.
**Return single parameter value (matches both GET, POST, FILE):**
### Get single parameter value:
If items is grouped in the html, it will return an array of items.
@@ -758,7 +760,7 @@ If items is grouped in the html, it will return an array of items.
$value = input()->get($index, $defaultValue, $methods);
```
**Return parameter object (matches both GET, POST, FILE):**
### Get parameter object
Will return an instance of `InputItem` or `InputFile` depending on the type.
@@ -774,7 +776,7 @@ If items is grouped in the html, it will return an array of items.
$object = input()->getObject($index, $defaultValue = null, $methods = null);
```
**Return specific GET parameter (where name is the name of your parameter):**
### Return specific GET parameter (where name is the name of your parameter):
```php
# -- match any (default) --
@@ -798,9 +800,11 @@ $object = input()->get($index, $defaultValue, 'file');
$object = input()->findGet($index, $defaultValue);
$object = input()->findPost($index, $defaultValue);
$object = input()->findFile($index, $defaultValue);
```
# -- examples --
### Managing files
```php
/**
* In this small example we loop through a collection of files
* added on the page like this
@@ -818,9 +822,10 @@ foreach(input()->get('images', []) as $image)
}
}
```
**Return all parameters:**
### Get all parameters
```php
// Get all
@@ -860,7 +865,132 @@ $siteId = input()->get('site_id', 2, ['post', 'get']);
# Advanced
## Load routes dynamicially using the router bootmanager
## Url rewriting
Sometimes it can be useful to manipulate the route about to be loaded.
simple-php-router allows you to easily change the route about to be executed.
All information about the current route is stored in the ```\Pecee\SimpleRouter\Router``` instance's `loadedRoute` property.
For easy access you can use the shortcut method `\Pecee\SimpleRouter\SimpleRouter::router()`.
```php
use Pecee\SimpleRouter;
$request = SimpleRouter::request();
$request->setRewriteCallback('Example\MyCustomClass@hello');
// -- or you can rewrite by url --
$request->setRewriteUrl('/my-rewrite-url');
```
### Examples
It's only possible to change the route BEFORE the route has initially been rendered. You can use the `Request` object to manipulate the route which are about to be loaded.
#### Rewrite using callback
This method is most efficient, as it will render the route immediately.
This method is useful for rendering 404-pages etc.
You can also change the callback by modifying the `$route` parameter. This is perfect if you just want to display a view quickly - or change the callback depending
on some criteria's for the request.
The callback below will fire immediately after the `Middleware` or `ExceptionHandler` has been loaded, as they are loaded before the route is rendered.
If you wish to change the callback from outside, please have this in mind.
The example below will render `DefaultController@notFound` regardless of the url.
**NOTE: Use this method if you want to load another controller. No additional middlewares or rules will be loaded.**
#### Middleware example
```php
namespace Demo\Middlewares;
use Pecee\Http\Middleware\IMiddleware;
use Pecee\Http\Request;
class CustomMiddleware implements IMiddleware {
public function handle(Request $request) {
$request->setRewriteCallback('Demo\Controllers\DefaultController@notFound');
return $request;
}
}
```
#### Exception handler example
```php
namespace Demo\Handlers;
use Pecee\Handlers\IExceptionHandler;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
class CustomExceptionHandler implements IExceptionHandler
{
public function handleError(Request $request, \Exception $error)
{
/* The router will throw the NotFoundHttpException on 404 */
if($error instanceof NotFoundHttpException) {
/*
* Render your own custom 404-view, rewrite the request to another route,
* or simply return the $request object to ignore the error and continue on rendering the route.
*
* The code below will make the router render our page.notfound route.
*/
$request->setRewriteCallback('Demo\Controllers\DefaultController@notFound');
return $request;
}
throw $error;
}
}
```
#### Rewrite using url
The example below will cause the router to reload the request and reinitialize all the routes. This method is slower, but will ensure that all middlewares and rules for the route is loaded.
This method is useful if you want to redirect a url to another-url which is dependent on a middleware. You can also add a custom rule by calling `$request->setRewriteRoute($route)` if
you want to customize request-methods or use another route-type like `RouteController` etc.
We are using the `url()` helper function to get the uri to another route added in the `routes.php` file.
**NOTE: Use this method if you want to fully load another route using it's settings (request method, middlewares etc).**
#### Middleware example
The example below will redirect the request to the `home`-route.
```php
namespace Demo\Middlewares;
use Pecee\Http\Middleware\IMiddleware;
use Pecee\Http\Request;
class CustomMiddleware implements IMiddleware {
public function handle(Request $request) {
$request->setRewriteUrl(url('home'));
return $request;
}
}
```
# Bootmanager: loading routes dynamically
Sometimes it can be necessary to keep urls stored in the database, file or similar. In this example, we want the url ```/my-cat-is-beatiful``` to load the route ```/article/view/1``` which the router knows, because it's defined in the ```routes.php``` file.
@@ -876,7 +1006,7 @@ class CustomRouterRules implement IRouterBootManager {
$rewriteRules = [
'/my-cat-is-beatiful' => '/article/view/1',
'/horses-are-great' => '/article/view/2'
'/horses-are-great' => '/article/view/2',
];
foreach($rewriteRules as $url => $rule) {
@@ -884,11 +1014,11 @@ class CustomRouterRules implement IRouterBootManager {
// If the current uri matches the url, we use our custom route
if($request->getUri() === $url) {
$request->setUri($rule);
$request->setRewriteUrl($rule);
return $request;
}
}
return $request;
}
}
@@ -906,82 +1036,6 @@ The last thing we need to do, is to add our custom boot-manager to the ```routes
SimpleRouter::addBootManager(new CustomRouterRules());
```
## Easily overwrite route about to be loaded
Sometimes it can be useful to manipulate the route about to be loaded.
simple-php-router allows you to easily change the route about to be executed.
All information about the current route is stored in the ```\Pecee\SimpleRouter\Router``` instance's `loadedRoute` property.
For easy access you can use the shortcut method `\Pecee\SimpleRouter\SimpleRouter::router()`.
```php
use Pecee\SimpleRouter;
$route = SimpleRouter::router()->getLoadedRoute();
$route->setCallback('Example\MyCustomClass@hello');
// -- or you can rewrite by doing --
$route->setClass('Example\MyCustomClass');
$route->setMethod('hello');
```
### Examples
It's only possible to change the route BEFORE the route has initially been loaded. If you want to redirect to another route, we highly recommend that you
modify the `IRoute` object from a `Middleware` or `ExceptionHandler`, like the examples below.
#### Rewriting to new route
The example below will cause the router to re-route the request with another url. We are using the `url()` helper function to get the uri to another route added in the `routes.php` file.
**NOTE: Use this method if you want to fully load another route using it's settings (request method etc).**
```php
namespace Demo\Middlewares;
use Pecee\Http\Middleware\IMiddleware;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Route\ILoadableRoute;
class CustomMiddleware implements Middleware {
public function handle(Request $request, ILoadableRoute &$route) {
$request->setUri(url('home'));
}
}
```
#### Changing callback
You can also change the callback by modifying the `$route` parameter. This is perfect if you just want to display a view quickly - or change the callback depending
on some criteria's for the request.
The callback below will fire immediately after the `Middleware` or `ExceptionHandler` has been loaded, as they are loaded before the route is rendered.
If you wish to change the callback from outside, please have this in mind.
**NOTE: Use this method if you want to load another controller. No additional middlewares or rules will be loaded.**
```php
namespace Demo\Middlewares;
use Pecee\Http\Middleware\IMiddleware;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Route\ILoadableRoute;
class CustomMiddleware implements Middleware {
public function handle(Request $request, ILoadableRoute &$route) {
$route->callback('DefaultController@home');
}
}
```
## Adding routes manually
The ```SimpleRouter``` class referenced in the previous example, is just a simple helper class that knows how to communicate with the ```Router``` class.
@@ -1042,6 +1096,7 @@ class Router extends SimpleRouter {
# Credits
## Sites
This is some sites that uses the simple-router project in production.
- [holla.dk](http://www.holla.dk)
+1 -3
View File
@@ -2,16 +2,14 @@
namespace Pecee\Handlers;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Route\ILoadableRoute;
interface IExceptionHandler
{
/**
* @param Request $request
* @param ILoadableRoute $route
* @param \Exception $error
* @return Request|null
*/
public function handleError(Request $request, ILoadableRoute &$route = null, \Exception $error);
public function handleError(Request $request, \Exception $error);
}
@@ -4,7 +4,6 @@ namespace Pecee\Http\Middleware;
use Pecee\CsrfToken;
use Pecee\Http\Middleware\Exceptions\TokenMismatchException;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Route\ILoadableRoute;
class BaseCsrfVerifier implements IMiddleware
{
@@ -55,7 +54,7 @@ class BaseCsrfVerifier implements IMiddleware
return false;
}
public function handle(Request $request, ILoadableRoute &$route = null)
public function handle(Request $request)
{
if ($this->skip($request) === false && in_array($request->getMethod(), ['post', 'put', 'delete'], false) === true) {
+1 -3
View File
@@ -2,15 +2,13 @@
namespace Pecee\Http\Middleware;
use Pecee\Http\Request;
use Pecee\SimpleRouter\Route\ILoadableRoute;
interface IMiddleware
{
/**
* @param Request $request
* @param ILoadableRoute $route
* @return Request|null
*/
public function handle(Request $request, ILoadableRoute &$route);
public function handle(Request $request);
}
+94
View File
@@ -2,6 +2,9 @@
namespace Pecee\Http;
use Pecee\Http\Input\Input;
use Pecee\SimpleRouter\Route\ILoadableRoute;
use Pecee\SimpleRouter\Route\IRoute;
use Pecee\SimpleRouter\Route\RouteUrl;
class Request
{
@@ -12,6 +15,17 @@ class Request
protected $method;
protected $input;
/**
* @var ILoadableRoute|null
*/
protected $rewriteRoute;
protected $rewriteUrl;
/**
* @var ILoadableRoute|null
*/
protected $loadedRoute;
public function __construct()
{
$this->parseHeaders();
@@ -213,6 +227,86 @@ class Request
$this->method = $method;
}
/**
* Set rewrite route
*
* @param ILoadableRoute $route
* @return static
*/
public function setRewriteRoute(ILoadableRoute $route)
{
$this->rewriteRoute = $route;
return $this;
}
/**
* Get rewrite route
*
* @return IRoute|null
*/
public function getRewriteRoute()
{
return $this->rewriteRoute;
}
/**
* Get rewrite url
*
* @return string
*/
public function getRewriteUrl()
{
return $this->rewriteUrl;
}
/**
* Set rewrite url
*
* @param string $rewriteUrl
* @return static
*/
public function setRewriteUrl($rewriteUrl)
{
$this->rewriteUrl = $rewriteUrl;
return $this;
}
/**
* Set rewrite callback
* @param string $callback
* @return static
*/
public function setRewriteCallback($callback)
{
$this->rewriteRoute = new RouteUrl($this->uri, $callback);
return $this;
}
/**
* Get loaded route
* @return ILoadableRoute|null
*/
public function getLoadedRoute()
{
return $this->loadedRoute;
}
/**
* Set loaded route
*
* @param ILoadableRoute $route
* @return static
*/
public function setLoadedRoute(ILoadableRoute $route)
{
$this->loadedRoute = $route;
return $this;
}
public function __isset($name)
{
return array_key_exists($name, $this->data);
@@ -3,4 +3,5 @@ namespace Pecee\SimpleRouter\Exceptions;
class NotFoundHttpException extends HttpException
{
}
@@ -20,9 +20,8 @@ interface ILoadableRoute extends IRoute
* Loads and renders middlewares-classes
*
* @param Request $request
* @param ILoadableRoute $route
*/
public function loadMiddleware(Request $request, ILoadableRoute $route);
public function loadMiddleware(Request $request);
public function getUrl();
+2 -1
View File
@@ -8,10 +8,11 @@ interface IRoute
/**
* Method called to check if a domain matches
*
* @param string $route
* @param Request $request
* @return bool
*/
public function matchRoute(Request $request);
public function matchRoute($route, Request $request);
/**
* Called when route is matched.
@@ -23,10 +23,9 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
* Loads and renders middlewares-classes
*
* @param Request $request
* @param ILoadableRoute $route
* @throws HttpException
*/
public function loadMiddleware(Request $request, ILoadableRoute $route)
public function loadMiddleware(Request $request)
{
if (count($this->getMiddlewares()) > 0) {
@@ -42,7 +41,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
throw new HttpException($middleware . ' must be instance of Middleware');
}
$middleware->handle($request, $route);
$middleware->handle($request);
}
}
}
@@ -83,9 +83,9 @@ class RouteController extends LoadableRoute implements IControllerRoute
return '/' . trim($url, '/') . '/';
}
public function matchRoute(Request $request)
public function matchRoute($url, Request $request)
{
$url = parse_url(urldecode($request->getUri()), PHP_URL_PATH);
$url = parse_url(urldecode($url), PHP_URL_PATH);
$url = rtrim($url, '/') . '/';
/* Match global regular-expression for route */
+3 -2
View File
@@ -40,13 +40,14 @@ class RouteGroup extends Route implements IGroupRoute
/**
* Method called to check if route matches
*
* @param string $url
* @param Request $request
* @return bool
*/
public function matchRoute(Request $request)
public function matchRoute($url, Request $request)
{
/* Skip if prefix doesn't match */
if ($this->prefix !== null && stripos($request->getUri(), $this->prefix) === false) {
if ($this->prefix !== null && stripos($url, $this->prefix) === false) {
return false;
}
@@ -76,9 +76,9 @@ class RouteResource extends LoadableRoute implements IControllerRoute
return true;
}
public function matchRoute(Request $request)
public function matchRoute($url, Request $request)
{
$url = parse_url(urldecode($request->getUri()), PHP_URL_PATH);
$url = parse_url(urldecode($url), PHP_URL_PATH);
$url = rtrim($url, '/') . '/';
/* Match global regular-expression for route */
+2 -2
View File
@@ -11,9 +11,9 @@ class RouteUrl extends LoadableRoute
$this->setCallback($callback);
}
public function matchRoute(Request $request)
public function matchRoute($url, Request $request)
{
$url = parse_url(urldecode($request->getUri()), PHP_URL_PATH);
$url = parse_url(urldecode($url), PHP_URL_PATH);
$url = rtrim($url, '/') . '/';
/* Match global regular-expression for route */
+58 -69
View File
@@ -69,24 +69,6 @@ class Router
*/
protected $exceptionHandlers;
/**
* The current loaded route
* @var ILoadableRoute|null
*/
protected $loadedRoute;
/**
* List over route changes (to avoid endless-looping)
* @var array
*/
protected $routeRewrites = [];
/**
* If the route has been rewritten/changed this property will contain the original url.
* @var string
*/
protected $originalUrl;
/**
* Get current router instance
* @return static
@@ -151,6 +133,8 @@ class Router
$exceptionHandlers = [];
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri();
/* @var $route IRoute */
for ($i = $max; $i >= 0; $i--) {
@@ -167,7 +151,7 @@ class Router
$route->renderRoute($this->request);
$this->processingRoute = false;
if ($route->matchRoute($this->request) === true) {
if ($route->matchRoute($url, $this->request) === true) {
/* Add exception handlers */
if (count($route->getExceptionHandlers()) > 0) {
@@ -216,30 +200,24 @@ class Router
public function routeRequest($rewrite = false)
{
$this->loadedRoute = null;
$routeNotAllowed = false;
try {
/* Initialize boot-managers */
if (count($this->bootManagers) > 0) {
$max = count($this->bootManagers) - 1;
/* @var $manager IRouterBootManager */
for ($i = $max; $i >= 0; $i--) {
$manager = $this->bootManagers[$i];
$this->request = $manager->boot($this->request);
if (!($this->request instanceof Request)) {
throw new HttpException('Bootmanager "' . get_class($manager) . '" must return instance of ' . Request::class, 500);
}
}
}
if ($rewrite === false) {
/* Initialize boot-managers */
if (count($this->bootManagers) > 0) {
$max = count($this->bootManagers) - 1;
/* @var $manager IRouterBootManager */
for ($i = $max; $i >= 0; $i--) {
$manager = $this->bootManagers[$i];
$manager->boot($this->request);
}
}
/* Loop through each route-request */
$this->processRoutes($this->routes);
@@ -248,19 +226,19 @@ class Router
/* Verify csrf token for request */
$this->csrfVerifier->handle($this->request);
}
$this->originalUrl = $this->request->getUri();
}
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri();
$max = count($this->processedRoutes) - 1;
/* @var $route IRoute */
/* @var $route ILoadableRoute */
for ($i = $max; $i >= 0; $i--) {
$route = $this->processedRoutes[$i];
/* If the route matches */
if ($route->matchRoute($this->request) === true) {
if ($route->matchRoute($url, $this->request) === true) {
/* Check if request method matches */
if (count($route->getRequestMethods()) > 0 && in_array($this->request->getMethod(), $route->getRequestMethods(), false) === false) {
@@ -268,12 +246,20 @@ class Router
continue;
}
$this->loadedRoute = $route;
$this->loadedRoute->loadMiddleware($this->request, $this->loadedRoute);
$route->loadMiddleware($this->request);
/* If the request has changed, we reinitialize the router */
if ($this->request->getUri() !== $this->originalUrl && in_array($this->request->getUri(), $this->routeRewrites) === false) {
$this->routeRewrites[] = $this->request->getUri();
if ($this->request->getRewriteRoute() !== null) {
$this->request->getRewriteRoute()->renderRoute($this->request);
return;
}
/* If the request has changed */
$rewriteUrl = $this->request->getRewriteUrl();
if ($rewriteUrl !== null && $rewriteUrl !== $url) {
unset($this->processedRoutes[$i]);
$this->processedRoutes = array_values($this->processedRoutes);
$this->routeRequest(true);
return;
@@ -281,8 +267,8 @@ class Router
/* Render route */
$routeNotAllowed = false;
$this->request->setUri($this->originalUrl);
$this->loadedRoute->renderRoute($this->request);
$this->request->setLoadedRoute($route);
$route->renderRoute($this->request);
break;
}
@@ -296,35 +282,45 @@ class Router
$this->handleException(new HttpException('Route or method not allowed', 403));
}
if ($this->loadedRoute === null) {
if ($this->request->getLoadedRoute() === null) {
$this->handleException(new NotFoundHttpException('Route not found: ' . $this->request->getUri(), 404));
}
}
protected function handleException(\Exception $e)
{
$url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri();
$max = count($this->exceptionHandlers);
/* @var $handler IExceptionHandler */
for ($i = 0; $i < $max; $i++) {
$handler = $this->exceptionHandlers[$i];
$handler = new $handler();
if (!($handler instanceof IExceptionHandler)) {
if (($handler instanceof IExceptionHandler) === false) {
throw new HttpException('Exception handler must implement the IExceptionHandler interface.', 500);
}
$request = $handler->handleError($this->request, $this->loadedRoute, $e);
if ($handler->handleError($this->request, $e) !== null) {
/* If the request has changed */
if ($request !== null && $this->request->getUri() !== $this->originalUrl && in_array($request->getUri(), $this->routeRewrites) === false) {
$this->request = $request;
$this->routeRewrites[] = $request->getUri();
$this->routeRequest(true);
if ($this->request->getRewriteRoute() !== null) {
$this->request->getRewriteRoute()->renderRoute($this->request);
return;
return;
}
$rewriteUrl = $this->request->getRewriteUrl();
/* If the request has changed */
if ($rewriteUrl !== null && $rewriteUrl !== $url) {
unset($this->exceptionHandlers[$i]);
$this->exceptionHandlers = array_values($this->exceptionHandlers);
$this->routeRequest(true);
return;
}
}
}
@@ -437,9 +433,11 @@ class Router
return (($url === '') ? '/' : $url . '/') . $this->arrayToParams($getParams);
}
$loadedRoute = $this->request->getLoadedRoute();
/* If nothing is defined and a route is loaded we use that */
if ($name === null && $this->loadedRoute !== null) {
return $this->loadedRoute->findUrl($this->loadedRoute->getMethod(), $parameters, $name) . $this->arrayToParams($getParams);
if ($name === null && $loadedRoute !== null) {
return $loadedRoute->findUrl($loadedRoute->getMethod(), $parameters, $name) . $this->arrayToParams($getParams);
}
/* We try to find a match on the given name */
@@ -548,13 +546,4 @@ class Router
return $this;
}
/**
* Get loaded route
* @return ILoadableRoute|null
*/
public function getLoadedRoute()
{
return $this->loadedRoute;
}
}
+2 -3
View File
@@ -1,12 +1,11 @@
<?php
require_once 'Exceptions/MiddlewareLoadedException.php';
use Pecee\Http\Middleware\IMiddleware;
use Pecee\Http\Request;
class DummyMiddleware implements IMiddleware
class DummyMiddleware implements \Pecee\Http\Middleware\IMiddleware
{
public function handle(Request $request, \Pecee\SimpleRouter\Route\ILoadableRoute &$route)
public function handle(Request $request)
{
throw new MiddlewareLoadedException('Middleware loaded!');
}
+1 -1
View File
@@ -2,7 +2,7 @@
class ExceptionHandler implements \Pecee\Handlers\IExceptionHandler
{
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\Route\ILoadableRoute &$route = null, \Exception $error)
public function handleError(\Pecee\Http\Request $request, \Exception $error)
{
echo $error->getMessage();
}
@@ -2,7 +2,7 @@
class TestExceptionHandlerFirst implements \Pecee\Handlers\IExceptionHandler
{
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\Route\ILoadableRoute &$route = null, \Exception $error)
public function handleError(\Pecee\Http\Request $request, \Exception $error)
{
echo 'ExceptionHandler 1 loaded' . chr(10);
@@ -2,7 +2,7 @@
class TestExceptionHandlerSecond implements \Pecee\Handlers\IExceptionHandler
{
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\Route\ILoadableRoute &$route = null, \Exception $error)
public function handleError(\Pecee\Http\Request $request, \Exception $error)
{
echo 'ExceptionHandler 2 loaded' . chr(10);
@@ -2,7 +2,7 @@
class TestExceptionHandlerThird implements \Pecee\Handlers\IExceptionHandler
{
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\Route\ILoadableRoute &$route = null, \Exception $error)
public function handleError(\Pecee\Http\Request $request, \Exception $error)
{
echo 'ExceptionHandler 3 loaded' . chr(10);