PHPackages                             shtrihstr/simple-rest-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. shtrihstr/simple-rest-api

ActiveLibrary[HTTP &amp; Networking](/categories/http)

shtrihstr/simple-rest-api
=========================

Simple WordPress REST API Router

1.0.0(10y ago)62.3k4MITPHPPHP ^5.5 || ^7.0

Since May 3Pushed 8y ago2 watchersCompare

[ Source](https://github.com/shtrihstr/simple-rest-api)[ Packagist](https://packagist.org/packages/shtrihstr/simple-rest-api)[ RSS](/packages/shtrihstr-simple-rest-api/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

Simple WordPress REST API Router
================================

[](#simple-wordpress-rest-api-router)

WordPress REST API Router that is really easy to use.

Installation
------------

[](#installation)

#### Composer

[](#composer)

```
$ composer require shtrihstr/simple-rest-api

```

#### Old way

[](#old-way)

Download archive from [releases section](https://github.com/shtrihstr/simple-rest-api/releases)

```
require_once '/path/to/Route.php';
require_once '/path/to/Router.php';
```

Usage
-----

[](#usage)

#### Create a Router

[](#create-a-router)

```
$router = new Simple_REST_API\Router( 'my-plugin/v1.0', [ 'etag' => true ] );
```

#### Example GET Route

[](#example-get-route)

Here is an example definition of a GET route:

```
$router->get( '/posts', function() {
    return get_posts();
} );
```

#### Dynamic Routing

[](#dynamic-routing)

Now you can create another controller for viewing individual blog posts:

```
$router->get( '/post/{id}', function( WP_REST_Response $response, $id ) {
    $post = get_post( $id );
    if( ! $post ) {
        $response->set_status( 404 );
    }
    else {
        $response->set_data( $post );
    }
    return $response;
} );
```

#### Example POST Route

[](#example-post-route)

POST routes signify the creation of a resource. An example for this is a feedback form.

```
$router->post( '/feedback', function( WP_REST_Request $request, WP_REST_Response $response ) {
    $body_params = $request->get_body_params();
    $message = esc_html( $body_params['message'] );

    wp_mail( 'feedback@yoursite.com', '[YourSite] Feedback', $message );

    $response->set_status( 201 );
    $response->set_data( 'Thank you for your feedback!' );
    return $response;
} );
```

#### Other methods

[](#other-methods)

You can create controllers for most HTTP methods.

```
$router->put( '/post/{id}', function( $id ) {
    // ...
} );

$router->delete( '/post/{id}', function( $id ) {
    // ...
} );

$router->patch( '/post/{id}', function( $id ) {
    // ...
} );
```

#### Route Variables

[](#route-variables)

As it has been shown before, you can define variable parts in a route like this:

```
$router->get( '/post/{id}', function( $id ) {
    // ...
} );
```

It is also possible to have more than one variable part, just make sure the closure arguments match the names of the variable parts:

```
$router->get( '/post/{post_id}/paged/{page_id}', function( $post_id, $page_id ) {
    // ...
} );
```

While it's not recommended, you could also do this (note the switched arguments):

```
$router->get( '/post/{post_id}/paged/{page_id}', function( $page_id, $post_id ) {
    // ...
} );
```

You can also ask for the current Request and Response objects:

```
$router->get( '/post/{id}', function( WP_REST_Request $request, WP_REST_Response $response, $id ) {
    // ...
} );
```

#### Route Variable Converters

[](#route-variable-converters)

Before injecting the route variables into the controller, you can apply some converters:

```
$router->get( '/post/{id}', function( $id ) {
    // ...
} )->convert( 'id', function( $id ) { return (int) $id; } );
```

This is useful when you want to convert route variables to objects:

```
$router->get( '/comments/{user}', function( $user ) {
    // ...
} )->convert( 'user', function( $user ) { return get_user_by( 'id', $user ); } );
```

#### Requirements

[](#requirements)

The following will make sure the id argument is a positive integer since \\d+ matches any amount of digits:

```
$router->get( '/post/{id}', function( $id ) {
    // ...
} )->assert( 'id', '\d+' );
```

#### Middlewares

[](#middlewares)

Route middlewares are added to routes and they are only triggered when the corresponding route is matched. You can also stack them:

```
$before_callback = function() {
    $GLOBALS['wpdb']->queries = [];
};

$after_callback = function( WP_REST_Response $response ) {
    $data = $response->get_data();
    if( is_array( $data ) ) {
        $data['debug'] = [
            'time' => timer_stop(),
            'queries' => $GLOBALS['wpdb']->queries,
        ];
        $response->set_data( $data );
    }
};

$router->get( '/post/{id}', function( $id ) {
    // ...
} )->before( $before_callback )->after( $after_callback );
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 75% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3658d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/08367c3bbdbb3f962506e4bf872828e506f533bec4200f19064c6b7f56a82cd7?d=identicon)[Shtrih](/maintainers/Shtrih)

---

Top Contributors

[![kuliebiakin](https://avatars.githubusercontent.com/u/6066592?v=4)](https://github.com/kuliebiakin "kuliebiakin (3 commits)")[![shtrihstr](https://avatars.githubusercontent.com/u/11991783?v=4)](https://github.com/shtrihstr "shtrihstr (1 commits)")

---

Tags

apiwordpressrest

### Embed Badge

![Health badge](/badges/shtrihstr-simple-rest-api/health.svg)

```
[![Health](https://phpackages.com/badges/shtrihstr-simple-rest-api/health.svg)](https://phpackages.com/packages/shtrihstr-simple-rest-api)
```

###  Alternatives

[pixelpeter/laravel5-woocommerce-api-client

Laravel 5 wrapper for the Woocommerce REST API

125103.4k](/packages/pixelpeter-laravel5-woocommerce-api-client)[inpsyde/wp-rest-starter

Starter package for working with the WordPress REST API in an object-oriented fashion.

1088.4k](/packages/inpsyde-wp-rest-starter)[threesquared/laravel-wp-api

Laravel package for the Wordpress JSON REST API

1310.3k](/packages/threesquared-laravel-wp-api)[dbout/wp-module-rest-api

Quickly add routes to the WordPress Rest API.

141.3k](/packages/dbout-wp-module-rest-api)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
