PHPackages                             pinkcrab/perique-route - 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. pinkcrab/perique-route

ActiveLibrary

pinkcrab/perique-route
======================

WP REST Endpoint creation library for the Perique Framework.

2.1.0(4mo ago)3847↓100%[5 PRs](https://github.com/Pink-Crab/Perique-Route/pulls)MITPHPPHP &gt;=7.4.0CI passing

Since Jul 5Pushed 4mo agoCompare

[ Source](https://github.com/Pink-Crab/Perique-Route)[ Packagist](https://packagist.org/packages/pinkcrab/perique-route)[ Docs](https://pinkcrab.co.uk)[ RSS](/packages/pinkcrab-perique-route/feed)WikiDiscussions master Synced today

READMEChangelog (8)Dependencies (21)Versions (29)Used By (0)

[![logo](docs/Route.jpg "PinkCrab Route Module")](docs/Route.jpg)

Perique - Route
===============

[](#perique---route)

Library for registering WP Rest Routes in a more simple way.

[![Latest Stable Version](https://camo.githubusercontent.com/204f5ecff8205cf3c84fbdba4750e4102ba30470a7805adaae82e98129929042/687474703a2f2f706f7365722e707567782e6f72672f70696e6b637261622f706572697175652d726f7574652f76)](https://packagist.org/packages/pinkcrab/perique-route) [![Total Downloads](https://camo.githubusercontent.com/cdd62b3e0a8a85e8bd333474d7b9fa280a7918e10842de3c4d52a9bac93f02f4/687474703a2f2f706f7365722e707567782e6f72672f70696e6b637261622f706572697175652d726f7574652f646f776e6c6f616473)](https://packagist.org/packages/pinkcrab/perique-route) [![Latest Unstable Version](https://camo.githubusercontent.com/3a581742ef489c7adf7d7bb2d60d062978e2165574714a556fa0135c6590fbdf/687474703a2f2f706f7365722e707567782e6f72672f70696e6b637261622f706572697175652d726f7574652f762f756e737461626c65)](https://packagist.org/packages/pinkcrab/perique-route) [![License](https://camo.githubusercontent.com/465ecaf7f98ff6995ad20c3b2e97f9b5c34525b837f17f9134a51b5dc8578930/687474703a2f2f706f7365722e707567782e6f72672f70696e6b637261622f706572697175652d726f7574652f6c6963656e7365)](https://packagist.org/packages/pinkcrab/perique-route) [![PHP Version Require](https://camo.githubusercontent.com/e2395d581f60d570a2859dbab08a6e4bb6b57921d2fd80252c1ec9851465da7c/687474703a2f2f706f7365722e707567782e6f72672f70696e6b637261622f706572697175652d726f7574652f726571756972652f706870)](https://packagist.org/packages/pinkcrab/perique-route)[![GitHub contributors](https://camo.githubusercontent.com/d667eb5d7b930d37b293feec98bb7e09b05bba0f691011f8fb2baf0b36360c0d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6e7472696275746f72732f50696e6b2d437261622f506572697175652d526f7574653f6c6162656c3d436f6e7472696275746f7273)](https://camo.githubusercontent.com/d667eb5d7b930d37b293feec98bb7e09b05bba0f691011f8fb2baf0b36360c0d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6e7472696275746f72732f50696e6b2d437261622f506572697175652d526f7574653f6c6162656c3d436f6e7472696275746f7273)[![GitHub issues](https://camo.githubusercontent.com/050319f0f4e5d202dc4ec8f1311c667420b6d7a3de2dd668748da4c30ee75c6d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d7261772f50696e6b2d437261622f506572697175652d526f757465)](https://camo.githubusercontent.com/050319f0f4e5d202dc4ec8f1311c667420b6d7a3de2dd668748da4c30ee75c6d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d7261772f50696e6b2d437261622f506572697175652d526f757465)

[![WordPress 6.1 Test Suite [PHP7.4-8.2]](https://github.com/Pink-Crab/Perique-Route/actions/workflows/WP_6_1.yaml/badge.svg)](https://github.com/Pink-Crab/Perique-Route/actions/workflows/WP_6_1.yaml)[![WordPress 6.2 Test Suite [PHP7.4-8.2]](https://github.com/Pink-Crab/Perique-Route/actions/workflows/WP_6_2.yaml/badge.svg)](https://github.com/Pink-Crab/Perique-Route/actions/workflows/WP_6_2.yaml)[![WordPress 6.3 Test Suite [PHP7.4-8.2]](https://github.com/Pink-Crab/Perique-Route/actions/workflows/WP_6_3.yaml/badge.svg)](https://github.com/Pink-Crab/Perique-Route/actions/workflows/WP_6_3.yaml)[![WordPress 6.4 Test Suite [PHP7.4-8.3]](https://github.com/Pink-Crab/Perique-Route/actions/workflows/WP_6_4.yaml/badge.svg)](https://github.com/Pink-Crab/Perique-Route/actions/workflows/WP_6_4.yaml)[![WordPress 6.5 Test Suite [PHP7.4-8.3]](https://github.com/Pink-Crab/Perique-Route/actions/workflows/WP_6_5.yaml/badge.svg)](https://github.com/Pink-Crab/Perique-Route/actions/workflows/WP_6_5.yaml)[![WordPress 6.6 Test Suite [PHP7.4-8.3]](https://github.com/Pink-Crab/Perique-Route/actions/workflows/WP_6_6.yaml/badge.svg)](https://github.com/Pink-Crab/Perique-Route/actions/workflows/WP_6_6.yaml)

[![codecov](https://camo.githubusercontent.com/c5f5c6aff431788652b9a6db3f544964e219313488ee5a9929f7ddef3adaa0e4/68747470733a2f2f636f6465636f762e696f2f67682f50696e6b2d437261622f506572697175652d526f7574652f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d34794563654961534650)](https://codecov.io/gh/Pink-Crab/Perique-Route)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/2e4291f8d9e608e86a08adb51b529520af09c93cb6c86f992cc9fd440685cbe5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f50696e6b2d437261622f506572697175652d526f7574652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Pink-Crab/Perique-Route/?branch=master)[![Maintainability](https://camo.githubusercontent.com/92e1c83c50c7a19782e3a23d488686662e4e0785311f6f2146d56b643928ce0e/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f32383539373832376332613739303565313163372f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/Pink-Crab/Perique-Route/maintainability)

---

Why?
----

[](#why)

Registering WP Rest Routes can either be extremely simple or a frustrating wit the argument array format. The Perique Route library allows for a simpler way to register single routes and groupes.

---

Setup
-----

[](#setup)

To install, you can use composer

```
$ composer require pinkcrab/perique-route
```

You will need to include the Registration\_Middleware to the App at boot. We have provided a static method that will handle the dependency injection.

```
// @file: plugin.php

$app_factory->module( \PinkCrab\Route\Module\Route::class );
```

One you have the Route Middleware added to the registration process, all classes which extend `Route_Controller` will now be processed and all routes defined will be registered.

To create a route controller, just extend `Route_Controller` and return an array of all routes and groups. The controllers are constructed with the DI Container, so all dependencies can be passed.

```
class Some_Route extends Route_Controller {

    // @required
    protected $namespace = 'acme/v1';

    // @optional, access to constructor, so allows for full DI.
    protected $some_service;
    protected function __construct(Service $service){
        $this->some_service = $service;
    }

    // @required
    protected function define_routes( Route_Factory $factory): array {
        return [
            // Factory allows for get,post,delete,patch,put requests.
            $factory->get('/users', [$this->some_service, 'list_users' ]),
            $factory->post('/users', [$this->some_service, 'new_user' ]),

            // Create your groups using the group builder.
            $factory->group_builder('/users/{id}', function( Route_Group $group) : Route_Group {
                // Define the GET method.
                $group->get([$this->some_service, 'show_user'])
                    ->argument( // Define the argument proprties as per WP API
                        Integer_Type::on('id')
                            ->validate('is_numeric')
                            ->sanitization('absint')
                            ->required()
                    );

                // Define the DELETE method.
                $group->delete([$this->some_service, 'delete_user'])
                    ->authentication('some_extra_check_for_delete');

                // Define group wide authentication (applied to both routes).
                $group->authentication([$this->some_service, 'check_api_key_in_header']);

                return $group;
            })
        ];
    }

}
```

Once you have your Route\_Controller setup, its just a case of passing the class to the `registration` array and it will be loaded along with Perique.

```
//file: config/registration.php
return [
    ...other classes
    Some_Route::class,
];
```

Routes
======

[](#routes)

Each route must be defined as part of a `Route Model`, these can either be created by hand or using the supplied `Route_Factory` (which is how the `Route_Controller` operates.)

Route Model
-----------

[](#route-model)

A route model has 4 properties which must be defined, `$method`, `$route`, `$callback` &amp; `$namespace`. Route and Method are passed via the constructor, but namespace must be set manually.

As per WP Api standards, all arguments in the route must be defined, this is all handled via the `Arguments` object and is explained in more detail below.

> All properties are defined as `protected` and should be handled via the supplied methods

```
$route = new Route('POST', 'route/path');
$route->namespace('the-thing/v1');
$route->callback(function( WP_REST_Request $request ) {
    // Do the things
});
```

It is also possible to define an `authentication` callback too

```
$route->authentication(function(WP_REST_Request $request): bool{
    return something_check($request);
});
```

[Route Docs](docs/route.md)

- [Route::namespace()](docs/route.md#namespace)
- [Route::authentication()](docs/route.md#authentication)
- [Route::callback()](docs/route.md#callback)
- [Route::argument()](docs/route.md#argument)
- [Route::with\_method()](docs/route.md#with_method)

Route\_Group
------------

[](#route_group)

Like single Route Models, the Route\_Group allows for a similar process of creating related routes that share a common endpoint route and also some functionality. Also like Routes, its better to use the supplied Route\_Factory, but the details here will express how to create a `Route_Group` manually (the core methods are used the same regardless).

```
$group = new Route_Group('my_endpoints/v2','route/');
$group->authentication('shared_group_auth')
$group->get('some_callable')
    ->authentication('additional_auth_for_get_only');
$group->post('some_other_callable');
```

> This would then create a group where all routes assigned are created with the above namespace and route.

[Read Group Docs](docs/group.md)

*Setters*

- [Route\_Group::post()](docs/group.md#post-http-method)
- [Route\_Group::get()](docs/group.md#get-http-method)
- [Route\_Group::patch()](docs/group.md#patch-http-method)
- [Route\_Group::put()](docs/group.md#put-http-method)
- [Route\_Group::delete()](docs/group.md#delete-http-method)
- [Route\_Group::authentication()](docs/group.md#authentication)
- [Route\_Group::argument()](docs/group.md#argument)

Route\_Factory
--------------

[](#route_factory)

As most of the time you will be creating endpoints with a fixed namespace, there is a factory that can be used to populate this for every route it creates, while giving a clean, fluent API that can be used to create routes inline as part of arrays and return values.

```
$factory = new Route_Factory('my_endpoints/v2');
$get = $factory->get('/endpoint', 'some_callable');
$post = $factory->get('/endpoint_2', 'some_other_callable');
```

Both of the above endpoints will be created with the `my_endpoints/v2` namespace.

### Method Helpers

[](#method-helpers)

There are a collection of helper methods on the `Route_Factory` which make it really easy to add methods to the existing namespace.

```
$route = $factory->get('the/route/{name}', [$this, 'some_callback_for_get']);
$route = $factory->post('the/route/{name}', [$this, 'some_callback_for_post']);
$route = $factory->put('the/route/{name}', [$this, 'some_callback_for_put']);
$route = $factory->patch('the/route/{name}', [$this, 'some_callback_for_patch']);
$route = $factory->delete('the/route/{name}', [$this, 'some_callback_for_delete']);
```

It is even possible to create a group around the same route.

```
$group = $factory->group_builder('the/route/{name}', function(Route_Group $group){
    $group->get([$this, 'some_callback_for_get']);
    $group->post([$this, 'some_callback_for_post']);
    $group->delete([$this, 'some_callback_for_delete']);
    $group->put([$this, 'some_callback_for_put']);
    $group->path([$this, 'some_callback_for_path']);
});
```

Route Controller
----------------

[](#route-controller)

The easiest way to define routes, is to extend the `Route_Controller` abstract class. This can be created with a predefined `protected ?string $namespace;` property and a method for defining the routes/group `abstract protected function define_routes( Route_Factory $factory): array`

> See the example above

Manual Route Creation
---------------------

[](#manual-route-creation)

If you do not wish to use the Route Controller above, you will need create an instance of the `Route_Manager` class and add your route or groups to the manager and the execute the manager before `rest_init` hook is called.

```
$manager = new Route_Manager(
    new WP_Rest_Registrar(),
    new Hook_Loader()
);

// Add routes and groups
$manager->from_route(new Route(...));
$manager->from_group(new Route_Group(...));

// Dispatch
$manager->execute();
```

> When `execute()` is called, it will create the `rest_init` hooks, so there is no need to do `add_action(...)`

Previous Versions
-----------------

[](#previous-versions)

- For Perique 1.4.\* use Route 1.0.\*
- For Perique 1.0 - 1.3.\* use Route 0.1.\*

Change Log
----------

[](#change-log)

- 2.1.0 Updated to use PinkCrab Perique 2.1
- 2.0.1 Update dependencies and extended tests to cover WP6.3 &amp; WP6.4
- 2.0.0 Support for Perique 2.0
- 1.0.1 Update readme/docs
- 1.0.0 Update dev testing dependencies for WP6.1, Remove Utils and replace all with FunctionConstructors and updated docs to use `construct_registration_middleware()` rather than being given a constructed instance of the Middleware.
- 0.1.2 Update dev testing dependencies for WP6.0
- 0.1.1 Bumped to version 0.2.0 of PinkCrab Collection Dependency
- 0.1.0 Inital version

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance74

Regular maintenance activity

Popularity22

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 98.5% 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

Every ~224 days

Recently: every ~244 days

Total

8

Last Release

142d ago

Major Versions

0.1.2 → 1.0.02023-03-02

1.0.1 → 2.0.02023-04-03

PHP version history (3 changes)0.1.0PHP &gt;=7.1.0

1.0.0PHP &gt;=7.2.0

2.0.0PHP &gt;=7.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/d82b9e8ef7816d3d0b9812ad233f61f6a313f529e0ac85721781b46ad292e1ea?d=identicon)[glynnquelch](/maintainers/glynnquelch)

---

Top Contributors

[![gin0115](https://avatars.githubusercontent.com/u/28779094?v=4)](https://github.com/gin0115 "gin0115 (129 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/pinkcrab-perique-route/health.svg)

```
[![Health](https://phpackages.com/badges/pinkcrab-perique-route/health.svg)](https://phpackages.com/packages/pinkcrab-perique-route)
```

PHPackages © 2026

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