From bc67038e11acc8a31421ce2bb5a1d8c8b177298c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Sat, 19 Aug 2017 13:48:47 +0100 Subject: [PATCH 01/10] Added helpers.php file - Added updated helpers.php file - Updated helpers section in documentation. --- README.md | 40 ++++++++++++++++++++------------ helpers.php | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 14 deletions(-) create mode 100644 helpers.php diff --git a/README.md b/README.md index ef10ef5..46155f7 100644 --- a/README.md +++ b/README.md @@ -204,9 +204,9 @@ SimpleRouter::start(); ### Helper functions -We recommend that you add these helper functions to your project. Theese will allow you to access functionality of the router more easily. +We recommend that you add these helper functions to your project. These will allow you to access functionality of the router more easily. -To implement the functions below, simply copy the code to a new file and require the file before initializing the router. +To implement the functions below, simply copy the code to a new file and require the file before initializing the router or copy the `helpers.php` we've included in this library. ```php getInput()->get($index, $defaultValue, $methods); + } + return request()->getInput(); } @@ -395,12 +403,12 @@ SimpleRouter::all('/ajax/abc/123', function($param1, $param2) { ### Custom regex for matching parameters -By default simple-php-router uses the `\w` regular expression when matching parameters. +By default simple-php-router uses the `\w` regular expression when matching parameters. This decision was made with speed and reliability in mind, as this match will match both letters, number and most of the used symbols on the internet. -However, sometimes it can be necessary to add a custom regular expression to match more advanced characters like `-` etc. +However, sometimes it can be necessary to add a custom regular expression to match more advanced characters like `-` etc. -Instead of adding a custom regular expression to all your parameters, you can simply add a global regular expression which will be used on all the parameters on the route. +Instead of adding a custom regular expression to all your parameters, you can simply add a global regular expression which will be used on all the parameters on the route. **Note:** If you the regular expression to be available across, we recommend using the global parameter on a group as demonstrated in the examples below. @@ -663,11 +671,11 @@ ExceptionHandler are classes that handles all exceptions. ExceptionsHandlers mus ## Handling 404, 403 and other errors -If you simply want to catch a 404 (page not found) etc. you can use the `Router::error($callback)` static helper method. +If you simply want to catch a 404 (page not found) etc. you can use the `Router::error($callback)` static helper method. This will add a callback method which is fired whenever an error occurs on all routes. -The basic example below simply redirect the page to `/not-found` if an `NotFoundHttpException` (404) occurred. +The basic example below simply redirect the page to `/not-found` if an `NotFoundHttpException` (404) occurred. The code should be placed in the file that contains your routes. ```php @@ -825,7 +833,7 @@ If items is grouped in the html, it will return an array of items. **Note:** `get` will automatically trim the value and ensure that it's not empty. If it's empty the `$defaultValue` will be returned. ```php -$value = input()->get($index, $defaultValue, $methods); +$value = input($index, $defaultValue, $methods); ``` ### Get parameter object @@ -834,7 +842,7 @@ Will return an instance of `InputItem` or `InputFile` depending on the type. You can use this in your html as it will render the value of the item. However if you want to compare value in your if statements, you have to use -the `getValue` or use the `input()->get()` instead. +the `getValue` or use the `input()` instead. If items is grouped in the html, it will return an array of items. @@ -855,7 +863,11 @@ $object = input()->getObject($index, $defaultValue = null, $methods = null); * $defaultValue is returned if the value is empty. */ -$id = input()->get($index, $defaultValue); +$id = input()->get($index, $defaultValue, $method); + +# -- shortcut to above -- + +$id = input($index, $defaultValue, $method); # -- match specific -- @@ -880,7 +892,7 @@ $object = input()->findFile($index, $defaultValue); */ /* @var $image \Pecee\Http\Input\InputFile */ -foreach(input()->get('images', []) as $image) +foreach(input('images', []) as $image) { if($image->getMime() === 'image/jpeg') { @@ -926,7 +938,7 @@ Below example requires you to have the helper functions added. Please refer to t ```php /* Get parameter site_id or default-value 2 from either post-value or query-string */ -$siteId = input()->get('site_id', 2, ['post', 'get']); +$siteId = input('site_id', 2, ['post', 'get']); ``` --- diff --git a/helpers.php b/helpers.php new file mode 100644 index 0000000..d0e6e04 --- /dev/null +++ b/helpers.php @@ -0,0 +1,66 @@ +getInput()->get($index, $defaultValue, $methods); + } + + return request()->getInput(); +} + +function redirect($url, $code = null) +{ + if ($code !== null) { + response()->httpCode($code); + } + + response()->redirect($url); +} \ No newline at end of file From 7e8cb91f68c229c06cb224cd556f8a7585db8237 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Sat, 19 Aug 2017 16:21:55 +0100 Subject: [PATCH 02/10] Updated documentation --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 46155f7..30d62da 100644 --- a/README.md +++ b/README.md @@ -846,7 +846,7 @@ the `getValue` or use the `input()` instead. If items is grouped in the html, it will return an array of items. -**Note:** `getObject` will only return `$defaultValue` if the item doesn't exist. If you want `$defaultValue` to be returned if the item is empty, please use `input()->get()` instead. +**Note:** `getObject` will only return `$defaultValue` if the item doesn't exist. If you want `$defaultValue` to be returned if the item is empty, please use `input()` instead. ```php $object = input()->getObject($index, $defaultValue = null, $methods = null); @@ -871,9 +871,9 @@ $id = input($index, $defaultValue, $method); # -- match specific -- -$object = input()->get($index, $defaultValue, 'get'); -$object = input()->get($index, $defaultValue, 'post'); -$object = input()->get($index, $defaultValue, 'file'); +$object = input($index, $defaultValue, 'get'); +$object = input($index, $defaultValue, 'post'); +$object = input($index, $defaultValue, 'file'); # -- or -- From 8720e732e1a0183be6d7d69fa118239feadd9919 Mon Sep 17 00:00:00 2001 From: zaingithub Date: Sun, 20 Aug 2017 12:47:27 +0700 Subject: [PATCH 03/10] Update README.md Update section : ## Handling 404, 403 and other errors $exception->getCode to $exception->getCode() --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ef10ef5..a79b50f 100644 --- a/README.md +++ b/README.md @@ -674,7 +674,7 @@ The code should be placed in the file that contains your routes. Router::get('/not-found', 'PageController@notFound'); Router::error(function(Request $request, \Exception $exception) { - if($exception instanceof NotFoundHttpException && $exception->getCode == 404) { + if($exception instanceof NotFoundHttpException && $exception->getCode() == 404) { response()->redirect('/not-found'); } }); From 4cd1e8e069f37c7050fe19681660c7356d4de1ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 22 Aug 2017 17:18:48 +0100 Subject: [PATCH 04/10] Fixed: return / if all parameters are empty in `$router->getUrl` method. --- src/Pecee/SimpleRouter/Router.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index 9090d6a..621eebe 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -248,6 +248,7 @@ class Router if ($rewriteRoute !== null) { $rewriteRoute->loadMiddleware($this->request); + return $rewriteRoute->renderRoute($this->request); } @@ -265,6 +266,7 @@ class Router /* Render route */ $routeNotAllowed = false; $this->request->setLoadedRoute($route); + return $route->renderRoute($this->request); break; @@ -323,6 +325,7 @@ class Router if ($rewriteRoute !== null) { $rewriteRoute->loadMiddleware($this->request); + return $rewriteRoute->renderRoute($this->request); } @@ -434,6 +437,10 @@ class Router throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null'); } + if ($name === '' && $parameters === '') { + return '/'; + } + /* Only merge $_GET when all parameters are null */ if ($name === null && $parameters === null && $getParams === null) { $getParams = $_GET; From 7c970c442c9ac9b093fcc7450f864f82947f43ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Wed, 23 Aug 2017 16:10:15 +0100 Subject: [PATCH 05/10] Added Uri class which can be used to parse urls. --- src/Pecee/Http/Uri.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/Pecee/Http/Uri.php diff --git a/src/Pecee/Http/Uri.php b/src/Pecee/Http/Uri.php new file mode 100644 index 0000000..19a0713 --- /dev/null +++ b/src/Pecee/Http/Uri.php @@ -0,0 +1,13 @@ + Date: Wed, 23 Aug 2017 16:13:44 +0100 Subject: [PATCH 06/10] Uri is now always urldecoded in Request class to ensure compability with most webservers (issue: #268). --- src/Pecee/Http/Request.php | 2 +- src/Pecee/SimpleRouter/Route/RouteController.php | 3 +-- src/Pecee/SimpleRouter/Route/RouteResource.php | 3 +-- src/Pecee/SimpleRouter/Route/RouteUrl.php | 3 +-- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Pecee/Http/Request.php b/src/Pecee/Http/Request.php index 0d69a71..ecee552 100644 --- a/src/Pecee/Http/Request.php +++ b/src/Pecee/Http/Request.php @@ -30,7 +30,7 @@ class Request { $this->parseHeaders(); $this->host = $this->getHeader('http-host'); - $this->uri = $this->getHeader('request-uri'); + $this->uri = urldecode($this->getHeader('request-uri')); $this->input = new Input($this); $this->method = strtolower($this->input->get('_method', $this->getHeader('request-method'), 'post')); } diff --git a/src/Pecee/SimpleRouter/Route/RouteController.php b/src/Pecee/SimpleRouter/Route/RouteController.php index b32fa23..d1650d6 100644 --- a/src/Pecee/SimpleRouter/Route/RouteController.php +++ b/src/Pecee/SimpleRouter/Route/RouteController.php @@ -87,8 +87,7 @@ class RouteController extends LoadableRoute implements IControllerRoute public function matchRoute($url, Request $request) { - $url = parse_url(urldecode($url), PHP_URL_PATH); - $url = rtrim($url, '/') . '/'; + $url = rtrim(parse_url($url, PHP_URL_PATH), '/') . '/'; /* Match global regular-expression for route */ $regexMatch = $this->matchRegex($request, $url); diff --git a/src/Pecee/SimpleRouter/Route/RouteResource.php b/src/Pecee/SimpleRouter/Route/RouteResource.php index 7e806ca..20f0752 100644 --- a/src/Pecee/SimpleRouter/Route/RouteResource.php +++ b/src/Pecee/SimpleRouter/Route/RouteResource.php @@ -79,8 +79,7 @@ class RouteResource extends LoadableRoute implements IControllerRoute public function matchRoute($url, Request $request) { - $url = parse_url(urldecode($url), PHP_URL_PATH); - $url = rtrim($url, '/') . '/'; + $url = rtrim(parse_url($url, PHP_URL_PATH), '/') . '/'; /* Match global regular-expression for route */ $regexMatch = $this->matchRegex($request, $url); diff --git a/src/Pecee/SimpleRouter/Route/RouteUrl.php b/src/Pecee/SimpleRouter/Route/RouteUrl.php index 1c348aa..68ca0b7 100644 --- a/src/Pecee/SimpleRouter/Route/RouteUrl.php +++ b/src/Pecee/SimpleRouter/Route/RouteUrl.php @@ -14,8 +14,7 @@ class RouteUrl extends LoadableRoute public function matchRoute($url, Request $request) { - $url = parse_url(urldecode($url), PHP_URL_PATH); - $url = rtrim($url, '/') . '/'; + $url = rtrim(parse_url($url, PHP_URL_PATH), '/') . '/'; /* Match global regular-expression for route */ $regexMatch = $this->matchRegex($request, $url); From 6fb92d7cae190f4a1c95b98e6972cae1d53f88e7 Mon Sep 17 00:00:00 2001 From: Juan Antonio Tubio Date: Wed, 23 Aug 2017 17:28:14 +0200 Subject: [PATCH 07/10] Add IIS web.config file example --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/README.md b/README.md index ef10ef5..125180b 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ If you want a great new feature or experience any issues what-so-ever, please fe - [Installation](#installation) - [Setting up Apache](#setting-up-apache) - [Setting up Nginx](#setting-up-nginx) + - [Setting up IIS](#setting-up-iis) - [Configuration](#configuration) - [Helper functions](#helper-functions) @@ -172,6 +173,45 @@ RewriteCond %{SCRIPT_FILENAME} !-l RewriteRule ^(.*)$ index.php/$1 ``` +### Setting up IIS + +On IIS you have to add some lines your `web.config` file in the `public` folder or create a new one. If rewriting is not working for you, please check that your IIS version have included the `url rewrite` module or download and install them from Microsoft web site. + +#### web.config example + +Below is an example of an working `web.config` file used by simple-php-router. + +Simply create a new `web.config` file in your projects `public` directory and paste the contents below in your newly created file. This will redirect all requests to your `index.php` file (see Configuration section below). If the xxx file already exists, add the `` section inside the `` branch. + +``` + + + + + + + + + + + + + + + + + + + + + + + + + + +``` + ### 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. From acf37c4023bd0257a2d1c6130ccde22105bb5ad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Wed, 23 Aug 2017 20:41:49 +0100 Subject: [PATCH 08/10] Updated Uri class. --- src/Pecee/Http/Uri.php | 162 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 157 insertions(+), 5 deletions(-) diff --git a/src/Pecee/Http/Uri.php b/src/Pecee/Http/Uri.php index 19a0713..3c5623d 100644 --- a/src/Pecee/Http/Uri.php +++ b/src/Pecee/Http/Uri.php @@ -1,13 +1,165 @@ originalUrl = $url; + $this->data = array_merge($this->data, $this->parseUrl($url)); + } + + /** + * Check if url is using a secure protocol like https + * @return bool + */ + public function isSecure() + { + return (strtolower($this->getScheme()) === 'https'); + } + + /** + * Checks if url is relative + * @return bool + */ + public function isRelative() + { + return ($this->getHost() === null); + } + + /** + * Get url scheme + * @return string|null + */ + public function getScheme() + { + return $this->data['scheme']; + } + + /** + * Get url host + * @return string|null + */ + public function getHost() + { + return $this->data['host']; + } + + /** + * Get url port + * @return int|null + */ + public function getPort() + { + return ($this->data['port'] !== null) ? (int)$this->data['port'] : null; + } + + /** + * Parse username from url + * @return string|null + */ + public function getUserName() + { + return $this->data['user']; + } + + /** + * Parse password from url + * @return string|null + */ + public function getPassword() + { + return $this->data['pass']; + } + + /** + * Get path from url + * @return string + */ + public function getPath() + { + return $this->data['path']; + } + + /** + * Get querystring from url + * @return string|null + */ + public function getQueryString() + { + return $this->data['query']; + } + + /** + * Get fragment from url (everything after #) + * @return string|null + */ + public function getFragment() + { + return $this->data['fragment']; + } + + /** + * @return string + */ + public function getOriginalUrl() + { + return $this->originalUrl; + } + + /** + * UTF-8 aware parse_url() replacement. + * @throws \InvalidArgumentException + * @return array + */ + public function parseUrl($url) + { + $enc_url = preg_replace_callback( + '%[^:/@?&=#]+%u', + function ($matches) { + return urlencode($matches[0]); + }, + $url + ); + + $parts = parse_url($enc_url); + + if ($parts === false) { + throw new \InvalidArgumentException('Malformed URL: ' . $url); + } + + foreach ((array)$parts as $name => $value) { + $parts[$name] = urldecode($value); + } + + return $parts; + } + + /** + * Returns data array with information about the url + * @return array + */ + public function getData() + { + return $this->data; + } + + public function __toString() + { + return $this->getOriginalUrl(); } } \ No newline at end of file From 65c811356d51e37f032b1db8827f1ed8bc535aca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Wed, 23 Aug 2017 21:04:11 +0100 Subject: [PATCH 09/10] Fixed url parsing for unicode characters. --- README.md | 4 ++-- src/Pecee/Http/Middleware/BaseCsrfVerifier.php | 4 ++-- src/Pecee/Http/Request.php | 10 +++++----- src/Pecee/Http/Response.php | 2 +- src/Pecee/Http/Uri.php | 13 ++++++++++--- .../SimpleRouter/Route/RouteController.php | 2 -- src/Pecee/SimpleRouter/Route/RouteResource.php | 2 -- src/Pecee/SimpleRouter/Route/RouteUrl.php | 2 -- src/Pecee/SimpleRouter/Router.php | 17 +++++++++-------- test/Dummy/Handler/ExceptionHandlerFirst.php | 2 +- test/Dummy/Handler/ExceptionHandlerSecond.php | 2 +- test/Helpers/TestRouter.php | 2 +- test/RouterRewriteTest.php | 2 +- test/RouterUrlTest.php | 9 ++++++++- 14 files changed, 41 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 30d62da..5cdd7f5 100644 --- a/README.md +++ b/README.md @@ -706,7 +706,7 @@ class CustomExceptionHandler implements IExceptionHandler /* You can use the exception handler to format errors depending on the request and type. */ - if (stripos($request->getUri(), '/api') !== false) { + if (stripos($request->getUri()->getPath(), '/api') !== false) { response()->json([ 'error' => $error->getMessage(), @@ -1091,7 +1091,7 @@ class CustomRouterRules implement IRouterBootManager { // If the current uri matches the url, we use our custom route - if($request->getUri() === $url) { + if($request->getUri()->getPath() === $url) { $request->setRewriteUrl($rule); return $request; } diff --git a/src/Pecee/Http/Middleware/BaseCsrfVerifier.php b/src/Pecee/Http/Middleware/BaseCsrfVerifier.php index 056d23f..5d2b4cf 100644 --- a/src/Pecee/Http/Middleware/BaseCsrfVerifier.php +++ b/src/Pecee/Http/Middleware/BaseCsrfVerifier.php @@ -41,9 +41,9 @@ class BaseCsrfVerifier implements IMiddleware $url = rtrim($url, '/'); if ($url[strlen($url) - 1] === '*') { $url = rtrim($url, '*'); - $skip = (stripos($request->getUri(), $url) === 0); + $skip = (stripos($request->getUri()->getPath(), $url) === 0); } else { - $skip = ($url === rtrim($request->getUri(), '/')); + $skip = ($url === $request->getUri()->getPath()); } if ($skip === true) { diff --git a/src/Pecee/Http/Request.php b/src/Pecee/Http/Request.php index ecee552..91cb994 100644 --- a/src/Pecee/Http/Request.php +++ b/src/Pecee/Http/Request.php @@ -29,8 +29,8 @@ class Request public function __construct() { $this->parseHeaders(); - $this->host = $this->getHeader('http-host'); - $this->uri = urldecode($this->getHeader('request-uri')); + $this->setHost($this->getHeader('http-host')); + $this->setUri(new Uri($this->getHeader('request-uri'))); $this->input = new Input($this); $this->method = strtolower($this->input->get('_method', $this->getHeader('request-method'), 'post')); } @@ -58,7 +58,7 @@ class Request } /** - * @return string + * @return Uri */ public function getUri() { @@ -215,9 +215,9 @@ class Request } /** - * @param string $uri + * @param Uri $uri */ - public function setUri($uri) + public function setUri(Uri $uri) { $this->uri = $uri; } diff --git a/src/Pecee/Http/Response.php b/src/Pecee/Http/Response.php index c5730f1..0c8625a 100644 --- a/src/Pecee/Http/Response.php +++ b/src/Pecee/Http/Response.php @@ -41,7 +41,7 @@ class Response public function refresh() { - $this->redirect($this->request->getUri()); + $this->redirect($this->request->getUri()->getPath()); } /** diff --git a/src/Pecee/Http/Uri.php b/src/Pecee/Http/Uri.php index 3c5623d..1232219 100644 --- a/src/Pecee/Http/Uri.php +++ b/src/Pecee/Http/Uri.php @@ -20,6 +20,11 @@ class Uri { $this->originalUrl = $url; $this->data = array_merge($this->data, $this->parseUrl($url)); + + if (isset($this->data['path']) === true && $this->data['path'] !== '/') { + $this->data['path'] = rtrim($this->data['path'], '/') . '/'; + } + } /** @@ -122,12 +127,14 @@ class Uri /** * UTF-8 aware parse_url() replacement. + * @param string $url + * @param int $component * @throws \InvalidArgumentException * @return array */ - public function parseUrl($url) + public function parseUrl($url, $component = -1) { - $enc_url = preg_replace_callback( + $encodedUrl = preg_replace_callback( '%[^:/@?&=#]+%u', function ($matches) { return urlencode($matches[0]); @@ -135,7 +142,7 @@ class Uri $url ); - $parts = parse_url($enc_url); + $parts = parse_url($encodedUrl, $component); if ($parts === false) { throw new \InvalidArgumentException('Malformed URL: ' . $url); diff --git a/src/Pecee/SimpleRouter/Route/RouteController.php b/src/Pecee/SimpleRouter/Route/RouteController.php index d1650d6..bafa324 100644 --- a/src/Pecee/SimpleRouter/Route/RouteController.php +++ b/src/Pecee/SimpleRouter/Route/RouteController.php @@ -87,8 +87,6 @@ class RouteController extends LoadableRoute implements IControllerRoute public function matchRoute($url, Request $request) { - $url = rtrim(parse_url($url, PHP_URL_PATH), '/') . '/'; - /* Match global regular-expression for route */ $regexMatch = $this->matchRegex($request, $url); diff --git a/src/Pecee/SimpleRouter/Route/RouteResource.php b/src/Pecee/SimpleRouter/Route/RouteResource.php index 20f0752..c042b23 100644 --- a/src/Pecee/SimpleRouter/Route/RouteResource.php +++ b/src/Pecee/SimpleRouter/Route/RouteResource.php @@ -79,8 +79,6 @@ class RouteResource extends LoadableRoute implements IControllerRoute public function matchRoute($url, Request $request) { - $url = rtrim(parse_url($url, PHP_URL_PATH), '/') . '/'; - /* Match global regular-expression for route */ $regexMatch = $this->matchRegex($request, $url); diff --git a/src/Pecee/SimpleRouter/Route/RouteUrl.php b/src/Pecee/SimpleRouter/Route/RouteUrl.php index 68ca0b7..6c37e55 100644 --- a/src/Pecee/SimpleRouter/Route/RouteUrl.php +++ b/src/Pecee/SimpleRouter/Route/RouteUrl.php @@ -14,8 +14,6 @@ class RouteUrl extends LoadableRoute public function matchRoute($url, Request $request) { - $url = rtrim(parse_url($url, PHP_URL_PATH), '/') . '/'; - /* Match global regular-expression for route */ $regexMatch = $this->matchRegex($request, $url); diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index 9090d6a..b646184 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -122,7 +122,7 @@ class Router $exceptionHandlers = []; - $url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri(); + $url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri()->getPath(); /* @var $route IRoute */ for ($i = $max; $i >= 0; $i--) { @@ -224,7 +224,7 @@ class Router } } - $url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri(); + $url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri()->getPath(); $max = count($this->processedRoutes) - 1; @@ -248,6 +248,7 @@ class Router if ($rewriteRoute !== null) { $rewriteRoute->loadMiddleware($this->request); + return $rewriteRoute->renderRoute($this->request); } @@ -265,6 +266,7 @@ class Router /* Render route */ $routeNotAllowed = false; $this->request->setLoadedRoute($route); + return $route->renderRoute($this->request); break; @@ -284,9 +286,9 @@ class Router $rewriteUrl = $this->request->getRewriteUrl(); if ($rewriteUrl !== null) { - $message = sprintf('Route not found: "%s" (rewrite from: "%s")', $rewriteUrl, $this->request->getUri()); + $message = sprintf('Route not found: "%s" (rewrite from: "%s")', $rewriteUrl, $this->request->getUri()->getPath()); } else { - $message = sprintf('Route not found: "%s"', $this->request->getUri()); + $message = sprintf('Route not found: "%s"', $this->request->getUri()->getPath()); } $this->handleException(new NotFoundHttpException($message, 404)); @@ -300,7 +302,7 @@ class Router */ protected function handleException(\Exception $e) { - $url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri(); + $url = ($this->request->getRewriteUrl() !== null) ? $this->request->getRewriteUrl() : $this->request->getUri()->getPath(); $max = count($this->exceptionHandlers); @@ -323,6 +325,7 @@ class Router if ($rewriteRoute !== null) { $rewriteRoute->loadMiddleware($this->request); + return $rewriteRoute->renderRoute($this->request); } @@ -443,9 +446,7 @@ class Router /* Return current route if no options has been specified */ if ($name === null && $parameters === null) { - $url = rtrim(parse_url($this->request->getUri(), PHP_URL_PATH), '/'); - - return (($url === '') ? '/' : $url . '/') . $this->arrayToParams($getParams); + return $this->request->getUri()->getPath() . $this->arrayToParams($getParams); } $loadedRoute = $this->request->getLoadedRoute(); diff --git a/test/Dummy/Handler/ExceptionHandlerFirst.php b/test/Dummy/Handler/ExceptionHandlerFirst.php index 87e4761..db752e7 100644 --- a/test/Dummy/Handler/ExceptionHandlerFirst.php +++ b/test/Dummy/Handler/ExceptionHandlerFirst.php @@ -7,7 +7,7 @@ class ExceptionHandlerFirst implements \Pecee\Handlers\IExceptionHandler global $stack; $stack[] = static::class; - $request->setUri('/'); + $request->setUri(new \Pecee\Http\Uri('/')); return $request; } diff --git a/test/Dummy/Handler/ExceptionHandlerSecond.php b/test/Dummy/Handler/ExceptionHandlerSecond.php index 90c18ae..86a11d1 100644 --- a/test/Dummy/Handler/ExceptionHandlerSecond.php +++ b/test/Dummy/Handler/ExceptionHandlerSecond.php @@ -7,7 +7,7 @@ class ExceptionHandlerSecond implements \Pecee\Handlers\IExceptionHandler global $stack; $stack[] = static::class; - $request->setUri('/'); + $request->setUri(new \Pecee\Http\Uri('/')); return $request; } diff --git a/test/Helpers/TestRouter.php b/test/Helpers/TestRouter.php index 5db693b..8347999 100644 --- a/test/Helpers/TestRouter.php +++ b/test/Helpers/TestRouter.php @@ -5,7 +5,7 @@ class TestRouter extends \Pecee\SimpleRouter\SimpleRouter public static function debugNoReset($testUri, $testMethod = 'get') { - static::request()->setUri($testUri); + static::request()->setUri(new \Pecee\Http\Uri($testUri)); static::request()->setMethod($testMethod); static::start(); diff --git a/test/RouterRewriteTest.php b/test/RouterRewriteTest.php index e72bce6..d55aaa8 100644 --- a/test/RouterRewriteTest.php +++ b/test/RouterRewriteTest.php @@ -64,7 +64,7 @@ class RouteRewriteTest extends PHPUnit_Framework_TestCase TestRouter::error(function (\Pecee\Http\Request $request, \Exception $error) { - if (strtolower($request->getUri()) == '/my/test') { + if (strtolower($request->getUri()->getPath()) == '/my/test') { $request->setRewriteUrl('/another-non-existing'); return $request; diff --git a/test/RouterUrlTest.php b/test/RouterUrlTest.php index 4ea583c..f669632 100644 --- a/test/RouterUrlTest.php +++ b/test/RouterUrlTest.php @@ -31,7 +31,7 @@ class RouterUrlTest extends PHPUnit_Framework_TestCase public function testUnicodeCharacters() { // Test spanish characters - TestRouter::get('/cursos/listado/{listado?}/{category?}', 'DummyController@method1', ['defaultParameterRegex' => '[\w\p{L}\s-]+']); + TestRouter::get('/cursos/listado/{listado?}/{category?}', 'DummyController@method1', ['defaultParameterRegex' => '[\w\p{L}\s-\í]+']); TestRouter::debugNoReset('/cursos/listado/especialidad/cirugía local', 'get'); $this->assertEquals('/cursos/listado/{listado?}/{category?}/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl()); @@ -40,6 +40,13 @@ class RouterUrlTest extends PHPUnit_Framework_TestCase TestRouter::debugNoReset('/kategori/økse', 'get'); $this->assertEquals('/kategori/økse/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl()); + TestRouter::get('/test/{param}', 'DummyController@method1', ['defaultParameterRegex' => '[\w\p{L}\s-\í]+']); + TestRouter::debugNoReset('/test/Dermatología'); + + $parameters = TestRouter::request()->getLoadedRoute()->getParameters(); + + $this->assertEquals('Dermatología', $parameters['param']); + TestRouter::router()->reset(); } From c7473938c5d3758012f73cba2bc333b4ac782426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Wed, 23 Aug 2017 22:09:55 +0200 Subject: [PATCH 10/10] Changed xxx to web.config --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 125180b..c620f81 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,7 @@ On IIS you have to add some lines your `web.config` file in the `public` folder Below is an example of an working `web.config` file used by simple-php-router. -Simply create a new `web.config` file in your projects `public` directory and paste the contents below in your newly created file. This will redirect all requests to your `index.php` file (see Configuration section below). If the xxx file already exists, add the `` section inside the `` branch. +Simply create a new `web.config` file in your projects `public` directory and paste the contents below in your newly created file. This will redirect all requests to your `index.php` file (see Configuration section below). If the `web.config` file already exists, add the `` section inside the `` branch. ```