PHPackages                             stonedz/pff2-rest - 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. stonedz/pff2-rest

ActivePff2-module[HTTP &amp; Networking](/categories/http)

stonedz/pff2-rest
=================

Creates REST controllers in pff2

v4.1(4mo ago)0122↓85%MITPHP

Since Nov 12Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/stonedz/pff2-rest)[ Packagist](https://packagist.org/packages/stonedz/pff2-rest)[ Docs](https://github.com/stonedz/pff2-rest)[ RSS](/packages/stonedz-pff2-rest/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (3)Versions (9)Used By (0)

pff2-rest
=========

[](#pff2-rest)

REST module for `stonedz/pff2` (v4) to build JSON APIs with:

- automatic REST method dispatch (`GET/POST/PUT/DELETE`)
- optional auth hook per request
- API version prefix routing (e.g. `api1/...`)
- JSON exception output for API requests

Requirements
------------

[](#requirements)

- PHP + `stonedz/pff2` v4
- Module dependencies declared in this package:
    - `exception_handler`
    - `pff2-permissions`
- Optional module:
    - `pff2-annotations` (only needed for annotation-based REST detection)

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

[](#installation)

```
composer require stonedz/pff2-rest
```

Enable modules in app config
----------------------------

[](#enable-modules-in-app-config)

In your app `config.user.php`, ensure the modules list includes:

```
$pffConfig['modules'] = [
	'exception_handler',
	'pff2-permissions',
	'pff2-rest',
];
```

If you want annotation-based REST detection, also add:

```
'pff2-annotations',
```

Module configuration
--------------------

[](#module-configuration)

Create (or override) config at:

`app/config/modules/pff2-rest/module.conf.yaml`

Example:

```
moduleConf:
  annotationName: Rest
  enableAuth: true
  authType: token
  authClass: ApiAuthCkecker
  apiVersions:
	- api1
```

### Config keys

[](#config-keys)

- `annotationName`: method annotation name checked by `pff2-annotations` (when installed/enabled).
- `enableAuth`: if `true`, auth class is invoked before handling request.
- `authType`: currently informational in this module code.
- `authClass`: class name under namespace `\pff\models\` implementing `IRestAuth`.
- `apiVersions`: URL prefixes treated as API version roots (`api1`, `v2`, etc).

How routing works
-----------------

[](#how-routing-works)

If request URL starts with a configured API version, pff2-rest rewrites route from:

`/api1/users/42`

to controller/action form:

`Api1_Users/index/42`

Then, if controller implements `IRestController`, action is replaced by HTTP method:

- `GET` → `getHandler`
- `POST` → `postHandler`
- `PUT` → `putHandler`
- `DELETE` → `deleteHandler`
- `OPTIONS` → immediate `200`

If `pff2-annotations` is installed and enabled, methods marked with the configured annotation name are also treated as REST requests.

Creating a REST controller
--------------------------

[](#creating-a-rest-controller)

Implement `pff\modules\Iface\IRestController`:

```
