Skip to content

Commit 0b537a7

Browse files
πŸ“˜ Migrate doc to wiki
1 parent b17c3f6 commit 0b537a7

File tree

4 files changed

+1
-459
lines changed

4 files changed

+1
-459
lines changed

β€ŽREADME.md

+1-119
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ WordPress module designed for developers that want to add routes to the [WordPre
1010

1111
If you want to know more about how the WordPress API works, you can [read the WordPress documentation](https://developer.wordpress.org/rest-api/) :)
1212

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).
1814

1915
## Installation
2016

@@ -39,120 +35,6 @@ In your PHP script, make sure you include the autoloader:
3935
require __DIR__ . '/vendor/autoload.php';
4036
~~~
4137

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 :
115-
116-
```php
117-
#[Action([Method::GET, Method::POST, Method::PUT])]
118-
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-
15638
## Contributing
15739

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

β€Ždoc/error-handling.md

-125
This file was deleted.

β€Ždoc/options.md

-80
This file was deleted.

0 commit comments

Comments
Β (0)