PHPackages                             warmans/silex-rest-provider - 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. [API Development](/categories/api)
4. /
5. warmans/silex-rest-provider

ActiveLibrary[API Development](/categories/api)

warmans/silex-rest-provider
===========================

Basic library for generating REST resources in Silex using extremely terse configuration. Based somewhat on mach/silex-rest.

2.0.0(11y ago)13941PHPPHP &gt;=5.4.0

Since Sep 25Pushed 11y ago1 watchersCompare

[ Source](https://github.com/warmans/silex-rest-provider)[ Packagist](https://packagist.org/packages/warmans/silex-rest-provider)[ RSS](/packages/warmans-silex-rest-provider/feed)WikiDiscussions master Synced yesterday

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

Yet Another Silex Rest Provider
===============================

[](#yet-another-silex-rest-provider)

[![Build Status](https://camo.githubusercontent.com/4fe0eacde7d8877fe23f1cd2db9e1367c77f31ddbdd9077d132b78a9ceee3c18/68747470733a2f2f7472617669732d63692e6f72672f7761726d616e732f73696c65782d726573742d70726f76696465722e737667)](https://travis-ci.org/warmans/silex-rest-provider)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/829252a5bb90591871cf2d82c92e0455d56f29b58589a480d2ae324dfac3fbbf/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7761726d616e732f73696c65782d726573742d70726f76696465722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/warmans/silex-rest-provider/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/d2362cd01519455ff549f775c19d79990fc10176e6c05105c400e85c554cfd51/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7761726d616e732f73696c65782d726573742d70726f76696465722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/warmans/silex-rest-provider/?branch=master)

Provider to simplify/standardize the generation of REST API resources in Silex based somewhat on mach/silex-rest.

### Important Note on Versions

[](#important-note-on-versions)

Versions below 2.0.0 support Silex 1.x while versions above and including 2.0.0 support silex 2.0@dev. The 2.0.0 versions WILL NOT work with silex 1.2.

### The Problem

[](#the-problem)

With mach's rest provider you define your api as follows:

```
$app['some.controller'] = function() {
    return new \Some\Controller();
}

$app['some.controller.2'] = function() {
    return new \Some\Controller();
}

$app['some.controller.3'] = function() {
    return new \Some\Controller();
}

$app['some.controller.4'] = function() {
    return new \Some\Controller();
}

$r1 = $app['rest']->resource('foo', 'some.controller'); // /foo
$r2 = $r1->subresource('bar', 'some.controller.2');     // /foo/0/bar
$r3 = $r2->subresource('baz', 'some.controller.3');     // /foo/0/bar/0/baz
$r4 = $r2->subresource('baz-alt', 'some.controller.4'); // /foo/0/bar/0/baz-alt

```

This is fine if you have a small set of resources but as an API grows you end up with a bit of a mess. Deleting resources can also cause problems as you have to find the factory, and untangle the dependent resources for the resource being removed.

### The Solution

[](#the-solution)

Generate the api based on a configuration array instead and allow inline controller factory definition.

The same api is defined as follows:

```
$resources = [[
    'uri' => 'foo',
    'ctl' => function() { return new \Some\Controller(); },
    'sub' => [[
        'uri' => 'bar',
        'ctl' => function() { return new \Some\Controller(); },
        'sub' => [[
            'uri' => 'baz',
            'ctl' => function() { return new \Some\Controller(); },
        ],[
            'uri' => 'baz-alt',
            'ctl' => function() { return new \Some\Controller(); },
        ]]
    ]]
]];

$app['rest']->importApi($resources);

```

Internally this will register all the controllers and then setup routes for the HTTP verbs.

### Installation

[](#installation)

Install with composer than register the provider in your application

```
$app->register(new \SilexProvier\Rest\Provider\RestServiceProvider($app));

```

### Resources

[](#resources)

At minimum a resource must have the following elements:

```
[
    'uri' => '/api/v1/foo',
    'ctl' => function() { return new \Some\Controller(); }
]

```

The `URI` defines the URI segment for the resource and the controller defines a factory which will generate the controller used to handle requests to this segment.

The controller class must implement public methods for the HTTP verbs you want to implement.

RequestInvokesGET /api/v1/foocget()POST /api/v1/foopost()GET /api/v1/foo/1get($id)PUT /api/v1/foo/1put($id)PATCH /api/v1/foo/1patch($id)DELETE /api/v1/foo/1delete($id)Note that both the root of the config and each subresources are ARRAYs of resources so even if you have only a single root resource it must be wrapped in an array as follows:

```
$myApi = [[
    'uri' => '/api/v1/foo',
    'ctl' => function() { return new \Some\Controller(); }
]];

```

### Sub Resources

[](#sub-resources)

Sub resources can be defines as follows:

```
[
    'uri' => '/api/v1/foo',
    'ctl' => function() { return new \Some\Controller(); },
    'sub' => [[
        'uri' => 'bar',
        'ctl' => function() { return new \Some\Controller(); },
    ]]
]

```

For each additional sub resource in a hierarchy an additional id property is passed to the controller action. These are named id, idd, iddd, idddd and so on for as many sub resources as exist.

With the above example a call to `GET /api/v1/foo/1/bar/2` will call the equivalent of `\Some\Controller::get(1, 2)`so `\Some\Controller` should define its get method as `get($id, $idd)`.

Similarly a call to `GET /api/v1/foo/1/bar` will invoke `\Some\Controller::cget(1)`.

### Additional information on Controllers

[](#additional-information-on-controllers)

If you would prefer not to defined factories inline you can just assign ctl to the name of an existing service e.g.

```
'ctl' => 'my.registered.controller'

```

If you want access to a controller that WAS defined inline its name will be prefixed with `rest.ctl`then use the full hierarchy of resources in a dot-separated string. For example the config defined in the sub resources section above would register:

1. rest.ctl.api.v1.foo
2. rest.ctl.api.v1.foo.bar

and so on.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity61

Established project with proven stability

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 ~30 days

Total

3

Last Release

4185d ago

Major Versions

0.0.2 → 2.0.02014-11-25

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1299401?v=4)[SW](/maintainers/warmans)[@warmans](https://github.com/warmans)

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/warmans-silex-rest-provider/health.svg)

```
[![Health](https://phpackages.com/badges/warmans-silex-rest-provider/health.svg)](https://phpackages.com/packages/warmans-silex-rest-provider)
```

###  Alternatives

[tobiassjosten/facebook-service-provider

Silex ServiceProvider for the Facebook SDK

266.1k](/packages/tobiassjosten-facebook-service-provider)[glen/slack-unfurl

Extensible Slack App for link unfurling

211.4k5](/packages/glen-slack-unfurl)

PHPackages © 2026

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