From 225f43d7481a28202a06b314e44d8cebde7d6cb5 Mon Sep 17 00:00:00 2001 From: Igor Ilic Date: Tue, 24 Aug 2021 09:46:03 +0200 Subject: [PATCH] Fixes #9 --- README.md | 31 +++++++++++++++++++++++++++++++ Routes.php | 4 +--- sample/index.php | 2 +- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 51e1f01..7677273 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,37 @@ try { } ``` +## Examples + +Using chained method to wrap multiple routes with a same middleware or a route prefix + +```php +$routes + ->prefix('/user') // all the routes add will have the /user prefix + ->middleware([ 'verify_token' ]) // all the routes added will have the verify_token middelware applied + ->route('/', [ HomeController::class, 'getUsers' ], Routes::GET) + ->route('/', [ HomeController::class, 'addUser' ], Routes::POST) + ->route('/', [ HomeController::class, 'updateUser' ], Routes::PATCH) + ->route('/', [ HomeController::class, 'replaceUser' ], Routes::PUT) + ->add('/test', [ HomeController::class, 'deleteUser' ], Routes::DELETE); +``` + +Dynamic routes example: + +```php +$routes->add('/test/{int:userID}-{username}/{float:amount}/{bool:valid}', function ( + Request $request, + int $userID, + string $username, + float $amount, + bool $valid +) { + echo 'Dynamic route content here'; +}); +``` + +For more example look in the [sample folder](/sample). + ## Features * [x] Static routes diff --git a/Routes.php b/Routes.php index 5b37f3e..871665c 100644 --- a/Routes.php +++ b/Routes.php @@ -156,9 +156,7 @@ public function route(string $path, callable|array|string $callback, string|arra */ public function add(string $path = '', callable|array|string|null $callback = NULL, string|array|null $methods = self::GET) { - if ( empty($this->tmpRoutes) ) { - $this->route($path, $callback, $methods); - } + $this->route($path, $callback, $methods); foreach ( $this->tmpRoutes as $method => $route ) { if ( !isset($this->routes[$method]) ) $this->routes[$method] = []; diff --git a/sample/index.php b/sample/index.php index 6a71654..27bffb2 100644 --- a/sample/index.php +++ b/sample/index.php @@ -29,7 +29,7 @@ ->route('/', [ HomeController::class, 'addUser' ], Routes::POST) ->route('/', [ HomeController::class, 'updateUser' ], Routes::PATCH) ->route('/', [ HomeController::class, 'replaceUser' ], Routes::PUT) - ->add('/', [ HomeController::class, 'deleteUser' ], Routes::DELETE); + ->add('/test', [ HomeController::class, 'deleteUser' ], Routes::DELETE); $routes->add('/test', function (Request $request) { $request