Skip to content

Commit 5b32d7c

Browse files
committed
docs: Update middlewares and plugins as dependencies
1 parent 5e00f49 commit 5b32d7c

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

docs/docs/advanced-user-guide/middlewares.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,47 @@ Another cool feature of WP-FastEndpoints is the support for middlewares.
22

33
Middlewares are pieces of code that can either run before and/or after a request is handled.
44

5-
At this stage, you might be already familiar with both the `schema(...)` and `returns(...)`
6-
middlewares. However, you can also create your own.
5+
At this stage, you might be already familiar with `returns(...)` which is a middleware.
6+
However, you can also create your own.
77

8-
```php
8+
```php hl_lines="10 18 26"
9+
<?php
910
use Attributes\Wp\FastEndpoints\Contracts\Middleware;
1011

1112
class MyCustomMiddleware extends Middleware
1213
{
1314
/**
14-
* Create this function if you want that your middleware is
15-
* triggered when it receives a request and after checking
16-
* the user permissions.
17-
*/
18-
public function onRequest(/* Type what you need */)
19-
{
20-
return;
15+
* This function is triggered before the main handler runs
16+
* but after checking the user permissions.
17+
*/
18+
public function onRequest(#(1))) {
19+
return; #(2)
2120
}
2221

2322
/**
24-
* Create this function when you want your middleware to be
25-
* triggered before sending a response to the client
26-
*/
27-
public function onResponse(/* Type what you need */) {
28-
return;
23+
* This function is triggered after the main handler,
24+
* before sending a response to the client
25+
*/
26+
public function onResponse(#(3)) {
27+
return; #(4)
2928
}
3029
}
3130

32-
// Attach middleware to endpoint
3331
$router->get('/test', function () {
3432
return true;
3533
})
3634
->middleware(new MyCustomMiddleware());
3735
```
3836

37+
1. Supports all features that a regular endpoint supports
38+
e.g. [injectables](/advanced-user-guide/dependency-injection/injectables),
39+
[typed request data](/advanced-user-guide/dependency-injection/request-payload)
40+
2. Early response return is also supported. See [Responses page](/advanced-user-guide/responses)
41+
3. Middlewares supports all features that a regular endpoint supports
42+
e.g. [injectables](/advanced-user-guide/dependency-injection/injectables),
43+
[typed request data](/advanced-user-guide/dependency-injection/request-payload)
44+
4. Early response return is also supported. See [Responses page](/advanced-user-guide/responses)
45+
3946
???+ tip
4047
You can create both methods in a middleware: `onRequest` and `onResponse`.
4148
However, to save some CPU cycles only create the one you need [CPU emoji]
42-
43-
## Responses
44-
45-
If you need you can also take advantage of either WP_Error and WP_REST_Response to send
46-
a direct response to the client. See [Responses page](/advanced-user-guide/responses)
47-
for more info

docs/docs/advanced-user-guide/plugins-as-dependencies.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Currently, we support both native WP endpoints and FastEndpoints 😊
2626
=== "With FastEndpoints"
2727

2828
```php
29+
<?php
2930
$router->get('/example/all-plugins', function () {
3031
return "Loads all active plugins";
3132
});
@@ -38,6 +39,7 @@ Currently, we support both native WP endpoints and FastEndpoints 😊
3839
=== "Native WP endpoints"
3940

4041
```php
42+
<?php
4143
// Loads all active plugins
4244
register_rest_route('native/v1', 'example/all-plugins', [
4345
'methods' => 'GET',
@@ -66,6 +68,7 @@ One common scenario where router dependencies might be useful is when we want to
6668
of loading all active plugins per endpoint.
6769

6870
```php
71+
<?php
6972
$router = new \Attributes\Wp\FastEndpoints\Router('my-api', 'v1');
7073
$router->depends(['my-plugin']); // All endpoints and sub-routers would have this dependency
7174
```

0 commit comments

Comments
 (0)