You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Flight::getUrl('user_view', [ 'id' => 5 ]); // will return '/users/5'
410
410
```
411
411
412
+
## Route Middleware
413
+
Flight supports route and group route middleware. Middleware is a function that is executed before (or after) the route callback. This is a great way to add API authentication checks in your code, or to validate that the user has permission to access the route.
414
+
415
+
Here's a basic example:
416
+
417
+
```php
418
+
// If you only supply an anonymous function, it will be executed before the route callback.
419
+
// there are no "after" middleware functions except for classes (see below)
420
+
Flight::route('/path', function() { echo ' Here I am!'; })->addMiddleware(function() {
421
+
echo 'Middleware first!';
422
+
});
423
+
424
+
Flight::start();
425
+
426
+
// This will output "Middleware first! Here I am!"
427
+
```
428
+
429
+
There are some very important notes about middleware that you should be aware of before you use them:
430
+
- Middleware functions are executed in the order they are added to the route. The execution is similar to how [Slim Framework handles this](https://www.slimframework.com/docs/v4/concepts/middleware.html#how-does-middleware-work).
431
+
- Befores are executed in the order added, and Afters are executed in reverse order.
432
+
- If your middleware function returns false, all execution is stopped and a 403 Forbidden error is thrown. You'll probably want to handle this more gracefully with a `Flight::redirect()` or something similar.
433
+
- If you need parameters from your route, they will be passed in a single array to your middleware function. (`function($params) { ... }` or `public function before($params) {}`). The reason for this is that you can structure your parameters into groups and in some of those groups, your parameters may actually show up in a different order which would break the middleware function by referring to the wrong parameter. This way, you can access them by name instead of position.
434
+
435
+
### Middleware Classes
436
+
437
+
Middleware can be registered as a class as well. If you need the "after" functionality, you must use a class.
438
+
439
+
```php
440
+
class MyMiddleware {
441
+
public function before($params) {
442
+
echo 'Middleware first!';
443
+
}
444
+
445
+
public function after($params) {
446
+
echo 'Middleware last!';
447
+
}
448
+
}
449
+
450
+
$MyMiddleware = new MyMiddleware();
451
+
Flight::route('/path', function() { echo ' Here I am! '; })->addMiddleware($MyMiddleware); // also ->addMiddleware([ $MyMiddleware, $MyMiddleware2 ]);
452
+
453
+
Flight::start();
454
+
455
+
// This will display "Middleware first! Here I am! Middleware last!"
456
+
```
457
+
458
+
### Middleware Groups
459
+
460
+
You can add a route group, and then every route in that group will have the same middleware as well. This is useful if you need to group a bunch of routes by say an Auth middleware to check the API key in the header.
Copy file name to clipboardExpand all lines: flight/Engine.php
+64-13Lines changed: 64 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@
10
10
11
11
namespaceflight;
12
12
13
+
useClosure;
13
14
useErrorException;
14
15
useException;
15
16
useflight\core\Dispatcher;
@@ -19,6 +20,7 @@
19
20
useflight\net\Router;
20
21
useflight\template\View;
21
22
useThrowable;
23
+
useflight\net\Route;
22
24
23
25
/**
24
26
* The Engine class contains the core functionality of the framework.
@@ -32,12 +34,12 @@
32
34
* @method void halt(int $code = 200, string $message = '') Stops processing and returns a given response.
33
35
*
34
36
* Routing
35
-
* @method void route(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a URL to a callback function with all applicable methods
36
-
* @method void group(string $pattern, callable $callback) Groups a set of routes together under a common prefix.
37
-
* @method void post(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a POST URL to a callback function.
38
-
* @method void put(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a PUT URL to a callback function.
39
-
* @method void patch(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a PATCH URL to a callback function.
40
-
* @method void delete(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a DELETE URL to a callback function.
37
+
* @method Route route(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a URL to a callback function with all applicable methods
38
+
* @method void group(string $pattern, callable $callback, array $group_middlewares = []) Groups a set of routes together under a common prefix.
39
+
* @method Route post(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a POST URL to a callback function.
40
+
* @method Route put(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a PUT URL to a callback function.
41
+
* @method Route patch(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a PATCH URL to a callback function.
42
+
* @method Route delete(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a DELETE URL to a callback function.
41
43
* @method Router router() Gets router
42
44
* @method string getUrl(string $alias) Gets a url from an alias
43
45
*
@@ -375,6 +377,7 @@ public function _start(): void
375
377
ob_start();
376
378
377
379
// Route the request
380
+
$failed_middleware_check = false;
378
381
while ($route = $router->route($request)) {
379
382
$params = array_values($route->params);
380
383
@@ -383,11 +386,55 @@ public function _start(): void
0 commit comments