@@ -2,46 +2,47 @@ Another cool feature of WP-FastEndpoints is the support for middlewares.
22
33Middlewares 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
910use Attributes\Wp\FastEndpoints\Contracts\Middleware;
1011
1112class 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
0 commit comments