diff --git a/flight/Engine.php b/flight/Engine.php index 2628a11..44affc1 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -32,13 +32,14 @@ use Throwable; * @method void halt(int $code = 200, string $message = '') Stops processing and returns a given response. * * Routing - * @method void route(string $pattern, callable $callback, bool $pass_route = false) Routes a URL to a callback function. + * @method void route(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a URL to a callback function with all applicable methods * @method void group(string $pattern, callable $callback) Groups a set of routes together under a common prefix. - * @method void post(string $pattern, callable $callback, bool $pass_route = false) Routes a POST URL to a callback function. - * @method void put(string $pattern, callable $callback, bool $pass_route = false) Routes a PUT URL to a callback function. - * @method void patch(string $pattern, callable $callback, bool $pass_route = false) Routes a PATCH URL to a callback function. - * @method void delete(string $pattern, callable $callback, bool $pass_route = false) Routes a DELETE URL to a callback function. + * @method void post(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a POST URL to a callback function. + * @method void put(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a PUT URL to a callback function. + * @method void patch(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a PATCH URL to a callback function. + * @method void delete(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a DELETE URL to a callback function. * @method Router router() Gets router + * @method string getUrl(string $alias) Gets a url from an alias * * Views * @method void render(string $file, array $data = null, string $key = null) Renders template @@ -151,7 +152,7 @@ class Engine $methods = [ 'start', 'stop', 'route', 'halt', 'error', 'notFound', 'render', 'redirect', 'etag', 'lastModified', 'json', 'jsonp', - 'post', 'put', 'patch', 'delete', 'group', + 'post', 'put', 'patch', 'delete', 'group', 'getUrl', ]; foreach ($methods as $name) { $this->dispatcher->set($name, [$this, '_' . $name]); @@ -462,10 +463,11 @@ class Engine * @param string $pattern URL pattern to match * @param callable $callback Callback function * @param bool $pass_route Pass the matching route object to the callback + * @param string $alias the alias for the route */ - public function _route(string $pattern, callable $callback, bool $pass_route = false): void + public function _route(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): void { - $this->router()->map($pattern, $callback, $pass_route); + $this->router()->map($pattern, $callback, $pass_route, $alias); } /** @@ -701,4 +703,16 @@ class Engine $this->halt(304); } } + + /** + * Gets a url from an alias that's supplied. + * + * @param string $alias the route alias + * @param array the params for the route if applicable + * @return string + */ + public function _getUrl(string $alias, array $params = []): string + { + return $this->router()->getUrlByAlias($alias, $params); + } } diff --git a/flight/Flight.php b/flight/Flight.php index b1a9b1f..399b151 100644 --- a/flight/Flight.php +++ b/flight/Flight.php @@ -23,13 +23,14 @@ use flight\template\View; * @method static void stop() Stops the framework and sends a response. * @method static void halt(int $code = 200, string $message = '') Stop the framework with an optional status code and message. * - * @method static void route(string $pattern, callable $callback, bool $pass_route = false) Maps a URL pattern to a callback. + * @method static void route(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Maps a URL pattern to a callback with all applicable methods. * @method static void group(string $pattern, callable $callback) Groups a set of routes together under a common prefix. - * @method void post(string $pattern, callable $callback, bool $pass_route = false) Routes a POST URL to a callback function. - * @method void put(string $pattern, callable $callback, bool $pass_route = false) Routes a PUT URL to a callback function. - * @method void patch(string $pattern, callable $callback, bool $pass_route = false) Routes a PATCH URL to a callback function. - * @method void delete(string $pattern, callable $callback, bool $pass_route = false) Routes a DELETE URL to a callback function. - * @method static Router router() Returns Router instance. + * @method void post(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a POST URL to a callback function. + * @method void put(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a PUT URL to a callback function. + * @method void patch(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a PATCH URL to a callback function. + * @method void delete(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a DELETE URL to a callback function. + * @method static Router router() Returns Router instance. + * @method string getUrl(string $alias) Gets a url from an alias * * @method static void map(string $name, callable $callback) Creates a custom framework method. * diff --git a/tests/EngineTest.php b/tests/EngineTest.php index cf2b13a..e1b9aaf 100644 --- a/tests/EngineTest.php +++ b/tests/EngineTest.php @@ -265,4 +265,11 @@ class EngineTest extends PHPUnit\Framework\TestCase $this->assertEquals('Fri, 13 Feb 2009 23:31:30 GMT', $engine->response()->headers()['Last-Modified']); $this->assertEquals(304, $engine->response()->status()); } + + public function testGetUrl() { + $engine = new Engine; + $engine->route('/path1/@param:[0-9]{3}', function() { echo 'I win'; }, false, 'path1'); + $url = $engine->getUrl('path1', [ 'param' => 123 ]); + $this->assertEquals('/path1/123', $url); + } } diff --git a/tests/FlightTest.php b/tests/FlightTest.php index 12b2772..29e90ca 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -187,4 +187,10 @@ class FlightTest extends PHPUnit\Framework\TestCase $this->expectOutputString('test delete'); Flight::start(); } + + public function testGetUrl() { + Flight::route('/path1/@param:[a-zA-Z0-9]{2,3}', function() { echo 'I win'; }, false, 'path1'); + $url = Flight::getUrl('path1', [ 'param' => 123 ]); + $this->assertEquals('/path1/123', $url); + } }