mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 16:57:53 +00:00
Compare commits
35 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bfdaf8ac52 | |||
| 71dc6e172f | |||
| 6ee172927f | |||
| bb5e629199 | |||
| 0cc0a59fd5 | |||
| 498fd6b07d | |||
| 96ab22a4f8 | |||
| 7f528c133b | |||
| 5a50190293 | |||
| 355ef01d63 | |||
| d3162b5a2b | |||
| 810b80487d | |||
| 18a9df56ca | |||
| 6e14ded03f | |||
| 899081f8d8 | |||
| e7b9206bc9 | |||
| cd6e800984 | |||
| 11bd5a7d11 | |||
| be32796b01 | |||
| 8b3d71a328 | |||
| 1fae638aaf | |||
| 37c8bc9f32 | |||
| 75029b330a | |||
| fd5d893040 | |||
| c1512740af | |||
| 212ae133de | |||
| ae58231fa1 | |||
| 358b25d4f1 | |||
| 3d45851d9b | |||
| ee5c2207f8 | |||
| b1ca3fc9ef | |||
| 253c0c70d4 | |||
| 53ba2d7ac5 | |||
| 315fe05769 | |||
| a57113309a |
@@ -116,7 +116,7 @@ SimpleRouter::group(['prefix' => 'v1', 'middleware' => '\MyWebsite\Middleware\So
|
||||
This is a basic example of an ExceptionHandler implementation:
|
||||
|
||||
```php
|
||||
namespace Demo\Handlers;
|
||||
namespace BB\Handlers;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\RouterEntry;
|
||||
@@ -140,7 +140,7 @@ class CustomExceptionHandler implements IExceptionHandler {
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
``
|
||||
|
||||
### Sub-domain routing
|
||||
|
||||
@@ -184,21 +184,91 @@ 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
|
||||
namespace Demo;
|
||||
namespace Pecee;
|
||||
|
||||
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) {
|
||||
|
||||
// change this to whatever makes sense in your project
|
||||
require_once 'routes.php';
|
||||
// Debug information
|
||||
Debug::getInstance()->add('Router initialised.');
|
||||
|
||||
// Do initial stuff
|
||||
// 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']);
|
||||
|
||||
parent::start('\\Demo\\Controllers');
|
||||
// Load routes.php
|
||||
$file = $_ENV['base_path'] . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'routes.php';
|
||||
if(file_exists($file)) {
|
||||
require_once $file;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -278,6 +348,7 @@ 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;
|
||||
|
||||
@@ -417,7 +488,6 @@ 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:
|
||||
|
||||
+18
-14
@@ -2,30 +2,31 @@
|
||||
|
||||
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.
|
||||
Please note that this demo-project only covers how to integrate simple-php-project 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 add Controllers, Middlewares or ExceptionHandlers with cool functionality.
|
||||
- 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 set 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.
|
||||
- Navigate to the `demo-project` folder and run `composer install`.
|
||||
- Point your webserver to `demo-project/public`.
|
||||
|
||||
### Setting up Nginx
|
||||
|
||||
If you are using Nginx please make sure that url-rewriting is enabled.
|
||||
If you are using Nginx remember to enable url-rewriting.
|
||||
|
||||
You can easily enable url-rewriting by adding the following configuration for the Nginx configuration-file for the demo-project.
|
||||
You can easily do this by adding the following configuration for the Nginx configuration for the demo-project.
|
||||
|
||||
```
|
||||
location / {
|
||||
@@ -35,7 +36,8 @@ location / {
|
||||
|
||||
### 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.
|
||||
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 `.htaccess` support is enabled in the Apache configuration - or add the rules manually.
|
||||
|
||||
## Folder structure
|
||||
|
||||
@@ -46,24 +48,26 @@ Nothing special is required for Apache to work. We've include the `.htaccess` fi
|
||||
|
||||
## 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.
|
||||
The demo project has it's own `Router` class implemented which extends the `SimpleRouter` class with further functionality such as
|
||||
default exceptionhandlers and middlewares. This class can be useful adding functionality that are required before and after routing
|
||||
occurs or add extra functionality to the router.
|
||||
|
||||
In this project we also use our custom router-class to autoload the `routes.php` file from our custom location (`app/routes.php`).
|
||||
In this project we also use our custom router-class to autoload the `routes.php` file.
|
||||
|
||||
Please check the `routes.php` file in `demo-project/app` for all the urls/rules available in the project.
|
||||
Please check the `routes.php` file in `demo-project/app` for all the urls/rules 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.
|
||||
We've added a custom CSRF-verifier middleware called `CsrfVerifier` and disabled CSRF checks for all calls to `/api/*`.
|
||||
|
||||
### 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.
|
||||
The included `CustomExceptionHandler` class returns a json response for errors received on calls to `/api/*` or otherwise just forms 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.
|
||||
`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.
|
||||
|
||||
### Urls
|
||||
|
||||
|
||||
@@ -22,8 +22,4 @@ class DefaultController {
|
||||
|
||||
}
|
||||
|
||||
public function notFound() {
|
||||
echo 'Page not found';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,14 +21,8 @@ class CustomExceptionHandler implements IExceptionHandler {
|
||||
|
||||
// else we just throw the error
|
||||
if($error->getCode() == 404) {
|
||||
|
||||
// Return 404 path
|
||||
$request->setUri('/404');
|
||||
return $request;
|
||||
|
||||
die(sprintf('An error occurred (%s):<br/>%s', $error->getCode(), $error->getMessage()));
|
||||
}
|
||||
|
||||
throw $error;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,19 +7,78 @@
|
||||
|
||||
namespace Demo;
|
||||
|
||||
use Pecee\Exception\RouterException;
|
||||
use Pecee\Handler\IExceptionHandler;
|
||||
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) {
|
||||
|
||||
// change this to whatever makes sense in your project
|
||||
require_once 'routes.php';
|
||||
|
||||
// Do initial stuff
|
||||
// Handle exceptions
|
||||
try {
|
||||
|
||||
parent::start('\\Demo\\Controllers');
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
// Set default namespace
|
||||
$defaultNamespace = '\\Demo\\Controllers';
|
||||
|
||||
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 IExceptionHandler)) {
|
||||
throw new \ErrorException('Exception handler must be an instance of \Pecee\Handler\IExceptionHandler');
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,18 +6,14 @@
|
||||
use Demo\Router;
|
||||
|
||||
Router::csrfVerifier(new \Demo\Middlewares\CsrfVerifier());
|
||||
Router::defaultExceptionHandler('\Demo\Handlers\CustomExceptionHandler');
|
||||
|
||||
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');
|
||||
});
|
||||
Router::get('/', 'DefaultController@index')->setAlias('home');
|
||||
Router::get('/contact', 'DefaultController@contact')->setAlias('contact');
|
||||
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');
|
||||
});
|
||||
@@ -96,10 +96,7 @@ class Request {
|
||||
* @return string
|
||||
*/
|
||||
public function getIp() {
|
||||
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);
|
||||
return ((isset($_SERVER['HTTP_X_FORWARDED_FOR']) && strlen($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -86,22 +86,15 @@ class RouterBase {
|
||||
$this->currentRoute = $route;
|
||||
|
||||
if($route instanceof RouterGroup && is_callable($route->getCallback())) {
|
||||
$group = $route;
|
||||
|
||||
$route->renderRoute($this->request);
|
||||
|
||||
if($route->matchRoute($this->request)) {
|
||||
|
||||
$group = $route;
|
||||
|
||||
$mergedSettings = array_merge($settings, $group->getMergeableSettings());
|
||||
|
||||
// Add ExceptionHandler
|
||||
if ($group->getExceptionHandler() !== null) {
|
||||
$this->exceptionHandlers[] = $route;
|
||||
}
|
||||
$group->renderRoute($this->request);
|
||||
$mergedSettings = array_merge($settings, $group->getMergeableSettings());
|
||||
|
||||
// Add ExceptionHandler
|
||||
if($group->matchRoute($this->request) && $group->getExceptionHandler() !== null) {
|
||||
$this->exceptionHandlers[] = $route;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$this->currentRoute = null;
|
||||
@@ -116,68 +109,64 @@ class RouterBase {
|
||||
}
|
||||
}
|
||||
|
||||
public function routeRequest($original = true) {
|
||||
public function routeRequest() {
|
||||
|
||||
$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;
|
||||
|
||||
try {
|
||||
$max = count($this->controllerUrlMap);
|
||||
|
||||
// Initialize boot-managers
|
||||
if(count($this->bootManagers)) {
|
||||
/* @var $manager RouterBootManager */
|
||||
foreach($this->bootManagers as $manager) {
|
||||
$this->request = $manager->boot($this->request);
|
||||
/* @var $route RouterEntry */
|
||||
for($i = 0; $i < $max; $i++) {
|
||||
|
||||
if(!($this->request instanceof Request)) {
|
||||
throw new RouterException('Custom router bootmanager "'. get_class($manager) .'" must return instance of 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;
|
||||
}
|
||||
}
|
||||
|
||||
// Loop through each route-request
|
||||
$this->processRoutes($this->routes);
|
||||
$routeNotAllowed = false;
|
||||
|
||||
if($original === true) {
|
||||
// Verify csrf token for request
|
||||
if ($this->baseCsrfVerifier !== null) {
|
||||
$this->baseCsrfVerifier->handle($this->request);
|
||||
}
|
||||
}
|
||||
$this->request->rewrite_uri = $this->request->uri;
|
||||
$this->request->setUri($originalUri);
|
||||
|
||||
$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 = $route;
|
||||
$route->loadMiddleware($this->request);
|
||||
|
||||
try {
|
||||
$this->request->loadedRoute->renderRoute($this->request);
|
||||
|
||||
break;
|
||||
} catch(\Exception $e) {
|
||||
$this->handleException($e);
|
||||
}
|
||||
}
|
||||
|
||||
} catch(\Exception $e) {
|
||||
$this->handleException($e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if($routeNotAllowed) {
|
||||
@@ -191,8 +180,6 @@ class RouterBase {
|
||||
|
||||
protected function handleException(\Exception $e) {
|
||||
|
||||
$request = null;
|
||||
|
||||
/* @var $route RouterGroup */
|
||||
foreach ($this->exceptionHandlers as $route) {
|
||||
$route->loadMiddleware($this->request);
|
||||
@@ -203,13 +190,7 @@ class RouterBase {
|
||||
throw new RouterException('Exception handler must implement the IExceptionHandler interface.');
|
||||
}
|
||||
|
||||
$request = $handler->handleError($this->request, $this->request->loadedRoute, $e);
|
||||
}
|
||||
|
||||
if($request !== null) {
|
||||
$this->request = $request;
|
||||
$this->routeRequest(false);
|
||||
return;
|
||||
$handler->handleError($this->request, $this->request->loadedRoute, $e);
|
||||
}
|
||||
|
||||
throw $e;
|
||||
@@ -310,16 +291,15 @@ class RouterBase {
|
||||
|
||||
public function arrayToParams(array $getParams = null, $includeEmpty = true) {
|
||||
|
||||
if(is_array($getParams) && count($getParams)) {
|
||||
if(is_array($getParams)) {
|
||||
if ($includeEmpty === false) {
|
||||
$getParams = array_filter($getParams, function ($item) {
|
||||
return (!empty($item));
|
||||
});
|
||||
}
|
||||
|
||||
return '?' . http_build_query($getParams);
|
||||
return http_build_query($getParams);
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -374,8 +354,8 @@ class RouterBase {
|
||||
|
||||
$url = rtrim($url, '/') . '/';
|
||||
|
||||
if($getParams !== null) {
|
||||
$url .= $this->arrayToParams($getParams);
|
||||
if($getParams !== null && count($getParams)) {
|
||||
$url .= '?' . $this->arrayToParams($getParams);
|
||||
}
|
||||
|
||||
return $url;
|
||||
@@ -397,8 +377,8 @@ class RouterBase {
|
||||
|
||||
$url = parse_url($this->request->getUri(), PHP_URL_PATH);
|
||||
|
||||
if($getParams !== null) {
|
||||
$url .= $this->arrayToParams($getParams);
|
||||
if(count($getParams)) {
|
||||
$url .= '?' . $this->arrayToParams($getParams);
|
||||
}
|
||||
|
||||
return $url;
|
||||
@@ -469,18 +449,22 @@ class RouterBase {
|
||||
|
||||
$url = '/' . trim(join('/', $url), '/') . '/';
|
||||
|
||||
if($getParams !== null) {
|
||||
$url .= $this->arrayToParams($getParams);
|
||||
if($getParams !== null && count($getParams)) {
|
||||
$url .= '?' . $this->arrayToParams($getParams);
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public static function getInstance() {
|
||||
if(static::$instance === null) {
|
||||
static::$instance = new static();
|
||||
if(self::$instance === null) {
|
||||
self::$instance = new static();
|
||||
}
|
||||
return static::$instance;
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public static function reset() {
|
||||
self::$instance = null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -310,7 +310,7 @@ abstract class RouterEntry {
|
||||
if($max) {
|
||||
for($i = 0; $i < $max; $i++) {
|
||||
$name = $parameterNames[$i];
|
||||
$parameterValue = isset($parameterValues[$name['name']]) ? $parameterValues[$name['name']] : null;
|
||||
$parameterValue = (isset($parameterValues[$name['name']]) && !empty($parameterValues[$name['name']])) ? $parameterValues[$name['name']] : null;
|
||||
|
||||
if($name['required'] && $parameterValue === null) {
|
||||
throw new RouterException('Missing required parameter ' . $name['name'], 404);
|
||||
|
||||
@@ -93,23 +93,9 @@ 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,8 +11,4 @@ class DummyController {
|
||||
echo 'Params: ' . join(', ', $params);
|
||||
}
|
||||
|
||||
public function notFound() {
|
||||
echo 'not found';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
<?php
|
||||
class ExceptionHandler implements \Pecee\Handler\IExceptionHandler {
|
||||
|
||||
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\RouterEntry $router = null, \Exception $error){
|
||||
throw $error;
|
||||
}
|
||||
|
||||
}
|
||||
+10
-2
@@ -7,11 +7,19 @@ 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;
|
||||
|
||||
@@ -27,9 +35,9 @@ class GroupTest extends PHPUnit_Framework_TestCase {
|
||||
}
|
||||
|
||||
public function testNestedGroup() {
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/api/v1/test');
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setMethod('get');
|
||||
\Pecee\Http\Request::getInstance()->setUri('/api/v1/test');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/api'], function() {
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/v1'], function() {
|
||||
|
||||
+11
-6
@@ -2,24 +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\SimpleRouter::group(['exceptionHandler' => 'ExceptionHandler'], function() {
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start', ['middleware' => 'DummyMiddleware']);
|
||||
});
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start', ['middleware' => 'DummyMiddleware']);
|
||||
|
||||
$found = false;
|
||||
|
||||
try {
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
}catch(\Exception $e) {
|
||||
}catch(Exception $e) {
|
||||
$found = ($e instanceof MiddlewareLoadedException);
|
||||
}
|
||||
|
||||
|
||||
+23
-27
@@ -2,68 +2,58 @@
|
||||
|
||||
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::getInstance()->getRequest()->setUri('/my/test/url');
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setMethod('get');
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\Http\Request::getInstance()->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::getInstance()->getRequest()->setUri('/my/test/url');
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\Http\Request::getInstance()->setMethod('post');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start');
|
||||
@@ -78,6 +68,8 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testSimpleParam() {
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||
\Pecee\Http\Request::getInstance()->setUri('/test-param1');
|
||||
|
||||
@@ -88,6 +80,8 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testMultiParam() {
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||
\Pecee\Http\Request::getInstance()->setUri('/test-param1-param2');
|
||||
|
||||
@@ -98,6 +92,8 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testPathParam() {
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||
\Pecee\Http\Request::getInstance()->setUri('/test/path/param1');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user