Skip to content

Commit

Permalink
Fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
gigili committed Aug 24, 2021
1 parent dcb6e8a commit 225f43d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [];
Expand Down
2 changes: 1 addition & 1 deletion sample/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 225f43d

Please sign in to comment.