diff --git a/README.md b/README.md index 73deaad..2cb77d7 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file +SOFTWARE. diff --git a/composer.json b/composer.json index a8294cd..a6e56c8 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ } ], "require": { - "php": ">=7.1", + "php": ">=7.4", "ext-json": "*" }, "require-dev": { diff --git a/src/Pecee/Http/Request.php b/src/Pecee/Http/Request.php index 12ede38..63bd5d6 100644 --- a/src/Pecee/Http/Request.php +++ b/src/Pecee/Http/Request.php @@ -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); diff --git a/src/Pecee/SimpleRouter/Route/Route.php b/src/Pecee/SimpleRouter/Route/Route.php index a8a5c37..401b1d7 100644 --- a/src/Pecee/SimpleRouter/Route/Route.php +++ b/src/Pecee/SimpleRouter/Route/Route.php @@ -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; diff --git a/src/Pecee/SimpleRouter/Router.php b/src/Pecee/SimpleRouter/Router.php index 76721d2..509ca2a 100644 --- a/src/Pecee/SimpleRouter/Router.php +++ b/src/Pecee/SimpleRouter/Router.php @@ -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; } -} \ No newline at end of file +} diff --git a/tests/Pecee/SimpleRouter/RouterRouteTest.php b/tests/Pecee/SimpleRouter/RouterRouteTest.php index 971676a..18ab4d9 100644 --- a/tests/Pecee/SimpleRouter/RouterRouteTest.php +++ b/tests/Pecee/SimpleRouter/RouterRouteTest.php @@ -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); } diff --git a/tests/TestRouter.php b/tests/TestRouter.php index 1a4cfd7..636c836 100644 --- a/tests/TestRouter.php +++ b/tests/TestRouter.php @@ -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; + } + } \ No newline at end of file