Skip to content

Commit 5c55562

Browse files
Merge pull request #27 from dimitriBouteille/develop
📦 Release 1.2.0
2 parents 6d8d572 + 73ee32b commit 5c55562

23 files changed

+109
-509
lines changed

.php-cs-fixer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
'no_unused_imports' => true,
4343
'align_multiline_comment' => true,
4444
'array_indentation' => true,
45+
'blank_line_after_opening_tag' => false,
4546
'header_comment' => [
4647
'header' => $header,
4748
'comment_type' => 'PHPDoc',

README.md

Lines changed: 1 addition & 119 deletions
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.

composer.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@
3434
}
3535
},
3636
"require-dev": {
37-
"friendsofphp/php-cs-fixer": "^3.57",
38-
"phpstan/phpstan": "^1.11",
39-
"rector/rector": "^1.1",
4037
"phpunit/phpunit": "^10.5",
41-
"phpstan/phpstan-phpunit": "^1.3",
42-
"phpstan/extension-installer": "^1.3",
43-
"szepeviktor/phpstan-wordpress": "^1.3",
44-
"roots/wordpress": "^6.5"
38+
"phpstan/phpstan": "^2.0",
39+
"rector/rector": "^2.0",
40+
"friendsofphp/php-cs-fixer": "^3.69",
41+
"phpstan/extension-installer": "^1.4",
42+
"szepeviktor/phpstan-wordpress": "^2.0",
43+
"phpstan/phpstan-phpunit": "^2.0",
44+
"roots/wordpress": "^6.7"
4545
},
4646
"config": {
4747
"allow-plugins": {
48-
"phpstan/extension-installer": true,
49-
"roots/wordpress-core-installer": true
48+
"roots/wordpress-core-installer": true,
49+
"phpstan/extension-installer": true
5050
}
5151
},
5252
"extra": {

doc/error-handling.md

Lines changed: 0 additions & 125 deletions
This file was deleted.

doc/options.md

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)