mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 16:57:53 +00:00
Compare commits
189 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cfa18e520a | |||
| 5e448f0835 | |||
| 98ad310404 | |||
| a5aac57ce9 | |||
| 7f924c7d0a | |||
| 3a90578351 | |||
| 52e0f5ef94 | |||
| 0090c167bb | |||
| ae68598024 | |||
| 7362b748af | |||
| 4fe85d6568 | |||
| c82d91375c | |||
| e774122b1e | |||
| e8aceb291c | |||
| 957a382248 | |||
| 48e2e3f9bc | |||
| f95e12c49c | |||
| 9f509ac818 | |||
| 3b4d23e9ae | |||
| b3e99a2283 | |||
| c1aa5782a1 | |||
| 387d9710d8 | |||
| fd098266f7 | |||
| c7473938c5 | |||
| 65c811356d | |||
| ea255baec3 | |||
| acf37c4023 | |||
| 6fb92d7cae | |||
| e84c8c2f02 | |||
| 7c970c442c | |||
| 4cd1e8e069 | |||
| 8720e732e1 | |||
| 7e8cb91f68 | |||
| bc67038e11 | |||
| e31d8af2f7 | |||
| 9f285fd0ce | |||
| b7f5d31544 | |||
| 5086347802 | |||
| 26f89213a2 | |||
| bb7991afdc | |||
| 796d01bc25 | |||
| 74187ee326 | |||
| f090d6c038 | |||
| 6e859e11ab | |||
| 6aa38cfa4c | |||
| 62f0075cf3 | |||
| 6890b60737 | |||
| 1e07140865 | |||
| bc03490100 | |||
| 2c61cef7ad | |||
| 3f9ab73f7f | |||
| e44d6bc30b | |||
| 3f61c90749 | |||
| e4e632dca9 | |||
| a5c4a1f721 | |||
| 0bc7fa7bd5 | |||
| eb036ded3b | |||
| debe080181 | |||
| dd99e1d488 | |||
| 1c07311c5d | |||
| 801c84eb05 | |||
| ed98519152 | |||
| 55d031e152 | |||
| 8509062e00 | |||
| 4b4ab906fb | |||
| 9617cacc31 | |||
| 54a824ce44 | |||
| 99088719ed | |||
| 2062ff4189 | |||
| 3e41ee28b6 | |||
| 13a5f40cd0 | |||
| 751b4444ae | |||
| 351f7a01e8 | |||
| 8cd42a2c4b | |||
| 1496878ae9 | |||
| e8e1471bab | |||
| b3f2e5f812 | |||
| 4de1498723 | |||
| a60fa9f919 | |||
| ea243f2c89 | |||
| ce6f34f4d4 | |||
| 56653568e4 | |||
| 2868ffe023 | |||
| 5d330643e7 | |||
| 8ab52364da | |||
| 57aa8eac1e | |||
| 110bc2afcf | |||
| 4efc72d013 | |||
| a92a4cdf0d | |||
| 49fc991f9a | |||
| 0a4c78cf64 | |||
| 20353c6e4d | |||
| 6706d86784 | |||
| 4c62f86a26 | |||
| 20c1e0ef69 | |||
| 6445746324 | |||
| 1a2921acb4 | |||
| fe560a9ba5 | |||
| 258d9d05c7 | |||
| c522801c28 | |||
| 7e7319de06 | |||
| 9d275d6d3b | |||
| 531b35532b | |||
| e6dd9f3f55 | |||
| c258f937e8 | |||
| e803b1ca5c | |||
| 88d58cd7b7 | |||
| 45faf9830e | |||
| d04c74ccad | |||
| c915c0386a | |||
| 02809a4daf | |||
| 30479b15ca | |||
| da6b5af19f | |||
| 4dde51e833 | |||
| 914ec9d1b7 | |||
| 961a4d0e94 | |||
| 5f72755a98 | |||
| 2216090a5f | |||
| 41d15d3acd | |||
| d467f60e55 | |||
| ded9c8ebe0 | |||
| c30ae6b098 | |||
| 394f7beb8b | |||
| c93308c8e1 | |||
| baab004482 | |||
| c3241012af | |||
| 00c0cad211 | |||
| 9e2ba2674b | |||
| c6e85676da | |||
| 4d18f33a81 | |||
| 5a74c9d27d | |||
| 9d98b2da4c | |||
| b5eef6f3ee | |||
| 6930864e7e | |||
| ad0eceb814 | |||
| f3f129ae0b | |||
| 4bec2bc5fb | |||
| 85b9fac21e | |||
| 4f47463497 | |||
| 0c93633d13 | |||
| e5c86c1822 | |||
| 973344e46e | |||
| 32c305bd2c | |||
| 4a03005c68 | |||
| 52034411cf | |||
| 4c8ed5bb3d | |||
| 9fed6ffb3f | |||
| 15da599e82 | |||
| 9274acb591 | |||
| 5c7759ab72 | |||
| c7b8593185 | |||
| fb478f475c | |||
| 1142d9d4ce | |||
| 5d5c96e802 | |||
| 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 |
@@ -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)
|
||||
|
||||
@@ -42,6 +43,7 @@ If you want a great new feature or experience any issues what-so-ever, please fe
|
||||
- [Namespaces](#namespaces)
|
||||
- [Subdomain-routing](#subdomain-routing)
|
||||
- [Route prefixes](#route-prefixes)
|
||||
- [Partial groups](#partial-groups)
|
||||
- [Form Method Spoofing](#form-method-spoofing)
|
||||
- [Accessing The Current Route](#accessing-the-current-route)
|
||||
- [Other examples](#other-examples)
|
||||
@@ -78,7 +80,6 @@ If you want a great new feature or experience any issues what-so-ever, please fe
|
||||
- [Bootmanager: loading routes dynamically](#bootmanager-loading-routes-dynamically)
|
||||
- [Adding routes manually](#adding-routes-manually)
|
||||
- [Parameters](#parameters)
|
||||
- [Custom default regex for matching parameters](#custom-default-regex-for-matching-parameters)
|
||||
- [Extending](#extending)
|
||||
|
||||
- [Credits](#credits)
|
||||
@@ -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 `web.config` file already exists, add the `<rewrite>` section inside the `<system.webServer>` branch.
|
||||
|
||||
```
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<system.webServer>
|
||||
<rewrite>
|
||||
<rules>
|
||||
<!-- Remove slash '/' from the en of the url -->
|
||||
<rule name="RewriteRequestsToPublic">
|
||||
<match url="^(.*)$" />
|
||||
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
|
||||
</conditions>
|
||||
<action type="Rewrite" url="/{R:0}" />
|
||||
</rule>
|
||||
|
||||
<!-- When requested file or folder don't exists, will request again through index.php -->
|
||||
<rule name="Imported Rule 1" stopProcessing="true">
|
||||
<match url="^(.*)$" ignoreCase="true" />
|
||||
<conditions logicalGrouping="MatchAll">
|
||||
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
|
||||
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
|
||||
</conditions>
|
||||
<action type="Rewrite" url="/index.php/{R:1}" appendQueryString="true" />
|
||||
</rule>
|
||||
</rules>
|
||||
</rewrite>
|
||||
</system.webServer>
|
||||
</configuration>
|
||||
```
|
||||
|
||||
### 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.
|
||||
@@ -204,9 +244,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
|
||||
<?php
|
||||
@@ -250,10 +290,18 @@ function request()
|
||||
|
||||
/**
|
||||
* Get input class
|
||||
* @return \Pecee\Http\Input\Input
|
||||
* @param string|null $index Parameter index name
|
||||
* @param string|null $defaultValue Default return value
|
||||
* @param string|array|null $methods Default method
|
||||
* @return \Pecee\Http\Input\Input|string
|
||||
*/
|
||||
function input()
|
||||
function input($index = null, $defaultValue = null, $methods = null)
|
||||
{
|
||||
if($index !== null)
|
||||
{
|
||||
return request()->getInput()->get($index, $defaultValue, $methods);
|
||||
}
|
||||
|
||||
return request()->getInput();
|
||||
}
|
||||
|
||||
@@ -310,7 +358,7 @@ SimpleRouter::any('foo', function() {
|
||||
});
|
||||
```
|
||||
|
||||
We've created a simple method which matches `GET` and `POST` which is most commenly used:
|
||||
We've created a simple method which matches `GET` and `POST` which is most commonly used:
|
||||
|
||||
```php
|
||||
SimpleRouter::form('foo', function() {
|
||||
@@ -395,12 +443,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.
|
||||
|
||||
@@ -494,7 +542,7 @@ SimpleRouter::group(['namespace' => 'Admin'], function () {
|
||||
});
|
||||
```
|
||||
|
||||
### Subdomain-routing
|
||||
### Sub domain-routing
|
||||
|
||||
Route groups may also be used to handle sub-domain routing. Sub-domains may be assigned route parameters just like route URIs, allowing you to capture a portion of the sub-domain for usage in your route or controller. The sub-domain may be specified using the `domain` key on the group attribute array:
|
||||
|
||||
@@ -518,6 +566,29 @@ SimpleRouter::group(['prefix' => '/admin'], function () {
|
||||
});
|
||||
```
|
||||
|
||||
## Partial groups
|
||||
|
||||
Partial router groups has the same benefits as a normal group, but supports parameters and are only rendered once the url has matched.
|
||||
|
||||
This can be extremely useful in situations, where you only want special routes to be added, when a certain criteria or logic has been met.
|
||||
|
||||
**NOTE:** Use partial groups with caution as routes added within are only rendered and available once the url of the partial-group has matched. This can cause `url()` not to find urls for the routes added within.
|
||||
|
||||
**Example:**
|
||||
|
||||
```php
|
||||
SimpleRouter::partialGroup('/admin/{applicationId}', function ($applicationId) {
|
||||
|
||||
SimpleRouter::get('/', function($applicationId) {
|
||||
|
||||
// Matches The "/admin/applicationId" URL
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
## Form Method Spoofing
|
||||
|
||||
HTML forms do not support `PUT`, `PATCH` or `DELETE` actions. So, when defining `PUT`, `PATCH` or `DELETE` routes that are called from an HTML form, you will need to add a hidden `_method` field to the form. The value sent with the `_method` field will be used as the HTTP request method:
|
||||
@@ -663,18 +734,18 @@ 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
|
||||
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');
|
||||
}
|
||||
});
|
||||
@@ -698,7 +769,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(),
|
||||
@@ -825,7 +896,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,11 +905,11 @@ 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.
|
||||
|
||||
**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);
|
||||
@@ -855,13 +926,17 @@ $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 --
|
||||
|
||||
$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 --
|
||||
|
||||
@@ -880,7 +955,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 +1001,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']);
|
||||
```
|
||||
|
||||
---
|
||||
@@ -1079,7 +1154,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;
|
||||
}
|
||||
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
use Pecee\SimpleRouter\SimpleRouter as Router;
|
||||
|
||||
/**
|
||||
* Get url for a route by using either name/alias, class or method name.
|
||||
*
|
||||
* The name parameter supports the following values:
|
||||
* - Route name
|
||||
* - Controller/resource name (with or without method)
|
||||
* - Controller class name
|
||||
*
|
||||
* When searching for controller/resource by name, you can use this syntax "route.name@method".
|
||||
* You can also use the same syntax when searching for a specific controller-class "MyController@home".
|
||||
* If no arguments is specified, it will return the url for the current loaded route.
|
||||
*
|
||||
* @param string|null $name
|
||||
* @param string|array|null $parameters
|
||||
* @param array|null $getParams
|
||||
* @return string
|
||||
*/
|
||||
function url($name = null, $parameters = null, $getParams = null)
|
||||
{
|
||||
return Router::getUrl($name, $parameters, $getParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Pecee\Http\Response
|
||||
*/
|
||||
function response()
|
||||
{
|
||||
return Router::response();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Pecee\Http\Request
|
||||
*/
|
||||
function request()
|
||||
{
|
||||
return Router::request();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get input class
|
||||
* @param string|null $index Parameter index name
|
||||
* @param string|null $defaultValue Default return value
|
||||
* @param string|array|null $methods Default method
|
||||
* @return \Pecee\Http\Input\Input|string
|
||||
*/
|
||||
function input($index = null, $defaultValue = null, $methods = null)
|
||||
{
|
||||
if ($index !== null) {
|
||||
return request()->getInput()->get($index, $defaultValue, $methods);
|
||||
}
|
||||
|
||||
return request()->getInput();
|
||||
}
|
||||
|
||||
function redirect($url, $code = null)
|
||||
{
|
||||
if ($code !== null) {
|
||||
response()->httpCode($code);
|
||||
}
|
||||
|
||||
response()->redirect($url);
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -29,8 +29,11 @@ class Request
|
||||
public function __construct()
|
||||
{
|
||||
$this->parseHeaders();
|
||||
$this->host = $this->getHeader('http-host');
|
||||
$this->uri = $this->getHeader('request-uri');
|
||||
$this->setHost($this->getHeader('http-host'));
|
||||
|
||||
// Check if special IIS header exist, otherwise use default.
|
||||
$this->setUri(new Uri($this->getHeader('unencoded-url', $this->getHeader('request-uri'))));
|
||||
|
||||
$this->input = new Input($this);
|
||||
$this->method = strtolower($this->input->get('_method', $this->getHeader('request-method'), 'post'));
|
||||
}
|
||||
@@ -58,7 +61,7 @@ class Request
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @return Uri
|
||||
*/
|
||||
public function getUri()
|
||||
{
|
||||
@@ -164,7 +167,7 @@ class Request
|
||||
*/
|
||||
public function getHeader($name, $defaultValue = null)
|
||||
{
|
||||
if (isset($this->headers[strtolower($name)])) {
|
||||
if (array_key_exists(strtolower($name), $this->headers) === true) {
|
||||
return $this->headers[strtolower($name)];
|
||||
}
|
||||
|
||||
@@ -215,9 +218,9 @@ class Request
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $uri
|
||||
* @param Uri $uri
|
||||
*/
|
||||
public function setUri($uri)
|
||||
public function setUri(Uri $uri)
|
||||
{
|
||||
$this->uri = $uri;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace Pecee\Http;
|
||||
|
||||
class Response
|
||||
@@ -41,7 +42,7 @@ class Response
|
||||
|
||||
public function refresh()
|
||||
{
|
||||
$this->redirect($this->request->getUri());
|
||||
$this->redirect($this->request->getUri()->getPath());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,14 +83,19 @@ class Response
|
||||
}
|
||||
|
||||
/**
|
||||
* Json encode array
|
||||
* @param array $value
|
||||
* Json encode
|
||||
* @param array|\JsonSerializable $value
|
||||
* @throws \InvalidArgumentException;
|
||||
*/
|
||||
public function json(array $value)
|
||||
public function json($value)
|
||||
{
|
||||
$this->header('Content-Type: application/json');
|
||||
if (($value instanceof \JsonSerializable) === false && is_array($value) === false) {
|
||||
throw new \InvalidArgumentException('Invalid type for parameter "value". Must be of type array or object implementing the \JsonSerializable interface.');
|
||||
}
|
||||
|
||||
$this->header('Content-type: application/json');
|
||||
echo json_encode($value);
|
||||
die();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
namespace Pecee\Http;
|
||||
|
||||
class Uri
|
||||
{
|
||||
private $originalUrl;
|
||||
private $data = [
|
||||
'scheme',
|
||||
'host',
|
||||
'port',
|
||||
'user',
|
||||
'pass',
|
||||
'path',
|
||||
'query',
|
||||
'fragment',
|
||||
];
|
||||
|
||||
public function __construct($url)
|
||||
{
|
||||
$this->originalUrl = $url;
|
||||
$this->data = array_merge($this->data, $this->parseUrl(urldecode($url)));
|
||||
|
||||
if (isset($this->data['path']) === true && $this->data['path'] !== '/') {
|
||||
$this->data['path'] = rtrim($this->data['path'], '/') . '/';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @param string $url
|
||||
* @param int $component
|
||||
* @throws \InvalidArgumentException
|
||||
* @return array
|
||||
*/
|
||||
public function parseUrl($url, $component = -1)
|
||||
{
|
||||
$encodedUrl = preg_replace_callback(
|
||||
'%[^:/@?&=#]+%u',
|
||||
function ($matches) {
|
||||
return urlencode($matches[0]);
|
||||
},
|
||||
$url
|
||||
);
|
||||
|
||||
$parts = parse_url($encodedUrl, $component);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Pecee\SimpleRouter\Route;
|
||||
|
||||
interface IPartialGroupRoute
|
||||
{
|
||||
|
||||
}
|
||||
@@ -21,7 +21,7 @@ interface IRoute
|
||||
*
|
||||
* @param Request $request
|
||||
* @throws \Pecee\SimpleRouter\Exceptions\NotFoundHttpException
|
||||
* @return void
|
||||
* @return string
|
||||
*/
|
||||
public function renderRoute(Request $request);
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
||||
|
||||
$regex = sprintf(static::PARAMETERS_REGEX_FORMAT, $this->paramModifiers[0], $this->paramOptionalSymbol, $this->paramModifiers[1]);
|
||||
|
||||
if (preg_match_all('/' . $regex . '/', $this->url, $matches)) {
|
||||
if (preg_match_all('/' . $regex . '/u', $this->url, $matches)) {
|
||||
$this->parameters = array_fill_keys($matches[1], null);
|
||||
}
|
||||
}
|
||||
@@ -92,7 +92,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
||||
* Used when calling the url() helper.
|
||||
*
|
||||
* @param string|null $method
|
||||
* @param array|null $parameters
|
||||
* @param string|array|null $parameters
|
||||
* @param string|null $name
|
||||
* @return string
|
||||
*/
|
||||
@@ -123,11 +123,17 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
||||
|
||||
for ($i = $max; $i >= 0; $i--) {
|
||||
$param = $keys[$i];
|
||||
$value = $value = ($parameters !== null && array_key_exists($param, $parameters)) ? $parameters[$param] : $params[$param];
|
||||
|
||||
/* If parameter is specifically set to null - use the original-defined value */
|
||||
if ($value === null && isset($this->originalParameters[$param])) {
|
||||
$value = $this->originalParameters[$param];
|
||||
if ($parameters === '' || (is_array($parameters) && count($parameters) === 0)) {
|
||||
$value = '';
|
||||
} else {
|
||||
$p = (array)$parameters;
|
||||
$value = array_key_exists($param, $p) ? $p[$param] : $params[$param];
|
||||
|
||||
/* If parameter is specifically set to null - use the original-defined value */
|
||||
if ($value === null && isset($this->originalParameters[$param])) {
|
||||
$value = $this->originalParameters[$param];
|
||||
}
|
||||
}
|
||||
|
||||
if (stripos($url, $param1) !== false || stripos($url, $param) !== false) {
|
||||
@@ -138,6 +144,7 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
||||
}
|
||||
}
|
||||
|
||||
$url = '/' . ltrim($url, '/');
|
||||
$url .= join('/', $unknownParams);
|
||||
|
||||
return rtrim($url, '/') . '/';
|
||||
|
||||
@@ -34,9 +34,15 @@ abstract class Route implements IRoute
|
||||
* @var bool
|
||||
*/
|
||||
protected $filterEmptyParams = false;
|
||||
protected $defaultParameterRegex = null;
|
||||
|
||||
/**
|
||||
* Default regular expression used for parsing parameters.
|
||||
* @var string|null
|
||||
*/
|
||||
protected $defaultParameterRegex;
|
||||
protected $paramModifiers = '{}';
|
||||
protected $paramOptionalSymbol = '?';
|
||||
protected $urlRegex = '/^%s\/?$/u';
|
||||
protected $group;
|
||||
protected $parent;
|
||||
protected $callback;
|
||||
@@ -64,16 +70,14 @@ abstract class Route implements IRoute
|
||||
$callback = $this->getCallback();
|
||||
|
||||
if ($callback === null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
/* Render callback function */
|
||||
if (is_callable($callback) === true) {
|
||||
|
||||
/* When the callback is a function */
|
||||
call_user_func_array($callback, $this->getParameters());
|
||||
|
||||
return;
|
||||
return call_user_func_array($callback, $this->getParameters());
|
||||
|
||||
}
|
||||
|
||||
@@ -101,7 +105,7 @@ abstract class Route implements IRoute
|
||||
});
|
||||
}
|
||||
|
||||
call_user_func_array([$class, $method], $parameters);
|
||||
return call_user_func_array([$class, $method], $parameters);
|
||||
}
|
||||
|
||||
protected function parseParameters($route, $url, $parameterRegex = null)
|
||||
@@ -113,7 +117,7 @@ abstract class Route implements IRoute
|
||||
// Ensures that hostnames/domains will work with parameters
|
||||
$url = '/' . ltrim($url, '/');
|
||||
|
||||
if (preg_match_all('/' . $regex . '/', $route, $parameters)) {
|
||||
if (preg_match_all('/' . $regex . '/u', $route, $parameters)) {
|
||||
|
||||
$urlParts = preg_split('/((\-?\/?)\{[^}]+\})/', rtrim($route, '/'));
|
||||
|
||||
@@ -138,7 +142,7 @@ abstract class Route implements IRoute
|
||||
}
|
||||
}
|
||||
|
||||
$regex = sprintf('(?:\/|\-)' . $parameters[2][$key] . '(?P<%s>%s)', $name, $regex) . $parameters[2][$key];
|
||||
$regex = sprintf('(?:\/|\-)%1$s(?P<%2$s>%3$s)%1$s', $parameters[2][$key], $name, $regex);
|
||||
|
||||
}
|
||||
|
||||
@@ -151,14 +155,14 @@ abstract class Route implements IRoute
|
||||
$urlRegex = preg_quote($route, '/');
|
||||
}
|
||||
|
||||
if (preg_match('/^' . $urlRegex . '\/?/', $url, $matches) > 0) {
|
||||
if (preg_match(sprintf($this->urlRegex, $urlRegex), $url, $matches) > 0) {
|
||||
|
||||
$values = [];
|
||||
|
||||
if (isset($parameters[1]) === true) {
|
||||
|
||||
/* Only take matched parameters with name */
|
||||
foreach ($parameters[1] as $name) {
|
||||
foreach ((array)$parameters[1] as $name) {
|
||||
$values[$name] = (isset($matches[$name]) && $matches[$name] !== '') ? $matches[$name] : null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,9 +87,6 @@ class RouteController extends LoadableRoute implements IControllerRoute
|
||||
|
||||
public function matchRoute($url, Request $request)
|
||||
{
|
||||
$url = parse_url(urldecode($url), PHP_URL_PATH);
|
||||
$url = rtrim($url, '/') . '/';
|
||||
|
||||
/* Match global regular-expression for route */
|
||||
$regexMatch = $this->matchRegex($request, $url);
|
||||
|
||||
|
||||
@@ -150,11 +150,11 @@ class RouteGroup extends Route implements IGroupRoute
|
||||
$this->setPrefix($values['prefix'] . $this->prefix);
|
||||
}
|
||||
|
||||
if (isset($values['exceptionHandler'])) {
|
||||
if ($merge === false && isset($values['exceptionHandler'])) {
|
||||
$this->setExceptionHandlers((array)$values['exceptionHandler']);
|
||||
}
|
||||
|
||||
if (isset($values['domain'])) {
|
||||
if ($merge === false && isset($values['domain'])) {
|
||||
$this->setDomains((array)$values['domain']);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Pecee\SimpleRouter\Route;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
|
||||
class RoutePartialGroup extends RouteGroup implements IPartialGroupRoute
|
||||
{
|
||||
protected $urlRegex = '/^%s\/?/u';
|
||||
|
||||
/**
|
||||
* Method called to check if route matches
|
||||
*
|
||||
* @param string $url
|
||||
* @param Request $request
|
||||
* @return bool
|
||||
*/
|
||||
public function matchRoute($url, Request $request)
|
||||
{
|
||||
if ($this->prefix !== null) {
|
||||
/* Parse parameters from current route */
|
||||
$parameters = $this->parseParameters($this->prefix, $url);
|
||||
|
||||
/* If no custom regular expression or parameters was found on this route, we stop */
|
||||
if ($parameters === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Set the parameters */
|
||||
$this->setParameters((array)$parameters);
|
||||
}
|
||||
|
||||
return $this->matchDomain($request);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -79,9 +79,6 @@ class RouteResource extends LoadableRoute implements IControllerRoute
|
||||
|
||||
public function matchRoute($url, Request $request)
|
||||
{
|
||||
$url = parse_url(urldecode($url), PHP_URL_PATH);
|
||||
$url = rtrim($url, '/') . '/';
|
||||
|
||||
/* Match global regular-expression for route */
|
||||
$regexMatch = $this->matchRegex($request, $url);
|
||||
|
||||
|
||||
@@ -14,9 +14,6 @@ class RouteUrl extends LoadableRoute
|
||||
|
||||
public function matchRoute($url, Request $request)
|
||||
{
|
||||
$url = parse_url(urldecode($url), PHP_URL_PATH);
|
||||
$url = rtrim($url, '/') . '/';
|
||||
|
||||
/* Match global regular-expression for route */
|
||||
$regexMatch = $this->matchRegex($request, $url);
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||
use Pecee\SimpleRouter\Route\IControllerRoute;
|
||||
use Pecee\SimpleRouter\Route\IGroupRoute;
|
||||
use Pecee\SimpleRouter\Route\ILoadableRoute;
|
||||
use Pecee\SimpleRouter\Route\IPartialGroupRoute;
|
||||
use Pecee\SimpleRouter\Route\IRoute;
|
||||
|
||||
class Router
|
||||
@@ -122,39 +123,13 @@ 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--) {
|
||||
|
||||
$route = $routes[$i];
|
||||
|
||||
/* @var $route IGroupRoute */
|
||||
if ($route instanceof IGroupRoute) {
|
||||
|
||||
$group = $route;
|
||||
|
||||
$this->processingRoute = true;
|
||||
$route->renderRoute($this->request);
|
||||
$this->processingRoute = false;
|
||||
|
||||
if ($route->matchRoute($url, $this->request) === true) {
|
||||
|
||||
/* Add exception handlers */
|
||||
if (count($route->getExceptionHandlers()) > 0) {
|
||||
/** @noinspection AdditionOperationOnArraysInspection */
|
||||
$exceptionHandlers += $route->getExceptionHandlers();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if ($group !== null) {
|
||||
|
||||
/* Add the parent group */
|
||||
$route->setGroup($group);
|
||||
}
|
||||
|
||||
if ($parent !== null) {
|
||||
|
||||
/* Add the parent route */
|
||||
@@ -165,6 +140,41 @@ class Router
|
||||
|
||||
}
|
||||
|
||||
if ($group !== null) {
|
||||
|
||||
/* Add the parent group */
|
||||
$route->setGroup($group);
|
||||
}
|
||||
|
||||
/* @var $route IGroupRoute */
|
||||
if ($route instanceof IGroupRoute) {
|
||||
|
||||
$group = $route;
|
||||
|
||||
if ($route->matchRoute($url, $this->request) === true) {
|
||||
|
||||
/* Add exception handlers */
|
||||
if (count($route->getExceptionHandlers()) > 0) {
|
||||
/** @noinspection AdditionOperationOnArraysInspection */
|
||||
$exceptionHandlers += $route->getExceptionHandlers();
|
||||
}
|
||||
|
||||
/* Only render partial group if it matches */
|
||||
if ($route instanceof IPartialGroupRoute) {
|
||||
$this->processingRoute = true;
|
||||
$route->renderRoute($this->request);
|
||||
$this->processingRoute = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (($route instanceof IPartialGroupRoute) === false) {
|
||||
$this->processingRoute = true;
|
||||
$route->renderRoute($this->request);
|
||||
$this->processingRoute = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ($route instanceof ILoadableRoute) {
|
||||
|
||||
/* Add the route to the map, so we can find the active one when all routes has been loaded */
|
||||
@@ -224,7 +234,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,9 +258,8 @@ class Router
|
||||
|
||||
if ($rewriteRoute !== null) {
|
||||
$rewriteRoute->loadMiddleware($this->request);
|
||||
$rewriteRoute->renderRoute($this->request);
|
||||
|
||||
return;
|
||||
return $rewriteRoute->renderRoute($this->request);
|
||||
}
|
||||
|
||||
/* If the request has changed */
|
||||
@@ -259,17 +268,15 @@ class Router
|
||||
if ($rewriteUrl !== null && $rewriteUrl !== $url) {
|
||||
unset($this->processedRoutes[$i]);
|
||||
$this->processedRoutes = array_values($this->processedRoutes);
|
||||
$this->routeRequest(true);
|
||||
|
||||
return;
|
||||
return $this->routeRequest(true);
|
||||
}
|
||||
|
||||
/* Render route */
|
||||
$routeNotAllowed = false;
|
||||
$this->request->setLoadedRoute($route);
|
||||
$route->renderRoute($this->request);
|
||||
|
||||
break;
|
||||
return $route->renderRoute($this->request);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,18 +293,26 @@ 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));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Exception $e
|
||||
* @throws HttpException
|
||||
* @throws \Exception
|
||||
* @return string
|
||||
*/
|
||||
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);
|
||||
|
||||
@@ -320,9 +335,8 @@ class Router
|
||||
|
||||
if ($rewriteRoute !== null) {
|
||||
$rewriteRoute->loadMiddleware($this->request);
|
||||
$rewriteRoute->renderRoute($this->request);
|
||||
|
||||
return;
|
||||
return $rewriteRoute->renderRoute($this->request);
|
||||
}
|
||||
|
||||
$rewriteUrl = $this->request->getRewriteUrl();
|
||||
@@ -331,9 +345,8 @@ class Router
|
||||
if ($rewriteUrl !== null && $rewriteUrl !== $url) {
|
||||
unset($this->exceptionHandlers[$i]);
|
||||
$this->exceptionHandlers = array_values($this->exceptionHandlers);
|
||||
$this->routeRequest(true);
|
||||
|
||||
return;
|
||||
return $this->routeRequest(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -433,6 +446,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;
|
||||
@@ -442,9 +459,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();
|
||||
|
||||
@@ -16,6 +16,7 @@ use Pecee\Http\Response;
|
||||
use Pecee\SimpleRouter\Exceptions\HttpException;
|
||||
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||
use Pecee\SimpleRouter\Route\IRoute;
|
||||
use Pecee\SimpleRouter\Route\RoutePartialGroup;
|
||||
use Pecee\SimpleRouter\Route\RouteController;
|
||||
use Pecee\SimpleRouter\Route\RouteGroup;
|
||||
use Pecee\SimpleRouter\Route\RouteResource;
|
||||
@@ -49,7 +50,7 @@ class SimpleRouter
|
||||
*/
|
||||
public static function start()
|
||||
{
|
||||
static::router()->routeRequest();
|
||||
echo static::router()->routeRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -171,14 +172,41 @@ class SimpleRouter
|
||||
*/
|
||||
public static function group(array $settings = [], \Closure $callback)
|
||||
{
|
||||
if (is_callable($callback) === false) {
|
||||
throw new \InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
|
||||
}
|
||||
|
||||
$group = new RouteGroup();
|
||||
$group->setCallback($callback);
|
||||
$group->setSettings($settings);
|
||||
|
||||
static::router()->addRoute($group);
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* Special group that has the same benefits as group but supports
|
||||
* parameters and which are only rendered when the url matches.
|
||||
*
|
||||
* @param string $url
|
||||
* @param array $settings
|
||||
* @param \Closure $callback
|
||||
* @throws \InvalidArgumentException
|
||||
* @return RoutePartialGroup
|
||||
*/
|
||||
public static function partialGroup($url, \Closure $callback, array $settings = [])
|
||||
{
|
||||
if (is_callable($callback) === false) {
|
||||
throw new \InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
|
||||
}
|
||||
|
||||
$settings['prefix'] = $url;
|
||||
|
||||
$group = new RoutePartialGroup();
|
||||
$group->setSettings($settings);
|
||||
$group->setCallback($callback);
|
||||
|
||||
static::router()->addRoute($group);
|
||||
|
||||
return $group;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
require_once 'Dummy/DummyMiddleware.php';
|
||||
require_once 'Dummy/DummyController.php';
|
||||
require_once 'Dummy/Handler/ExceptionHandler.php';
|
||||
require_once 'Helpers/TestRouter.php';
|
||||
|
||||
class RouterPartialGroupTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testParameters()
|
||||
{
|
||||
$result1 = null;
|
||||
$result2 = null;
|
||||
|
||||
TestRouter::partialGroup('{param1}/{param2}', function ($param1 = null, $param2 = null) use (&$result1, &$result2) {
|
||||
$result1 = $param1;
|
||||
$result2 = $param2;
|
||||
|
||||
TestRouter::get('/', 'DummyController@method1');
|
||||
});
|
||||
|
||||
TestRouter::debug('/param1/param2', 'get');
|
||||
|
||||
$this->assertEquals('param1', $result1);
|
||||
$this->assertEquals('param2', $result2);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -8,6 +8,48 @@ require_once 'Helpers/TestRouter.php';
|
||||
class RouterUrlTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testIssue253()
|
||||
{
|
||||
TestRouter::get('/', 'DummyController@method1');
|
||||
TestRouter::get('/page/{id?}', 'DummyController@method1');
|
||||
TestRouter::get('/test-output', function() {
|
||||
return 'return value';
|
||||
});
|
||||
|
||||
TestRouter::debugNoReset('/page/22', 'get');
|
||||
$this->assertEquals('/page/{id?}/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl());
|
||||
|
||||
TestRouter::debugNoReset('/', 'get');
|
||||
$this->assertEquals('/', TestRouter::router()->getRequest()->getLoadedRoute()->getUrl());
|
||||
|
||||
$output = TestRouter::debugOutput('/test-output', 'get');
|
||||
$this->assertEquals('return value', $output);
|
||||
|
||||
TestRouter::router()->reset();
|
||||
}
|
||||
|
||||
public function testUnicodeCharacters()
|
||||
{
|
||||
// Test spanish characters
|
||||
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());
|
||||
|
||||
// Test danish characters
|
||||
TestRouter::get('/kategori/økse', 'DummyController@method1', ['defaultParameterRegex' => '[\w\ø]+']);
|
||||
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();
|
||||
}
|
||||
|
||||
public function testOptionalParameters()
|
||||
{
|
||||
TestRouter::get('/aviso/legal', 'DummyController@method1');
|
||||
|
||||
Reference in New Issue
Block a user