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
Copy file name to clipboardExpand all lines: README.md
+1-119
Original file line number
Diff line number
Diff line change
@@ -10,11 +10,7 @@ WordPress module designed for developers that want to add routes to the [WordPre
10
10
11
11
If you want to know more about how the WordPress API works, you can [read the WordPress documentation](https://developer.wordpress.org/rest-api/) :)
12
12
13
-
-[Installation](#installation)
14
-
-[Basic usage](#usage)
15
-
-[Error handling](/doc/error-handling.md)
16
-
-[Permission](/doc/permission.md)
17
-
-[Route loader options](doc/options.md)
13
+
You can find all the documentation in the [wiki](https://github.com/dimitriBouteille/wp-module-rest-api/wiki).
18
14
19
15
## Installation
20
16
@@ -39,120 +35,6 @@ In your PHP script, make sure you include the autoloader:
39
35
require __DIR__ . '/vendor/autoload.php';
40
36
~~~
41
37
42
-
## Usage
43
-
44
-
Before creating your first route, you must initialize the module. It is advisable to add this code at the beginning of the `functions.php` file of your theme or in a `mu-plugin`.
45
-
46
-
```php
47
-
use Dbout\WpRestApi\RouteLoader;
48
-
49
-
// One folder
50
-
$loader = new RouteLoader(__DIR__ . '/src/Api/Routes');
51
-
52
-
// Multiple folders
53
-
$loader = new RouteLoader([
54
-
__DIR__ . '/themes/my-theme/api'
55
-
__DIR__ . '/src/Api/Routes',
56
-
]);
57
-
58
-
// You can also use pattern
59
-
$loader = new RouteLoader(__DIR__ . '/src/Modules/*/Api/Routes');
60
-
61
-
$loader->register();
62
-
```
63
-
64
-
> π‘ The module will automatically search for all classes that are in the folder and sub folder.
65
-
66
-
> π‘ You can pass as the second argument of RouteLoader an option object: [read the documentation](doc/options.md).
67
-
68
-
Now you have initialized the module, you just need to create your first route in the routes folder.
69
-
70
-
```php
71
-
<?php
72
-
73
-
use Dbout\WpRestApi\Attributes\Route;
74
-
use Dbout\WpRestApi\Attributes\Action;
75
-
76
-
#[Route(
77
-
namespace: 'app/v2',
78
-
route: 'document/(?P<documentId>\d+)'
79
-
)]
80
-
class Document
81
-
{
82
-
83
-
#[Action(Method::GET)]
84
-
public function get(\WP_REST_Request $request): \WP_REST_Response
85
-
{
86
-
// Add your logic
87
-
$id = $request->get_param('documentId');
88
-
89
-
return new \WP_REST_Response([
90
-
'success' => true,
91
-
]);
92
-
}
93
-
94
-
#[Action(Method::DELETE)]
95
-
public function delete(\WP_REST_Request $request): \WP_REST_Response
96
-
{
97
-
// Add your logic
98
-
$id = $request->get_param('documentId');
99
-
100
-
return new \WP_REST_Response([
101
-
'success' => true,
102
-
]);
103
-
}
104
-
}
105
-
```
106
-
107
-
You just created 2 routes π
108
-
109
-
-`GET:wp-json/app/v2/document/18`
110
-
-`DELETE:wp-json/app/v2/document/18`
111
-
112
-
The logic is extremely simple, you can use the following methods: `GET`, `POST`, `PUT`, `PATCH` and `DELETE`
113
-
114
-
If you need, you can define multiple methods for an action by passing a method array :
public function execute(\WP_REST_Request $request): \WP_REST_Response
119
-
{
120
-
// Add your logic
121
-
122
-
}
123
-
```
124
-
125
-
### Callback arguments
126
-
127
-
If your route contains parameters, you can retrieve them as an argument for your function :
128
-
129
-
```php
130
-
<?php
131
-
132
-
use Dbout\WpRestApi\Attributes\Route;
133
-
use Dbout\WpRestApi\Attributes\Action;
134
-
135
-
#[Route(
136
-
'app/v2',
137
-
'document/(?P<documentId>\d+)'
138
-
)]
139
-
class Document
140
-
{
141
-
142
-
#[Action(Method::GET)]
143
-
public function get(int $documentId): \WP_REST_Response
144
-
{
145
-
// Add your logic
146
-
147
-
return new \WP_REST_Response([
148
-
'success' => true,
149
-
]);
150
-
}
151
-
}
152
-
```
153
-
154
-
> π‘If your function contains a `WP_REST_Request` argument, the [WP_REST_Request](https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/#arguments) object will be passed as an argument.
155
-
156
38
## Contributing
157
39
158
40
We encourage you to contribute to this repository, so everyone can benefit from new features, bug fixes, and any other improvements. Have a look at our [contributing guidelines](CONTRIBUTING.md) to find out how to raise a pull request.
0 commit comments