mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-15 18:23:26 +03:00
Merge pull request #627 from skipperbent/v4-development
Version 5.0.0.0
This commit is contained in:
@@ -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');
|
||||
```
|
||||
|
||||
If you will set the correct status for the browser error use:
|
||||
|
||||
```php
|
||||
SimpleRouter::response()->httpCode(404);
|
||||
```
|
||||
|
||||
## 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).
|
||||
@@ -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
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
SOFTWARE.
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.1",
|
||||
"php": ">=7.4",
|
||||
"ext-json": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
|
||||
@@ -129,7 +129,12 @@ class Request
|
||||
$this->setHost($this->getHeader('http-host'));
|
||||
|
||||
// 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->setMethod((string)($_POST[static::FORCE_METHOD_KEY] ?? $this->getHeader('request-method')));
|
||||
$this->inputHandler = new InputHandler($this);
|
||||
|
||||
@@ -123,7 +123,8 @@ abstract class Route implements IRoute
|
||||
);
|
||||
|
||||
// Ensures that host names/domains will work with parameters
|
||||
$url = '/' . ltrim($url, '/');
|
||||
|
||||
if($route[0] == '{') $url = '/' . ltrim($url, '/');
|
||||
$urlRegex = '';
|
||||
$parameters = [];
|
||||
|
||||
@@ -131,7 +132,7 @@ abstract class Route implements IRoute
|
||||
$urlRegex = preg_quote($route, '/');
|
||||
} else {
|
||||
|
||||
foreach (preg_split('/((-?\/?){[^}]+})/', $route) as $key => $t) {
|
||||
foreach (preg_split('/((\.?-?\/?){[^}]+})/', $route) as $key => $t) {
|
||||
|
||||
$regex = '';
|
||||
|
||||
@@ -146,7 +147,7 @@ abstract class Route implements IRoute
|
||||
$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;
|
||||
|
||||
@@ -35,6 +35,12 @@ class Router
|
||||
* @var bool
|
||||
*/
|
||||
protected $isProcessingRoute;
|
||||
|
||||
/**
|
||||
* Defines all data from current processing route.
|
||||
* @var ILoadableRoute
|
||||
*/
|
||||
protected $currentProcessingRoute;
|
||||
|
||||
/**
|
||||
* All added routes
|
||||
@@ -371,6 +377,9 @@ class Router
|
||||
foreach ($this->processedRoutes as $key => $route) {
|
||||
|
||||
$this->debug('Matching route "%s"', get_class($route));
|
||||
|
||||
/* Add current processing route to constants */
|
||||
$this->currentProcessingRoute = $route;
|
||||
|
||||
/* If the route matches */
|
||||
if ($route->matchRoute($url, $this->request) === true) {
|
||||
@@ -927,6 +936,16 @@ class Router
|
||||
{
|
||||
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.
|
||||
@@ -950,4 +969,4 @@ class Router
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,6 +175,70 @@ class RouterRouteTest extends \PHPUnit\Framework\TestCase
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
@@ -48,4 +48,17 @@ class TestRouter extends \Pecee\SimpleRouter\SimpleRouter
|
||||
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