mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 02:42:13 +00:00
@@ -972,6 +972,12 @@ If you do not want a redirect, but want the error-page rendered on the current-u
|
|||||||
$request->setRewriteCallback('ErrorController@notFound');
|
$request->setRewriteCallback('ErrorController@notFound');
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If you will set the correct status for the browser error use:
|
||||||
|
|
||||||
|
```php
|
||||||
|
SimpleRouter::response()->httpCode(404);
|
||||||
|
```
|
||||||
|
|
||||||
## Using custom exception handlers
|
## Using custom exception handlers
|
||||||
|
|
||||||
This is a basic example of an ExceptionHandler implementation (please see "[Easily overwrite route about to be loaded](#easily-overwrite-route-about-to-be-loaded)" for examples on how to change callback).
|
This is a basic example of an ExceptionHandler implementation (please see "[Easily overwrite route about to be loaded](#easily-overwrite-route-about-to-be-loaded)" for examples on how to change callback).
|
||||||
@@ -2073,4 +2079,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
|
|||||||
+1
-1
@@ -27,7 +27,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=7.1",
|
"php": ">=7.4",
|
||||||
"ext-json": "*"
|
"ext-json": "*"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ class InputItem implements ArrayAccess, IInputItem, IteratorAggregate
|
|||||||
return isset($this->value[$offset]);
|
return isset($this->value[$offset]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function offsetGet($offset)
|
public function offsetGet($offset): ?self
|
||||||
{
|
{
|
||||||
if ($this->offsetExists($offset) === true) {
|
if ($this->offsetExists($offset) === true) {
|
||||||
return $this->value[$offset];
|
return $this->value[$offset];
|
||||||
|
|||||||
@@ -129,7 +129,12 @@ class Request
|
|||||||
$this->setHost($this->getHeader('http-host'));
|
$this->setHost($this->getHeader('http-host'));
|
||||||
|
|
||||||
// Check if special IIS header exist, otherwise use default.
|
// Check if special IIS header exist, otherwise use default.
|
||||||
$this->setUrl(new Url($this->getFirstHeader(['unencoded-url', 'request-uri'])));
|
$url = $this->getHeader('unencoded-url');
|
||||||
|
if($url !== null){
|
||||||
|
$this->setUrl(new Url($url));
|
||||||
|
}else{
|
||||||
|
$this->setUrl(new Url(urldecode($this->getHeader('request-uri'))));
|
||||||
|
}
|
||||||
$this->setContentType((string)$this->getHeader('content-type'));
|
$this->setContentType((string)$this->getHeader('content-type'));
|
||||||
$this->setMethod((string)($_POST[static::FORCE_METHOD_KEY] ?? $this->getHeader('request-method')));
|
$this->setMethod((string)($_POST[static::FORCE_METHOD_KEY] ?? $this->getHeader('request-method')));
|
||||||
$this->inputHandler = new InputHandler($this);
|
$this->inputHandler = new InputHandler($this);
|
||||||
|
|||||||
@@ -123,7 +123,8 @@ abstract class Route implements IRoute
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Ensures that host names/domains will work with parameters
|
// Ensures that host names/domains will work with parameters
|
||||||
$url = '/' . ltrim($url, '/');
|
|
||||||
|
if($route[0] == '{') $url = '/' . ltrim($url, '/');
|
||||||
$urlRegex = '';
|
$urlRegex = '';
|
||||||
$parameters = [];
|
$parameters = [];
|
||||||
|
|
||||||
@@ -131,7 +132,7 @@ abstract class Route implements IRoute
|
|||||||
$urlRegex = preg_quote($route, '/');
|
$urlRegex = preg_quote($route, '/');
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
foreach (preg_split('/((-?\/?){[^}]+})/', $route) as $key => $t) {
|
foreach (preg_split('/((\.?-?\/?){[^}]+})/', $route) as $key => $t) {
|
||||||
|
|
||||||
$regex = '';
|
$regex = '';
|
||||||
|
|
||||||
@@ -146,7 +147,7 @@ abstract class Route implements IRoute
|
|||||||
$regex = $parameterRegex ?? $this->defaultParameterRegex ?? static::PARAMETERS_DEFAULT_REGEX;
|
$regex = $parameterRegex ?? $this->defaultParameterRegex ?? static::PARAMETERS_DEFAULT_REGEX;
|
||||||
}
|
}
|
||||||
|
|
||||||
$regex = sprintf('((\/|-)(?P<%2$s>%3$s))%1$s', $parameters[2][$key], $name, $regex);
|
$regex = sprintf('((\/|-|\.)(?P<%2$s>%3$s))%1$s', $parameters[2][$key], $name, $regex);
|
||||||
}
|
}
|
||||||
|
|
||||||
$urlRegex .= preg_quote($t, '/') . $regex;
|
$urlRegex .= preg_quote($t, '/') . $regex;
|
||||||
|
|||||||
@@ -35,6 +35,12 @@ class Router
|
|||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
protected $isProcessingRoute;
|
protected $isProcessingRoute;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines all data from current processing route.
|
||||||
|
* @var ILoadableRoute
|
||||||
|
*/
|
||||||
|
protected $currentProcessingRoute;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All added routes
|
* All added routes
|
||||||
@@ -371,6 +377,9 @@ class Router
|
|||||||
foreach ($this->processedRoutes as $key => $route) {
|
foreach ($this->processedRoutes as $key => $route) {
|
||||||
|
|
||||||
$this->debug('Matching route "%s"', get_class($route));
|
$this->debug('Matching route "%s"', get_class($route));
|
||||||
|
|
||||||
|
/* Add current processing route to constants */
|
||||||
|
$this->currentProcessingRoute = $route;
|
||||||
|
|
||||||
/* If the route matches */
|
/* If the route matches */
|
||||||
if ($route->matchRoute($url, $this->request) === true) {
|
if ($route->matchRoute($url, $this->request) === true) {
|
||||||
@@ -927,6 +936,16 @@ class Router
|
|||||||
{
|
{
|
||||||
return $this->debugList;
|
return $this->debugList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current processing route details.
|
||||||
|
*
|
||||||
|
* @return ILoadableRoute
|
||||||
|
*/
|
||||||
|
public function getCurrentProcessingRoute(): ILoadableRoute
|
||||||
|
{
|
||||||
|
return $this->currentProcessingRoute;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Changes the rendering behavior of the router.
|
* Changes the rendering behavior of the router.
|
||||||
@@ -950,4 +969,4 @@ class Router
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -175,6 +175,70 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase
|
|||||||
|
|
||||||
TestRouter::debug('/test', 'get');
|
TestRouter::debug('/test', 'get');
|
||||||
|
|
||||||
|
$this->assertFalse($result);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFixedSubdomainDynamicDomain()
|
||||||
|
{
|
||||||
|
TestRouter::request()->setHost('other.world.com');
|
||||||
|
|
||||||
|
$result = false;
|
||||||
|
|
||||||
|
TestRouter::group(['domain' => 'other.{domain}'], function () use (&$result) {
|
||||||
|
TestRouter::get('/test', function ($domain = null) use (&$result) {
|
||||||
|
|
||||||
|
$result = true;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
TestRouter::debug('/test', 'get');
|
||||||
|
|
||||||
|
$this->assertTrue($result);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFixedSubdomainDynamicDomainParameter()
|
||||||
|
{
|
||||||
|
TestRouter::request()->setHost('other.world.com');
|
||||||
|
|
||||||
|
$result = false;
|
||||||
|
|
||||||
|
TestRouter::group(['domain' => 'other.{domain}'], function () use (&$result) {
|
||||||
|
TestRouter::get('/test', 'DummyController@param');
|
||||||
|
TestRouter::get('/test/{key}', 'DummyController@param');
|
||||||
|
});
|
||||||
|
|
||||||
|
$response = TestRouter::debugOutputNoReset('/test', 'get');
|
||||||
|
|
||||||
|
$this->assertEquals('world.com', $response);
|
||||||
|
|
||||||
|
$response = TestRouter::debugOutput('/test/unittest', 'get');
|
||||||
|
|
||||||
|
$this->assertEquals('unittest, world.com', $response);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWrongFixedSubdomainDynamicDomain()
|
||||||
|
{
|
||||||
|
TestRouter::request()->setHost('wrong.world.com');
|
||||||
|
|
||||||
|
$result = false;
|
||||||
|
|
||||||
|
TestRouter::group(['domain' => 'other.{domain}'], function () use (&$result) {
|
||||||
|
TestRouter::get('/test', function ($domain = null) use (&$result) {
|
||||||
|
|
||||||
|
$result = true;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
TestRouter::debug('/test', 'get');
|
||||||
|
} catch(\Exception $e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
$this->assertFalse($result);
|
$this->assertFalse($result);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,4 +48,17 @@ class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
|
|||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function debugOutputNoReset(string $testUrl, string $testMethod = 'get', bool $reset = true): string
|
||||||
|
{
|
||||||
|
$response = null;
|
||||||
|
|
||||||
|
// Route request
|
||||||
|
ob_start();
|
||||||
|
static::debugNoReset($testUrl, $testMethod, $reset);
|
||||||
|
$response = ob_get_clean();
|
||||||
|
|
||||||
|
// Return response
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user