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
@@ -102,31 +102,17 @@ Route includes a `League\Route\Cache\FileCache` implementation that stores the c
102
102
103
103
$cache = new League\Route\Cache\FileCache(
104
104
'/path/to/cache/file.cache',
105
-
$ttl = 86400 // Time-to-live in seconds (optional, default: 86400)
105
+
86400
106
106
);
107
107
108
108
$cachedRouter = new League\Route\Cache\Router($builder, $cache);
109
109
~~~
110
110
111
-
The FileCache requires a writable directory and will automatically create the cache file.
111
+
The FileCache requires a writable directory and will automatically create the cache file. The second argument is the TTL in seconds.
112
112
113
113
### PSR-16 Compatible Stores
114
114
115
-
You can use any PSR-16 simple cache implementation, such as:
116
-
117
-
- Redis (via redis-adapter/cache)
118
-
- Memcached
119
-
- APCu
120
-
- Any custom implementation
121
-
122
-
~~~php
123
-
<?php declare(strict_types=1);
124
-
125
-
// Example with league/container's PSR-16 adapter
126
-
$cache = new SomeRedisCache();
127
-
128
-
$cachedRouter = new League\Route\Cache\Router($builder, $cache);
129
-
~~~
115
+
Any PSR-16 compatible cache implementation will work. Browse available implementations at [Packagist](https://packagist.org/providers/psr/simple-cache-implementation).
130
116
131
117
## Cache Invalidation
132
118
@@ -141,13 +127,21 @@ This means you don't need to manually clear the cache when routes change. It hap
141
127
142
128
### Manual Cache Clearing
143
129
144
-
If you need to manually clear the cache (for example, during deployment or testing), you can delete the cache file or use your cache store's `clear()` method:
130
+
If you need to manually clear the cache (for example, during deployment or testing), you can use your cache store's`delete()` or`clear()` method:
145
131
146
132
~~~php
147
-
<?php declare(straight_types=1);
133
+
<?php declare(strict_types=1);
148
134
149
-
$cache->delete('route'); // Clear the default cache key
150
-
$cache->delete('my-custom-key'); // Clear a custom cache key
135
+
$cache->clear();
136
+
~~~
137
+
138
+
If you are using a PSR-16 store with named keys, use `delete()` with the matching key:
139
+
140
+
~~~php
141
+
<?php declare(strict_types=1);
142
+
143
+
$cache->delete('league/route/cache');
144
+
$cache->delete('my-custom-key');
151
145
~~~
152
146
153
147
### Handling Corruption
@@ -161,28 +155,25 @@ Both the standard `Router` and the `Cache\Router` implement the new `RouterInter
161
155
Type-hint against `RouterInterface` in your dependency injection container:
162
156
163
157
~~~php
164
-
<?php declare(straight_types=1);
158
+
<?php declare(strict_types=1);
165
159
166
160
use League\Route\RouterInterface;
167
161
168
162
$container = new League\Container\Container;
169
163
170
-
// Use the standard router
171
164
$container->add(
172
165
RouterInterface::class,
173
166
League\Route\Router::class
174
167
);
175
168
176
-
// Or use the cached router instead
177
169
$container->add(
178
170
RouterInterface::class,
179
171
function (): RouterInterface {
180
172
return new League\Route\Cache\Router(
181
173
function (League\Route\Router $router): League\Route\Router {
182
-
// Register your routes here
183
174
return $router;
184
175
},
185
-
new League\Route\Cache\FileCache('/tmp/route.cache')
176
+
new League\Route\Cache\FileCache('/tmp/route.cache', 86400)
186
177
);
187
178
}
188
179
);
@@ -191,7 +182,7 @@ $container->add(
191
182
This enables you to switch between routers based on environment or configuration:
Both `League\Route\Router` and `League\Route\Cache\Router` implement `League\Route\RouterInterface`. You can bind this interface in your container so that any service type-hinting against `RouterInterface` will receive the correct implementation:
69
+
70
+
~~~php
71
+
<?php declare(strict_types=1);
72
+
73
+
use League\Route\RouterInterface;
74
+
75
+
$container = new League\Container\Container;
76
+
77
+
$container->add(RouterInterface::class, function () use ($container): RouterInterface {
78
+
$strategy = (new League\Route\Strategy\ApplicationStrategy)->setContainer($container);
79
+
$router = (new League\Route\Router)->setStrategy($strategy);
Copy file name to clipboardExpand all lines: docs/unstable/http.md
+19Lines changed: 19 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -66,6 +66,25 @@ See more about controllers [here](/unstable/controllers).
66
66
67
67
Route does not provide any functionality for dealing with globals such as `$_GET`, `$_POST` etc, this is all handled by your [PSR-7](https://www.php-fig.org/psr/psr-7/) implementation, please refer to that documentation for details on how to interact with input on the request object.
68
68
69
+
### Route Attributes
70
+
71
+
When a route is matched, Route sets the route variables (wildcard segments and defaults from `setVars()`) as PSR-7 request attributes. This means you can retrieve them either from the `$args` array passed to your controller or directly from the request:
72
+
73
+
~~~php
74
+
<?php declare(strict_types=1);
75
+
76
+
use Psr\Http\Message\ResponseInterface;
77
+
use Psr\Http\Message\ServerRequestInterface;
78
+
79
+
$router = new League\Route\Router;
80
+
81
+
$router->map('GET', '/user/{id}', function (ServerRequestInterface $request, array $args): ResponseInterface {
82
+
$idFromArgs = $args['id'];
83
+
$idFromRequest = $request->getAttribute('id');
84
+
// ...
85
+
});
86
+
~~~
87
+
69
88
## The Response
70
89
71
90
Because Route is built around PSR-15, this means that middleware and controllers are handled in a [single pass](https://www.php-fig.org/psr/psr-15/meta/#52-single-pass-lambda) approach. What this means in practice is that all middleware is passed a request object but is expected to build and return its own response or pass off to the next middleware in the stack for that to create one. Any controller that is dispatched via Route is wrapped in a middleware that adheres to this.
Copy file name to clipboardExpand all lines: docs/unstable/middleware.md
+30-12Lines changed: 30 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,7 @@ sections:
5
5
Introduction: introduction
6
6
Example Middleware: example-middleware
7
7
Defining Middleware: defining-middleware
8
+
Lazy Middleware: lazy-middleware
8
9
Middleware Order: middleware-order
9
10
Route as a Middleware: route-as-a-middleware
10
11
---
@@ -35,18 +36,10 @@ class AuthMiddleware implements MiddlewareInterface
35
36
{
36
37
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
37
38
{
38
-
// determine authentication and/or authorisation
39
-
// ...
40
-
41
-
// if user has auth, use the request handler to continue to the next
42
-
// middleware and ultimately reach your route callable
43
39
if ($auth === true) {
44
40
return $handler->handle($request);
45
41
}
46
42
47
-
// if user does not have auth, possibly return a redirect response,
48
-
// this will not continue to any further middleware and will never
49
-
// reach your route callable
50
43
return new RedirectResponse(/* .. */);
51
44
}
52
45
}
@@ -99,6 +92,34 @@ $router
99
92
;
100
93
~~~
101
94
95
+
## Lazy Middleware
96
+
97
+
If you are using a PSR-11 dependency injection container, you can register middleware by class name using `lazyMiddleware()`. The middleware will be resolved from the container (or instantiated directly) at dispatch time, rather than upfront:
These lazy variants are available on the router, route groups, and individual routes, mirroring the eager `middleware()` methods.
122
+
102
123
## Middleware Order
103
124
104
125
Middleware is invoked in a specific order but depending on the logic contained in a middleware, you can control whether your code is run before or after your controller is invoked.
@@ -126,17 +147,14 @@ class SomeMiddleware implements MiddlewareInterface
126
147
{
127
148
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
128
149
{
129
-
// invoke the rest of the middleware stack and your controller resulting
130
-
// in a returned response object
131
150
$response = $handler->handle($request);
132
151
133
152
// ...
134
-
// do something with the response
135
153
return $response;
136
154
}
137
155
}
138
156
~~~
139
157
140
158
## Route as a Middleware
141
159
142
-
League\Route is itself a Request Handler, so an instance of `League\Route\Router` can be added to any existing middleware stack.
160
+
`League\Route\Router` implements `League\Route\RouterInterface`, which extends PSR-15's `RequestHandlerInterface`. This means an instance of `League\Route\Router` can be added to any existing middleware stack as a request handler.
0 commit comments