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
Middleware to normalize the trailing slash of the uri path. By default removes the slash so, for example, `/post/23/` is converted to `/post/23`. Useful if you have problems with the router.
11
10
12
11
## Requirements
13
12
14
-
* PHP >= 7.0
13
+
* PHP >= 7.2
15
14
* A [PSR-7 http library](https://github.com/middlewares/awesome-psr15-middlewares#psr-7-implementations)
16
15
* A [PSR-15 middleware dispatcher](https://github.com/middlewares/awesome-psr15-middlewares#dispatcher)
17
16
@@ -34,19 +33,37 @@ $dispatcher = new Dispatcher([
By default, this middleware removes the trailing slash of the uri path. Set `true` to the constructor's first argument to add instead remove:
40
39
41
-
Set `true` to add the slash instead remove so, for example, `post/23` is converted to `/post/23/`. Note that if the path contains an extension, the slash is **NOT** added. For example, `images/image.png` remains the same, instead be converted to `images/image.png/`.
40
+
```php
41
+
//Removes the slash, so /post/23/ is converted to /post/23
42
+
$slash = new Middlewares\TrailinSlash();
43
+
44
+
//Force the slash, so /post/23 is converted to /post/23/
45
+
$slash = new Middlewares\TrailinSlash(true);
46
+
```
42
47
43
-
#### `redirect(true)`
48
+
Of course, if the path contains an extension, the slash is **NOT** added. For example, `images/image.png` remains the same, instead be converted to `images/image.png/`.
44
49
45
-
Set this option to return a `301` response redirecting to the new path
If the path must be converted, this option returns a `301` response redirecting to the new path. Optionally, you can provide a `Psr\Http\Message\ResponseFactoryInterface` that will be used to create the redirect response. If it's not defined, [Middleware\Utils\Factory](https://github.com/middlewares/utils#factory) will be used to detect it automatically.
53
+
54
+
```php
55
+
$responseFactory = new MyOwnResponseFactory();
56
+
57
+
//Simply removes the slash
58
+
$slash = new Middlewares\TrailinSlash();
59
+
60
+
//Returns a redirect response to the new path
61
+
$slash = (new Middlewares\TrailinSlash())->redirect();
62
+
63
+
//Returns a redirect response to the new path using a specific response factory
64
+
$slash = (new Middlewares\TrailinSlash())->redirect($responseFactory);
65
+
```
48
66
49
-
A PSR-17 factory to create `301` responses.
50
67
---
51
68
52
69
Please see [CHANGELOG](CHANGELOG.md) for more information about recent changes and [CONTRIBUTING](CONTRIBUTING.md) for contributing details.
@@ -58,10 +75,8 @@ The MIT License (MIT). Please see [LICENSE](LICENSE) for more information.
0 commit comments