Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 15a7fc0

Browse files
committed
Updated middleware part of README
1 parent 9725895 commit 15a7fc0

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

README.md

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ $router->get('/about', function($req, $res) {
104104

105105
## Dynamic PCRE-based Route Patterns
106106

107-
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.
108108

109109
Examples:
110110

@@ -121,7 +121,7 @@ Commonly used PCRE-based subpatterns within Dynamic Route Patterns are:
121121

122122
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.
123123

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:
125125

126126
```php
127127
// Bad
@@ -139,7 +139,7 @@ $router->get('/hello/(\w+)', function($req, $res, $name) {
139139

140140
Note: The leading `/` at the very beginning of a route pattern is not mandatory, but is recommended.
141141

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:
143143

144144
```php
145145
$router->get('/movies/(\d+)/photos/(\d+)', function($req, $res, $movieId, $photoId) {
@@ -150,7 +150,7 @@ $router->get('/movies/(\d+)/photos/(\d+)', function($req, $res, $movieId, $photo
150150

151151
## Dynamic Placeholder-based Route Patterns
152152

153-
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.
154154

155155
Examples:
156156

@@ -166,7 +166,7 @@ $router->get('/movies/{movieId}/photos/{photoId}', function($req, $res, $movieId
166166
});
167167
```
168168

169-
Note: the name of the placeholder does not need to match with the name of the parameter that is passed into the route handling function:
169+
Note: the name of the placeholder does not need to match the name of the parameter that is passed into the route handling function:
170170

171171
```php
172172
$router->get('/movies/{foo}/photos/{bar}', function($req, $res, $movieId, $photoId) {
@@ -211,7 +211,7 @@ $router->get(
211211

212212
The code snippet above responds to the URLs `/blog`, `/blog/year`, `/blog/year/month`, `/blog/year/month/day`, and `/blog/year/month/day/slug`.
213213

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.
215215

216216
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)`:
217217
```php
@@ -277,7 +277,7 @@ $router->map404("/(.*)", function ($req, $res) {
277277

278278
## 500 handler
279279

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.
281281

282282
```php
283283
$router->map500("/(.*)", function ($req, $res) {
@@ -291,34 +291,48 @@ $router->map500("/(.*)", function ($req, $res) {
291291

292292
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.
293293

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.
295295

296296
## Before Middleware
297297

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.
299299

300300
```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) {
304302
if (!isAdmin()) {
305303
return new RedirectResponse("/", 403);
306304
}
307305

308-
return $res;
306+
return $next(new Response);
309307
});
310308
```
311309

312-
## After Middleware
310+
## In the main route for after middleware
313311

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
315313

316314
```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+
*/
318319

319-
$router->afterMiddleware([METHOD::ALL], "/admin?(.*)", function ($req, $res) {
320-
if (!isAdmin()) {
321-
return new RedirectResponse("/", 403);
320+
$res->getBody()->write($jsonString);
321+
$res->withHeader("content-type", "application/json");
322+
323+
return $next($res);
324+
});
325+
```
326+
## After Middleware
327+
328+
```php
329+
$router->afterMiddleware("/admin?(.*)", function (ServerRequest $req, ResponseInterface $res) {
330+
/*
331+
some code that makes sure the body is a valid json string
332+
*/
333+
334+
if (!jsonValid((string)$res->getBody())) {
335+
return new EmptyResponse(500);
322336
}
323337

324338
return $res;

0 commit comments

Comments
 (0)