mirror of
https://github.com/notrab/dumbo.git
synced 2025-01-16 21:58:25 +01:00
fix nested route middleware
This commit is contained in:
parent
c2d6c9e83a
commit
61892adc06
@ -108,6 +108,10 @@ $app->use(function($c, $next) {
|
||||
|
||||
### Bearer Auth
|
||||
|
||||
```bash
|
||||
curl -H 'Authorization: Bearer mysupersecret' http://localhost:8000/api
|
||||
```
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
|
@ -11,13 +11,7 @@ $user = new Dumbo();
|
||||
$protectedRoutes = new Dumbo();
|
||||
$token = "mysupersecret";
|
||||
|
||||
$protectedRoutes->use(BearerAuth::bearer($token));
|
||||
|
||||
$protectedRoutes->get("/", function ($c) {
|
||||
return $c->json(["message" => "Welcome to the protected routes!"]);
|
||||
});
|
||||
|
||||
$app->route("/api", $protectedRoutes);
|
||||
$app->use(BearerAuth::bearer($token));
|
||||
|
||||
$userData = [
|
||||
[
|
||||
|
@ -68,6 +68,16 @@ class Dumbo
|
||||
$this->middleware[] = $middleware;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the middleware stack
|
||||
*
|
||||
* @return array The middleware stack
|
||||
*/
|
||||
public function getMiddleware(): array
|
||||
{
|
||||
return $this->middleware;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a nested route to the application
|
||||
*
|
||||
@ -77,10 +87,16 @@ class Dumbo
|
||||
public function route(string $prefix, self $nestedApp): void
|
||||
{
|
||||
foreach ($nestedApp->router->getRoutes() as $route) {
|
||||
$combinedMiddleware = array_merge(
|
||||
$this->middleware,
|
||||
$nestedApp->getMiddleware()
|
||||
);
|
||||
|
||||
$this->router->addRoute(
|
||||
$route["method"],
|
||||
$prefix . $route["path"],
|
||||
$route["handler"]
|
||||
$route["handler"],
|
||||
$combinedMiddleware
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -101,7 +117,17 @@ class Dumbo
|
||||
$route["params"],
|
||||
$route["routePath"]
|
||||
);
|
||||
$response = $this->runMiddleware($context, $route["handler"]);
|
||||
|
||||
$combinedMiddleware = array_merge(
|
||||
$this->middleware,
|
||||
$route["middleware"] ?? []
|
||||
);
|
||||
|
||||
$response = $this->runMiddleware(
|
||||
$context,
|
||||
$route["handler"],
|
||||
$combinedMiddleware
|
||||
);
|
||||
|
||||
return $response instanceof ResponseInterface
|
||||
? $response
|
||||
@ -122,6 +148,11 @@ class Dumbo
|
||||
$this->send($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a server request from the PHP globals
|
||||
*
|
||||
* @return ServerRequestInterface The server request
|
||||
*/
|
||||
private function createServerRequestFromGlobals(): ServerRequestInterface
|
||||
{
|
||||
$request = ServerRequest::fromGlobals();
|
||||
|
@ -6,6 +6,12 @@ use Dumbo\Context;
|
||||
|
||||
class BearerAuth
|
||||
{
|
||||
/**
|
||||
* Create a middleware that checks for a Bearer token in the Authorization header
|
||||
*
|
||||
* @param string $token The expected Bearer token
|
||||
* @return callable The middleware
|
||||
*/
|
||||
public static function bearer(string $token): callable
|
||||
{
|
||||
return function (Context $ctx, callable $next) use ($token) {
|
||||
|
@ -6,21 +6,31 @@ use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class Router
|
||||
{
|
||||
/** @var array<array{method: string, path: string, handler: callable(Context): (ResponseInterface|null)}> */
|
||||
/** @var array<array{method: string, path: string, handler: callable(Context): (ResponseInterface|null), middleware: array}> */
|
||||
private $routes = [];
|
||||
|
||||
/** @var string */
|
||||
private $prefix = "";
|
||||
|
||||
/**
|
||||
* Add a route to the router
|
||||
*
|
||||
* @param string $method The HTTP method for this route
|
||||
* @param string $path The path for this route
|
||||
* @param callable(Context): (ResponseInterface|null) $handler The handler function for this route
|
||||
* @param array $middleware Array of middleware functions for this route
|
||||
*/
|
||||
public function addRoute(
|
||||
string $method,
|
||||
string $path,
|
||||
callable $handler
|
||||
callable $handler,
|
||||
array $middleware = []
|
||||
): void {
|
||||
$this->routes[] = [
|
||||
"method" => $method,
|
||||
"path" => $this->prefix . $path,
|
||||
"handler" => $handler,
|
||||
"middleware" => $middleware,
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user