Skip to content

Commit

Permalink
Update readme, add troubleshoot section
Browse files Browse the repository at this point in the history
  • Loading branch information
sudomaxime committed Apr 6, 2020
1 parent af28982 commit d23c115
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Easily write modern and reusable route middlewares for your Wordpress projects a
## Getting started
You need to have [Composer](https://getcomposer.org/) installed on your machine, [follow this link](https://getcomposer.org/doc/00-intro.md) for instructions on how to install Composer.

## Prerequisites
* You need to have PHP >= 5.5.0
* Wordpress >= 4.7.1

### Installing
The best way to install this library is with composer:
```bash
Expand Down Expand Up @@ -89,12 +93,14 @@ $myCustomAuthMiddleware = function ($req, $res) {
### Hook on an existing wordpress REST endpoint
You can also easily modify the response body of an existing wordpress endpoint with the `public hook()` method. Simply put, middlewares added to the `hook` handler will have a pre-filled `$response` parameter with the array that Wordpress would normally return to the client. You can easily modify the return response this way.

A hook request **MUST** end with a [WP_REST_Request](https://developer.wordpress.org/reference/classes/wp_rest_request/) class, it is possible to pass custom $request objects from middleware to middleware, however in order to make other plugins and other wordpress REST hooks work, you must respect this pattern.

note: *You do not need to put /wp-json in your endpoint address.*

```php
$router->hook('GET', '/wp/v2/posts/:id',
$authMiddleware,
$responseMiddleware
$responseMiddleware // this function ends by returning an instance of WP_REST_Response
);
```

Expand All @@ -115,6 +121,9 @@ $router = new Weblove\WPRouter\Router("custom"); // now set to /wp-json/custom
* `hook(string $method_verb, string $endpoint, middleware ...$middleware)` - Use an already existing Wordpress endpoint.
* **In development** `use(string $endpoint, middleware ...$middleware)` - Use middlewares on all verbs of that endpoint.

### Troubleshooting and frequent errors
* My endpoint is returning an error `Uncaught Error: Call to a member function get_matched_route()` - This means you have a hook that doesn't return an instance of WP_REST_Response after all the middlewares have been executed. See this guide section on how to properly hook on existing wordpress endpoints.

## Authors
* Maxime Nadeau - *initial work* - [Weblove](http://weblove.ca)

Expand Down

0 comments on commit d23c115

Please sign in to comment.