Fixes for group routing

pull/510/head
Austin Collier 1 year ago
parent 387f15bd8b
commit 74d2fd7002

@ -25,12 +25,12 @@ use flight\template\View;
*
* @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, 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 post(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a POST URL to a callback function.
* @method static void put(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a PUT URL to a callback function.
* @method static void patch(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a PATCH URL to a callback function.
* @method static 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 static string getUrl(string $alias) Gets a url from an alias
*
* @method static void map(string $name, callable $callback) Creates a custom framework method.
*

@ -45,7 +45,7 @@ class Router
/**
* Gets mapped routes.
*
* @return array<int, Route> Array of routes
* @return array<int,Route> Array of routes
*/
public function getRoutes(): array
{
@ -89,10 +89,11 @@ class Router
* @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 Alias for the route
* @return void
*/
public function get(string $pattern, callable $callback, bool $pass_route = false): void {
$this->map('GET ' . $pattern, $callback, $pass_route);
public function get(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): void {
$this->map('GET ' . $pattern, $callback, $pass_route, $alias);
}
/**
@ -101,10 +102,11 @@ class Router
* @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 Alias for the route
* @return void
*/
public function post(string $pattern, callable $callback, bool $pass_route = false): void {
$this->map('POST ' . $pattern, $callback, $pass_route);
public function post(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): void {
$this->map('POST ' . $pattern, $callback, $pass_route, $alias);
}
/**
@ -113,10 +115,11 @@ class Router
* @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 Alias for the route
* @return void
*/
public function put(string $pattern, callable $callback, bool $pass_route = false): void {
$this->map('PUT ' . $pattern, $callback, $pass_route);
public function put(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): void {
$this->map('PUT ' . $pattern, $callback, $pass_route, $alias);
}
/**
@ -125,10 +128,11 @@ class Router
* @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 Alias for the route
* @return void
*/
public function patch(string $pattern, callable $callback, bool $pass_route = false): void {
$this->map('PATCH ' . $pattern, $callback, $pass_route);
public function patch(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): void {
$this->map('PATCH ' . $pattern, $callback, $pass_route, $alias);
}
/**
@ -137,10 +141,11 @@ class Router
* @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 Alias for the route
* @return void
*/
public function delete(string $pattern, callable $callback, bool $pass_route = false): void {
$this->map('DELETE ' . $pattern, $callback, $pass_route);
public function delete(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): void {
$this->map('DELETE ' . $pattern, $callback, $pass_route, $alias);
}
/**

@ -193,4 +193,31 @@ class FlightTest extends PHPUnit\Framework\TestCase
$url = Flight::getUrl('path1', [ 'param' => 123 ]);
$this->assertEquals('/path1/123', $url);
}
public function testRouteGetUrlWithGroupSimpleParams() {
Flight::group('/path1/@id', function() {
Flight::route('/@name', function() { echo 'whatever'; }, false, 'path1');
});
$url = Flight::getUrl('path1', ['id' => 123, 'name' => 'abc']);
$this->assertEquals('/path1/123/abc', $url);
}
public function testRouteGetUrlNestedGroups() {
Flight::group('/user', function () {
Flight::group('/all_users', function () {
Flight::group('/check_user', function () {
Flight::group('/check_one', function () {
Flight::route("/normalpath", function () {
echo "normalpath";
},false,"normalpathalias");
});
});
});
});
$url = Flight::getUrl('normalpathalias');
$this->assertEquals('/user/all_users/check_user/check_one/normalpath', $url);
}
}

@ -541,4 +541,13 @@ class RouterTest extends PHPUnit\Framework\TestCase
$url = $this->router->getUrlByAlias('path1');
$this->assertEquals('/path1', $url);
}
public function testGetUrlByAliasWithGroupSimpleParams() {
$this->router->group('/path1/@id', function($router) {
$router->get('/@name', [$this, 'ok'], false, 'path1');
});
$url = $this->router->getUrlByAlias('path1', ['id' => 123, 'name' => 'abc']);
$this->assertEquals('/path1/123/abc', $url);
}
}

Loading…
Cancel
Save