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
{{ message }}
This repository was archived by the owner on May 23, 2023. It is now read-only.
This type of Route Patterns contain dynamic parts which can vary per request. The varying parts are named __subpatterns__ and are defined using regular expressions.
107
+
This type of Route Pattern contains dynamic parts which can vary per request. The varying parts are named __subpatterns__ and are defined using regular expressions.
108
108
109
109
Examples:
110
110
@@ -121,7 +121,7 @@ Commonly used PCRE-based subpatterns within Dynamic Route Patterns are:
121
121
122
122
Note: The [PHP PCRE Cheat Sheet](https://courses.cs.washington.edu/courses/cse154/15sp/cheat-sheets/php-regex-cheat-sheet.pdf) might come in handy.
123
123
124
-
The __subpatterns__ defined in Dynamic PCRE-based Route Patterns are converted to parameters which are passed into the route handling function. Prerequisite is that these subpatterns need to be defined as __parenthesized subpatterns__, which means that they should be wrapped between parens:
124
+
The __subpatterns__ defined in Dynamic PCRE-based Route Patterns are converted to parameters that are passed into the route handling function. The prerequisite is that these subpatterns need to be defined as __parenthesized subpatterns__, which means that they should be wrapped between parenthesis:
Note: The leading `/` at the very beginning of a route pattern is not mandatory, but is recommended.
141
141
142
-
When multiple subpatterns are defined, the resulting __route handling parameters__ are passed into the route handling function in the order they are defined in:
142
+
When multiple subpatterns are defined, the resulting __route handling parameters__ are passed into the route handling function in the order they are defined:
This type of Route Patterns are the same as __Dynamic PCRE-based Route Patterns__, but with one difference: they don't use regexes to do the pattern matching but they use the more easy __placeholders__ instead. Placeholders are strings surrounded by curly braces, e.g. `{name}`. You don't need to add parens around placeholders.
153
+
This type of Route Pattern is the same as __Dynamic PCRE-based Route Patterns__, but with one difference: they don't use regexes to do the pattern matching but they use the more easy __placeholders__ instead. Placeholders are strings surrounded by curly braces, e.g. `{name}`. You don't need to add parens around placeholders.
The code snippet above responds to the URLs `/blog`, `/blog/year`, `/blog/year/month`, `/blog/year/month/day`, and `/blog/year/month/day/slug`.
213
213
214
-
Note: With optional parameters it is important that the leading `/` of the subpatterns is put inside the subpattern itself. Don't forget to set default values for the optional parameters.
214
+
Note: With optional parameters, it is important that the leading `/` of the subpatterns is put inside the subpattern itself. Don't forget to set default values for the optional parameters.
215
215
216
216
The code snipped above unfortunately also responds to URLs like `/blog/foo` and states that the overview needs to be shown - which is incorrect. Optional subpatterns can be made successive by extending the parenthesized subpatterns so that they contain the other optional subpatterns: The pattern should resemble `/blog(/year(/month(/day(/slug))))` instead of the previous `/blog(/year)(/month)(/day)(/slug)`:
217
217
```php
@@ -277,7 +277,7 @@ $router->map404("/(.*)", function ($req, $res) {
277
277
278
278
## 500 handler
279
279
280
-
Defining a 500 handler is **recommended** and is exactly same to mapping a 404 handler.
280
+
Defining a 500 handler is **recommended** and is exactly the same as mapping a 404 handler.
281
281
282
282
```php
283
283
$router->map500("/(.*)", function ($req, $res) {
@@ -291,34 +291,48 @@ $router->map500("/(.*)", function ($req, $res) {
291
291
292
292
Middleware is software that connects the model and view in an MVC application, facilitating the communication and data flow between these two components while also providing a layer of abstraction, decoupling the model and view and allowing them to interact without needing to know the details of how the other component operates.
293
293
294
-
A good example is having before middleware that makes sure the user is an administrator before they go to a restricted page. You could do this in your routes controller for every admin page but that would be redundant. Or for after middleware, you may have a REST API that returns a JSON response. You can have after middleware to make sure to make sure the JSON response isn't malformed.
294
+
A good example is having before middleware that makes sure the user is an administrator before they go to a restricted page. You could do this in your routes controller for every admin page but that would be redundant. Or for after middleware, you may have a REST API that returns a JSON response. You can have after middleware to make sure the JSON response isn't malformed.
295
295
296
296
## Before Middleware
297
297
298
-
You can define before middleware similar to a route by providing a method, pattern, and controller.
298
+
You can define before middleware similar to a route by providing a method, pattern, and controller. Do note that with beforeMiddleware you're expected to create an object that implements ResponseInterface to pass to the main route.
299
299
300
300
```php
301
-
use HttpSoft\Response\RedirectResponse;
302
-
303
-
$router->beforeMiddleware([METHOD::ALL], "/admin?(.*)", function ($req, $res) {
301
+
$router->beforeMiddleware("/admin?(.*)", function (ServerRequest $req, Closure $next) {
304
302
if (!isAdmin()) {
305
303
return new RedirectResponse("/", 403);
306
304
}
307
305
308
-
return $res;
306
+
return $next(new Response);
309
307
});
310
308
```
311
309
312
-
## After Middleware
310
+
## In the main route for after middleware
313
311
314
-
The only difference between defining after and before middleware is the method you use.
312
+
If the router detects after middleware then the third parameter will be a closure with similar functionality to beforeMiddleware
315
313
316
314
```php
317
-
use HttpSoft\Response\RedirectResponse;
315
+
$router->all("/admin/roster/json", function (ServerRequest $req, Response $res, Closure $next) {
316
+
/*
317
+
some code that gets the admin roster and converts to json
318
+
*/
318
319
319
-
$router->afterMiddleware([METHOD::ALL], "/admin?(.*)", function ($req, $res) {
0 commit comments