Compare commits

...

26 Commits

Author SHA1 Message Date
Simon Sessingø 2d57b45c7b Fixed urls not being visible 2016-11-06 09:04:05 +01:00
Simon Sessingø 98cc8504d4 Development
- Group only loads if prefix matches (if any).
2016-11-06 08:13:47 +01:00
Simon Sessingø 035a5b1629 Development
- Added support for cloudflare when using getIp method in Request.
- Fixed undefined variable notice in RouterBase class.
2016-11-05 23:07:14 +01:00
Simon Sessingø 832aff0358 Optimised for cli-usage 2016-10-28 07:40:51 +02:00
Simon Sessingø 43e05ad821 Minor optimisations 2016-10-27 19:15:38 +02:00
Simon Sessingø 2fd32868c2 Optimisations and bugfixes 2016-10-27 17:06:05 +02:00
Simon Sessingø e51b72f0e0 Development
- Changed from http_build_query to custom solution as it doesn't support querystrings with "%" on some php versions.
2016-10-27 16:44:35 +02:00
Simon Sessingø 3b5e2aee9d Added credits 2016-10-20 08:37:39 +02:00
Simon Sessingø 27ba532b2d Updated documentation 2016-10-20 08:36:50 +02:00
Simon Sessingø a8620cbc70 Updates
- Simplified exception-handling (see demo project for examples).
- Optimised sample-project.
- Optimised and added further unit-tests.
- Optimised and bugfixes.
2016-10-20 08:31:21 +02:00
Simon Sessingø 4e054dccf5 Updated documentation 2016-10-04 02:51:37 +02:00
Simon Sessingø e7dfbb159c Updated documentation 2016-10-04 02:50:36 +02:00
Simon Sessingø 8c5a5327d1 Update documentation 2016-10-04 02:47:28 +02:00
Simon Sessingø 3c9a675f25 Optimised middleware load order 2016-09-28 12:29:30 +02:00
Simon Sessingø dfccd99f2f Automatically push middlewares if multiple in nested group. 2016-09-28 12:21:23 +02:00
Simon Sessingø 980a4e9b6a Updated documentation and added demo-project. 2016-09-26 14:37:54 +02:00
Simon Sessingø fab0d0641c Allow for default parameters (including A-Z, a-z, 0-9 and "-") when parsing parameters in RouterResource. 2016-06-14 22:20:05 +02:00
Simon Sessingø 4169716f87 Bugfix
- Fixed some routes with ending slash not matching on when using Ressource.
2016-06-04 18:44:33 +02:00
Simon Sessingø 53e5b5362f Development
- Enhanced regular expression used for matching parameters.
- Added basic unit-tests for parameters.
- Fixed typos in PHP-docs and other minor optimizations.
2016-06-04 18:20:46 +02:00
Simon Sessingø 6780b24e59 Development
- Optimized the way parameters are parsed as a result, simple-router now supports routes like `/{param1}-{param2}.json`.
- Replaced reg-ex for parameter-matching with `\w` which means that default parameter matching on routes now include `_` (underscore) per default.
- Simplified `MiddlewareTest` class.
2016-06-04 15:12:04 +02:00
Simon Sessingø b540c01650 Update README.md 2016-06-03 00:02:45 +02:00
Simon Sessingø eb8832beec Added setValue method to InputItem class. 2016-05-04 13:35:23 +02:00
Simon Sessingø eeafcb7862 Added exist method to Input class. 2016-05-03 07:20:52 +02:00
Simon Sessingø 1a9351c690 - Fixed notifications if included in projects running in command-line where certain variables aren't available. 2016-05-01 00:02:42 +02:00
Simon Sessingø f98e5ac59d - Optimized handleException method in RouterBase. 2016-04-23 10:52:51 +02:00
Simon Sessingø a2dbf4149b - Added route to ExceptionHandler so Middlewares can be loaded. 2016-04-23 10:50:56 +02:00
26 changed files with 546 additions and 219 deletions
+2 -1
View File
@@ -1,3 +1,4 @@
.idea
composer.lock
vendor/
vendor/
demo-project/vendor
+17 -80
View File
@@ -32,6 +32,14 @@ The goal of this project is to create a router that is 100% compatible with the
- Custom boot managers to redirect urls to other routes
- Input manager; to manage `GET`, `POST` params.
## Installation and demo
We've included a simple demo project for the router which can be found in the `demo-project` folder.
Please refer to the demo-project documentation for further reading on how to setup and install simple-php-router:
[Link to demo documentation](demo-project/README.md)
## Initialising the router
In your ```index.php``` require your ```routes.php``` and call the ```routeRequest()``` method when all your custom routes has been loaded. This will trigger and do the actual routing of the requests.
@@ -108,7 +116,7 @@ SimpleRouter::group(['prefix' => 'v1', 'middleware' => '\MyWebsite\Middleware\So
This is a basic example of an ExceptionHandler implementation:
```php
namespace BB\Handlers;
namespace Demo\Handlers;
use Pecee\Http\Request;
use Pecee\SimpleRouter\RouterEntry;
@@ -132,7 +140,7 @@ class CustomExceptionHandler implements IExceptionHandler {
}
}
``
```
### Sub-domain routing
@@ -176,93 +184,21 @@ This is a simple example of an integration into a framework.
The framework has it's own ```Router``` class which inherits from the ```SimpleRouter``` class. This allows the framework to add custom functionality.
```php
<?php
<?php
namespace Pecee;
namespace Demo;
use Pecee\Exception\RouterException;
use Pecee\Handler\ExceptionHandler;
use Pecee\Http\Middleware\IMiddleware;
use Pecee\SimpleRouter\RouterBase;
use Pecee\SimpleRouter\SimpleRouter;
class Router extends SimpleRouter {
protected static $defaultExceptionHandler;
protected static $defaultMiddlewares = array();
public static function start($defaultNamespace = null) {
// Debug information
Debug::getInstance()->add('Router initialised.');
// change this to whatever makes sense in your project
require_once 'routes.php';
// Load framework specific controllers
static::get('/js-wrap', 'ControllerJs@wrap', ['namespace' => '\Pecee\Controller'])->setAlias('pecee.js.wrap');
static::get('/css-wrap', 'ControllerCss@wrap', ['namespace' => '\Pecee\Controller'])->setAlias('pecee.css.wrap');
static::get('/captcha', 'ControllerCaptcha@show', ['namespace' => '\Pecee\Controller']);
// Do initial stuff
// Load routes.php
$file = $_ENV['base_path'] . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'routes.php';
if(file_exists($file)) {
require_once $file;
}
parent::start('\\Demo\\Controllers');
// Set default namespace
$defaultNamespace = '\\'.$_ENV['app_name'] . '\\Controller';
// Handle exceptions
try {
if(count(static::$defaultMiddlewares)) {
/* @var $middleware \Pecee\Http\Middleware\IMiddleware */
foreach(static::$defaultMiddlewares as $middleware) {
$middleware = new $middleware();
if(!($middleware instanceof IMiddleware)) {
throw new RouterException('Middleware must be implement the IMiddleware interface.');
}
$middleware->handle(RouterBase::getInstance()->getRequest());
}
}
parent::start($defaultNamespace);
} catch(\Exception $e) {
$route = RouterBase::getInstance()->getLoadedRoute();
// Otherwise use the fallback default exceptions handler
if(static::$defaultExceptionHandler !== null) {
static::loadExceptionHandler(static::$defaultExceptionHandler, $route, $e);
}
throw $e;
}
}
protected static function loadExceptionHandler($class, $route, $e) {
$class = new $class();
if(!($class instanceof ExceptionHandler)) {
throw new \ErrorException('Exception handler must be an instance of \Pecee\Handler\ExceptionHandler');
}
$class->handleError(RouterBase::getInstance()->getRequest(), $route, $e);
}
public static function defaultExceptionHandler($handler) {
static::$defaultExceptionHandler = $handler;
}
/**
* Add default middleware that will be loaded before any route
* @param string|array $middlewares
*/
public static function defaultMiddleware($middlewares) {
if(is_array($middlewares)) {
static::$defaultMiddlewares = $middlewares;
} else {
static::$defaultMiddlewares[] = $middlewares;
}
}
}
@@ -342,7 +278,6 @@ Sometimes it can be necessary to keep urls stored in the database, file or simil
To interfere with the router, we create a class that inherits from ```RouterBootManager```. This class will be loaded before any other rules in ```routes.php``` and allow us to "change" the current route, if any of our criteria are fulfilled (like coming from the url ```/my-cat-is-beatiful```).
```php
use Pecee\Http\Request;
use Pecee\SimpleRouter\RouterBootManager;
@@ -481,6 +416,8 @@ This is some sites that uses the simple-router project in production.
- [holla.dk](http://www.holla.dk)
- [ninjaimg.com](http://ninjaimg.com)
- [bookandbegin.com](https://bookandbegin.com)
- [dscuz.com](https://www.dscuz.com)
## Documentation
While I work on a better documentation, please refer to the Laravel 5 routing documentation here:
+100
View File
@@ -0,0 +1,100 @@
# Simple PHP router demo project
This project is here to give you a basic understanding of how to setup and using simple-php-router.
Please note that this demo-project only covers how to integrate the `simple-php-router` in a project without a framework. If you are using some sort of PHP framework in your project the implementation might vary.
**What we won't cover:**
- How to setup a solution that fits your need. This is a basic demo to help you get started.
- Understanding of MVC; including Controllers, Middlewares or ExceptionHandlers.
- How to integrate into third party frameworks.
**What we cover:**
- How to get up and running fast - from scratch.
- How to get ExceptionHandlers, Middlewares and Controllers working.
- How to setup your webservers.
## Installation
- Navigate to the `demo-project` folder in terminal and run `composer update` to install the latest version.
- Point your webserver to `demo-project/public`.
### Setting up Nginx
If you are using Nginx please make sure that url-rewriting is enabled.
You can easily enable url-rewriting by adding the following configuration for the Nginx configuration-file for the demo-project.
```
location / {
try_files $uri $uri/ /index.php?$query_string;
}
```
### Setting up Apache
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.
## Folder structure
| Folder | Description |
| ------------- |-------------|
| app |Contains projects-specific PHP classes|
| public |Public folder which are accessible through the web.|
## Notes
The demo project has it's own `Router` class implementation which extends the `SimpleRouter` class with further functionality.
This class can be useful adding additional functionality that are required before and after routing occurs or any extra functionality belonging to the router itself.
In this project we also use our custom router-class to autoload the `routes.php` file from our custom location (`app/routes.php`).
Please check the `routes.php` file in `demo-project/app` for all the urls/rules available in the project.
### CSRF-verifier
For the purpose of this demo, we've added a custom CSRF-verifier middleware called `CsrfVerifier` and disabled CSRF checks for all calls to `/api/*`. This will ensure that CSRF form-checks are not applied when calling our demo api url.
### Exception handlers
The included `CustomExceptionHandler` class returns a very basic json response for errors received on calls to `/api/*` or otherwise just a simple formatted error response.
### Middlewares
`ApiVerification` class is added to all calls to `/api/*`. This simple class just adds some data to the `Request` object, which is returned in one of the methods in the `ApiController` class. We've added this class to demonstrate that you can use middlewares to ensure that the user has the correct authentication - before router loads the controller itself.
### Urls
Please see `routes.php` for all routes and rules.
| URL |
| ------------- |
| / |
| /api/demo |
| /companies |
| /companies/[id] |
| /contact |
## The MIT License (MIT)
Copyright (c) 2016 Simon Sessingø / simple-php-router
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@@ -0,0 +1,22 @@
<?php
namespace Demo\Controllers;
use Pecee\Http\Request;
class ApiController {
public function index() {
// The variable authenticated is set to true in the ApiVerification middleware class.
$request = Request::getInstance();
header('content-type: application/json');
echo json_encode([
'authenticated' => $request->authenticated
]);
}
}
@@ -0,0 +1,29 @@
<?php
namespace Demo\Controllers;
class DefaultController {
public function index() {
// implement
echo 'DefaultController -> index';
}
public function contact() {
echo 'DefaultController -> contact';
}
public function companies($id = null) {
echo 'DefaultController -> companies -> id: ' . $id;
}
public function notFound() {
echo 'Page not found';
}
}
@@ -0,0 +1,34 @@
<?php
namespace Demo\Handlers;
use Pecee\Handler\IExceptionHandler;
use Pecee\Http\Request;
use Pecee\SimpleRouter\RouterEntry;
class CustomExceptionHandler implements IExceptionHandler {
public function handleError( Request $request, RouterEntry $router = null, \Exception $error) {
// Return json errors if we encounter an error on /api.
if(stripos($request->getUri(), '/api') !== false) {
header('content-type: application/json');
echo json_encode([
'error' => $error->getMessage(),
'code' => $error->getCode()
]);
die();
}
// else we just throw the error
if($error->getCode() == 404) {
// Return 404 path
$request->setUri('/404');
return $request;
}
throw $error;
}
}
@@ -0,0 +1,16 @@
<?php
namespace Demo\Middlewares;
use Pecee\Http\Middleware\IMiddleware;
use Pecee\Http\Request;
class ApiVerification implements IMiddleware {
public function handle(Request $request) {
// Do authentication
$request->authenticated = true;
}
}
@@ -0,0 +1,13 @@
<?php
namespace Demo\Middlewares;
use Pecee\Http\Middleware\BaseCsrfVerifier;
class CsrfVerifier extends BaseCsrfVerifier {
/**
* CSRF validation will be ignored on the following urls.
*/
protected $except = ['/api/*'];
}
+25
View File
@@ -0,0 +1,25 @@
<?php
/**
* Custom router which handles default middlewares, default exceptions and things
* that should be happen before and after the router is initialised.
*/
namespace Demo;
use Pecee\SimpleRouter\SimpleRouter;
class Router extends SimpleRouter {
public static function start($defaultNamespace = null) {
// change this to whatever makes sense in your project
require_once 'routes.php';
// Do initial stuff
parent::start('\\Demo\\Controllers');
}
}
+23
View File
@@ -0,0 +1,23 @@
<?php
/**
* This file contains all the routes for the project
*/
use Demo\Router;
Router::csrfVerifier(new \Demo\Middlewares\CsrfVerifier());
Router::group(['exceptionHandler' => 'Demo\Handlers\CustomExceptionHandler'], function() {
Router::get('/', 'DefaultController@index')->setAlias('home');
Router::get('/contact', 'DefaultController@contact')->setAlias('contact');
Router::get('/404', 'DefaultController@notFound')->setAlias('404');
Router::basic('/companies', 'DefaultController@companies')->setAlias('companies');
Router::basic('/companies/{id}', 'DefaultController@companies')->setAlias('companies');
// Api
Router::group(['prefix' => '/api', 'middleware' => 'Demo\Middlewares\ApiVerification'], function() {
Router::resource('/demo', 'ApiController');
});
});
+26
View File
@@ -0,0 +1,26 @@
{
"name": "pecee/simple-router-demo",
"description": "Simple router demo project",
"keywords": [
"simple-router",
"php",
"php-simple-router"
],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.4.0",
"pecee/simple-router": "1.*"
},
"require-dev": {
},
"config": {
"preferred-install": "dist"
},
"autoload": {
"psr-4": {
"Demo\\": "app/"
}
}
}
+5
View File
@@ -0,0 +1,5 @@
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1
+7
View File
@@ -0,0 +1,7 @@
<?php
// load composer dependencies
require '../vendor/autoload.php';
// Start the routing
\Demo\Router::start();
+5 -1
View File
@@ -97,6 +97,10 @@ class Input {
return $default;
}
public function exists($index) {
return ($this->getObject($index) !== null);
}
public function setGet() {
$this->get = new InputCollection();
@@ -123,7 +127,7 @@ class Input {
$postVars = array();
if(in_array($_SERVER['REQUEST_METHOD'], ['PUT', 'PATCH', 'DELETE'])) {
if(isset($_SERVER['REQUEST_METHOD']) && in_array($_SERVER['REQUEST_METHOD'], ['PUT', 'PATCH', 'DELETE'])) {
parse_str(file_get_contents('php://input'), $postVars);
} else {
$postVars = $_POST;
+10
View File
@@ -46,6 +46,16 @@ class InputItem {
return $this;
}
/**
* Set input value
* @param string $value
* @return static $this
*/
public function setValue($value) {
$this->value = $value;
return $this;
}
public function __toString() {
return (string)$this->getValue();
}
+7 -4
View File
@@ -22,9 +22,9 @@ class Request {
public function __construct() {
$this->data = array();
$this->host = $_SERVER['HTTP_HOST'];
$this->uri = $_SERVER['REQUEST_URI'];
$this->method = (isset($_POST['_method'])) ? strtolower($_POST['_method']) : strtolower($_SERVER['REQUEST_METHOD']);
$this->host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : array();
$this->uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : array();
$this->method = (isset($_POST['_method'])) ? strtolower($_POST['_method']) : (isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : array());
$this->headers = $this->getAllHeaders();
$this->input = new Input();
}
@@ -96,7 +96,10 @@ class Request {
* @return string
*/
public function getIp() {
return ((isset($_SERVER['HTTP_X_FORWARDED_FOR']) && strlen($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']);
if(isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
return $_SERVER['HTTP_CF_CONNECTING_IP'];
}
return ((isset($_SERVER['HTTP_X_FORWARDED_FOR']) && strlen($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null);
}
/**
+96 -76
View File
@@ -72,27 +72,36 @@ class RouterBase {
array_push($newPrefixes, trim($route->getPrefix(), '/'));
}
/* @var $group RouterGroup */
$group = null;
if(!($route instanceof RouterGroup)) {
if(is_array($newPrefixes) && count($newPrefixes) && $backStack) {
$route->setUrl( '/' . join('/', $newPrefixes) . $route->getUrl() );
}
$group = null;
$this->controllerUrlMap[] = $route;
}
$this->currentRoute = $route;
if($route instanceof RouterGroup && is_callable($route->getCallback())) {
$group = $route;
$route->renderRoute($this->request);
$mergedSettings = array_merge($settings, $route->getMergeableSettings());
// Add exceptionhandler
if($route->matchRoute($this->request) && $route->getExceptionHandler() !== null) {
$this->exceptionHandlers[] = $route->getExceptionHandler();
if($route->matchRoute($this->request)) {
$group = $route;
$mergedSettings = array_merge($settings, $group->getMergeableSettings());
// Add ExceptionHandler
if ($group->getExceptionHandler() !== null) {
$this->exceptionHandlers[] = $route;
}
}
}
$this->currentRoute = null;
@@ -107,64 +116,68 @@ class RouterBase {
}
}
public function routeRequest() {
public function routeRequest($original = true) {
$originalUri = $this->request->getUri();
// Initialize boot-managers
if(count($this->bootManagers)) {
/* @var $manager RouterBootManager */
foreach($this->bootManagers as $manager) {
$this->request = $manager->boot($this->request);
if(!($this->request instanceof Request)) {
throw new RouterException('Custom router bootmanager "'. get_class($manager) .'" must return instance of Request.');
}
}
}
// Verify csrf token for request
if($this->baseCsrfVerifier !== null) {
$this->baseCsrfVerifier->handle($this->request);
}
// Loop through each route-request
$this->processRoutes($this->routes);
$routeNotAllowed = false;
$max = count($this->controllerUrlMap);
try {
/* @var $route RouterEntry */
for($i = 0; $i < $max; $i++) {
// Initialize boot-managers
if(count($this->bootManagers)) {
/* @var $manager RouterBootManager */
foreach($this->bootManagers as $manager) {
$this->request = $manager->boot($this->request);
$route = $this->controllerUrlMap[$i];
$routeMatch = $route->matchRoute($this->request);
if($routeMatch) {
if(count($route->getRequestMethods()) && !in_array($this->request->getMethod(), $route->getRequestMethods())) {
$routeNotAllowed = true;
continue;
if(!($this->request instanceof Request)) {
throw new RouterException('Custom router bootmanager "'. get_class($manager) .'" must return instance of Request.');
}
}
$routeNotAllowed = false;
$this->request->rewrite_uri = $this->request->uri;
$this->request->setUri($originalUri);
$this->request->loadedRoute = $route;
$route->loadMiddleware($this->request);
try {
$this->request->loadedRoute->renderRoute($this->request);
} catch(\Exception $e) {
$this->handleException($e);
}
break;
}
// Loop through each route-request
$this->processRoutes($this->routes);
if($original === true) {
// Verify csrf token for request
if ($this->baseCsrfVerifier !== null) {
$this->baseCsrfVerifier->handle($this->request);
}
}
$max = count($this->controllerUrlMap);
/* @var $route RouterEntry */
for ($i = 0; $i < $max; $i++) {
$route = $this->controllerUrlMap[$i];
$routeMatch = $route->matchRoute($this->request);
if ($routeMatch) {
if (count($route->getRequestMethods()) && !in_array($this->request->getMethod(), $route->getRequestMethods())) {
$routeNotAllowed = true;
continue;
}
$routeNotAllowed = false;
$this->request->rewrite_uri = $this->request->uri;
$this->request->setUri($originalUri);
$this->request->loadedRoute = $route;
$route->loadMiddleware($this->request);
$this->request->loadedRoute->renderRoute($this->request);
break;
}
}
} catch(\Exception $e) {
$this->handleException($e);
}
if($routeNotAllowed) {
@@ -178,13 +191,25 @@ class RouterBase {
protected function handleException(\Exception $e) {
foreach ($this->exceptionHandlers as $handler) {
$handler = new $handler($this->request);
$request = null;
/* @var $route RouterGroup */
foreach ($this->exceptionHandlers as $route) {
$route->loadMiddleware($this->request);
$handler = $route->getExceptionHandler();
$handler = new $handler();
if (!($handler instanceof IExceptionHandler)) {
throw new RouterException('Exception handler must implement the IExceptionHandler interface.');
}
$handler->handleError($this->request, $this->request->loadedRoute, $e);
$request = $handler->handleError($this->request, $this->request->loadedRoute, $e);
}
if($request !== null) {
$this->request = $request;
$this->routeRequest(false);
return;
}
throw $e;
@@ -285,17 +310,16 @@ class RouterBase {
public function arrayToParams(array $getParams = null, $includeEmpty = true) {
if(is_array($getParams)) {
if(is_array($getParams) && count($getParams)) {
if ($includeEmpty === false) {
$getParams = array_filter($getParams, function ($item) {
if (!empty($item)) {
return $item;
}
return (!empty($item));
});
}
return http_build_query($getParams);
return '?' . http_build_query($getParams);
}
return '';
}
@@ -350,8 +374,8 @@ class RouterBase {
$url = rtrim($url, '/') . '/';
if($getParams !== null && count($getParams)) {
$url .= '?' . $this->arrayToParams($getParams);
if($getParams !== null) {
$url .= $this->arrayToParams($getParams);
}
return $url;
@@ -373,8 +397,8 @@ class RouterBase {
$url = parse_url($this->request->getUri(), PHP_URL_PATH);
if(count($getParams)) {
$url .= '?' . $this->arrayToParams($getParams);
if($getParams !== null) {
$url .= $this->arrayToParams($getParams);
}
return $url;
@@ -445,22 +469,18 @@ class RouterBase {
$url = '/' . trim(join('/', $url), '/') . '/';
if($getParams !== null && count($getParams)) {
$url .= '?' . $this->arrayToParams($getParams);
if($getParams !== null) {
$url .= $this->arrayToParams($getParams);
}
return $url;
}
public static function getInstance() {
if(self::$instance === null) {
self::$instance = new static();
if(static::$instance === null) {
static::$instance = new static();
}
return self::$instance;
}
public static function reset() {
self::$instance = null;
return static::$instance;
}
}
+8 -13
View File
@@ -232,7 +232,7 @@ abstract class RouterEntry {
}
/**
* Dynamicially set settings value
* Dynamically set settings value
*
* @param string $name
* @param mixed|null $value
@@ -249,7 +249,7 @@ abstract class RouterEntry {
return new $name();
}
protected function parseParameters($route, $url, $parameterRegex = '[a-z0-9]+') {
protected function parseParameters($route, $url, $parameterRegex = '[\w]+') {
$parameterNames = array();
$regex = '';
$lastCharacter = '';
@@ -261,12 +261,6 @@ abstract class RouterEntry {
$character = $route[$i];
// Skip "/" if we are at the end of a parameter
if($lastCharacter === '}' && $character === '/') {
$lastCharacter = $character;
continue;
}
if($character === '{') {
// Remove "/" and "\" from regex
if(substr($regex, strlen($regex)-1) === '/') {
@@ -285,10 +279,10 @@ abstract class RouterEntry {
if($lastCharacter === '?') {
$parameter = substr($parameter, 0, strlen($parameter)-1);
$regex .= '(?:\\/?(?P<'.$parameter.'>[^\/]+)?\\/?)';
$regex .= '(?:\/?(?P<' . $parameter . '>'. $parameterRegex .')[^\/]?)?';
$required = false;
} else {
$regex .= '\\/(?P<' . $parameter . '>'. $parameterRegex .')\\/';
$regex .= '\/?(?P<' . $parameter . '>'. $parameterRegex .')[^\/]?';
}
$parameterNames[] = array('name' => $parameter, 'required' => $required);
$parameter = '';
@@ -307,7 +301,8 @@ abstract class RouterEntry {
$parameterValues = array();
if(preg_match('/^'.$regex.'$/is', $url, $parameterValues)) {
if(preg_match('/^'.$regex.'\/?$/is', $url, $parameterValues)) {
$parameters = array();
$max = count($parameterNames);
@@ -315,7 +310,7 @@ abstract class RouterEntry {
if($max) {
for($i = 0; $i < $max; $i++) {
$name = $parameterNames[$i];
$parameterValue = (isset($parameterValues[$name['name']]) && !empty($parameterValues[$name['name']])) ? $parameterValues[$name['name']] : null;
$parameterValue = isset($parameterValues[$name['name']]) ? $parameterValues[$name['name']] : null;
if($name['required'] && $parameterValue === null) {
throw new RouterException('Missing required parameter ' . $name['name'], 404);
@@ -399,7 +394,7 @@ abstract class RouterEntry {
}
/**
* Get allowed requeset methods
* Get allowed request methods
*
* @return array
*/
+14
View File
@@ -93,9 +93,23 @@ class RouterGroup extends RouterEntry {
unset($settings['namespace']);
}
// Push middleware if multiple
if($this->getMiddleware() !== null && isset($settings['middleware'])) {
if(!is_array($this->getMiddleware())) {
$middlewares = [$this->getMiddleware(), $settings['middleware']];
} else {
$middlewares = array_push($settings['middleware']);
}
$settings['middleware'] = array_unique(array_reverse($middlewares));
}
if(is_array($settings)) {
$this->settings = array_merge($this->settings, $settings);
}
return $this;
}
+1 -1
View File
@@ -52,7 +52,7 @@ class RouterResource extends RouterEntry {
$route = rtrim($this->url, '/') . '/{id?}/{action?}';
$parameters = $this->parseParameters($route, $url, '[0-9]+?');
$parameters = $this->parseParameters($route, $url);
if($parameters !== null) {
+1 -1
View File
@@ -105,7 +105,7 @@ class RouterRoute extends RouterEntry {
} else {
return strtolower($this->getAlias()) === strtolower($name);
}
return false;
}
+9
View File
@@ -6,4 +6,13 @@ class DummyController {
echo static::class . '@' .'start() OK';
}
public function param($params = null) {
$params = func_get_args();
echo 'Params: ' . join(', ', $params);
}
public function notFound() {
echo 'not found';
}
}
+8
View File
@@ -0,0 +1,8 @@
<?php
class ExceptionHandler implements \Pecee\Handler\IExceptionHandler {
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\RouterEntry $router = null, \Exception $error){
throw $error;
}
}
+2 -10
View File
@@ -7,19 +7,11 @@ class GroupTest extends PHPUnit_Framework_TestCase {
protected $result;
public function __construct() {
// Initial setup
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/api/v1/test';
$_SERVER['REQUEST_METHOD'] = 'get';
}
protected function group() {
$this->result = true;
}
public function testGroup() {
\Pecee\SimpleRouter\RouterBase::reset();
$this->result = false;
@@ -35,9 +27,9 @@ class GroupTest extends PHPUnit_Framework_TestCase {
}
public function testNestedGroup() {
\Pecee\SimpleRouter\RouterBase::reset();
\Pecee\Http\Request::getInstance()->setUri('/api/v1/test');
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/api/v1/test');
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setMethod('get');
\Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/api'], function() {
\Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/v1'], function() {
+9 -15
View File
@@ -2,35 +2,29 @@
require_once 'Dummy/DummyMiddleware.php';
require_once 'Dummy/DummyController.php';
require_once 'Dummy/Handler/ExceptionHandler.php';
class MiddlewareTest extends PHPUnit_Framework_TestCase {
public function __construct() {
// Initial setup
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/my/test/url';
$_SERVER['REQUEST_METHOD'] = 'get';
}
public function testMiddlewareFound() {
\Pecee\Http\Request::getInstance()->setMethod('get');
\Pecee\Http\Request::getInstance()->setUri('/my/test/url');
\Pecee\SimpleRouter\RouterBase::reset();
\Pecee\SimpleRouter\SimpleRouter::group(['exceptionHandler' => 'ExceptionHandler'], function() {
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start', ['middleware' => 'DummyMiddleware']);
});
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start', ['middleware' => 'DummyMiddleware']);
$found = false;
try {
\Pecee\SimpleRouter\SimpleRouter::start();
}catch(Exception $e) {
$this->assertTrue(($e instanceof MiddlewareLoadedException));
return;
}catch(\Exception $e) {
$found = ($e instanceof MiddlewareLoadedException);
}
throw new Exception('Middleware not loaded');
$this->assertTrue($found);
}
}
+57 -17
View File
@@ -2,58 +2,68 @@
require_once 'Dummy/DummyMiddleware.php';
require_once 'Dummy/DummyController.php';
require_once 'Dummy/Handler/ExceptionHandler.php';
class RouterRouteTest extends PHPUnit_Framework_TestCase {
public function testNotFound() {
\Pecee\Http\Request::getInstance()->setMethod('get');
\Pecee\Http\Request::getInstance()->setUri('/test-param1-param2');
\Pecee\SimpleRouter\SimpleRouter::group(['exceptionHandler' => 'ExceptionHandler'], function() {
\Pecee\SimpleRouter\SimpleRouter::get('/non-existing-path', 'DummyController@start');
});
$found = false;
try {
\Pecee\SimpleRouter\SimpleRouter::start();
}catch(\Exception $e) {
$found = ($e instanceof \Pecee\Exception\RouterException && $e->getCode() == 404);
}
$this->assertTrue($found);
public function __construct() {
// Initial setup
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/my/test/url';
$_SERVER['REQUEST_METHOD'] = 'get';
}
public function testGet() {
\Pecee\SimpleRouter\RouterBase::reset();
\Pecee\Http\Request::getInstance()->setMethod('get');
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/my/test/url');
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setMethod('get');
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start');
\Pecee\SimpleRouter\SimpleRouter::start();
}
public function testPost() {
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/my/test/url');
\Pecee\Http\Request::getInstance()->setMethod('post');
\Pecee\SimpleRouter\RouterBase::reset();
\Pecee\SimpleRouter\SimpleRouter::post('/my/test/url', 'DummyController@start');
\Pecee\SimpleRouter\SimpleRouter::start();
}
public function testPut() {
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/my/test/url');
\Pecee\Http\Request::getInstance()->setMethod('put');
\Pecee\SimpleRouter\RouterBase::reset();
\Pecee\SimpleRouter\SimpleRouter::put('/my/test/url', 'DummyController@start');
\Pecee\SimpleRouter\SimpleRouter::start();
}
public function testDelete() {
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/my/test/url');
\Pecee\Http\Request::getInstance()->setMethod('delete');
\Pecee\SimpleRouter\RouterBase::reset();
\Pecee\SimpleRouter\SimpleRouter::delete('/my/test/url', 'DummyController@start');
\Pecee\SimpleRouter\SimpleRouter::start();
}
public function testMethodNotAllowed() {
\Pecee\SimpleRouter\RouterBase::reset();
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/my/test/url');
\Pecee\Http\Request::getInstance()->setMethod('post');
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start');
@@ -66,4 +76,34 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
}
public function testSimpleParam() {
\Pecee\Http\Request::getInstance()->setMethod('get');
\Pecee\Http\Request::getInstance()->setUri('/test-param1');
\Pecee\SimpleRouter\SimpleRouter::get('/test-{param1}', 'DummyController@param');
\Pecee\SimpleRouter\SimpleRouter::start();
}
public function testMultiParam() {
\Pecee\Http\Request::getInstance()->setMethod('get');
\Pecee\Http\Request::getInstance()->setUri('/test-param1-param2');
\Pecee\SimpleRouter\SimpleRouter::get('/test-{param1}-{param2}', 'DummyController@param');
\Pecee\SimpleRouter\SimpleRouter::start();
}
public function testPathParam() {
\Pecee\Http\Request::getInstance()->setMethod('get');
\Pecee\Http\Request::getInstance()->setUri('/test/path/param1');
\Pecee\SimpleRouter\SimpleRouter::get('/test/path/{param}', 'DummyController@param');
\Pecee\SimpleRouter\SimpleRouter::start();
}
}