2017 © Pedro Peláez
 

library silex-restful-controller

Simple Silex Controller for implementing restful Apis

image

spanitz/silex-restful-controller

Simple Silex Controller for implementing restful Apis

  • Monday, April 14, 2014
  • by spanitz
  • Repository
  • 1 Watchers
  • 2 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Restful Controller for Silex

The RestfulControllerProvider provides an alternative way of implementing class-based route controllers., (*1)

Getting started

Just register the RestfulControllerProvider for a specific route:, (*2)

<?php

use spanitz\Silex\Provider\RestfulControllerProvider;

$app = new Silex\Application();
$app->mount('/api', new RestfulControllerProvider());

Usage

Let's take the well-known Todo example:, (*3)

<?php
namespace Api\Controller;

use spanitz\Silex\RestfulController;

class Todo extends RestfulController
{
    public function get ($id = null)
    {
        $data = array();
        // fetch multiple todos, or a single one by its $id...

        return $data;
    }

    public function put ($id = null)
    {
        // update todo...
    }

    public function post ()
    {
        // create todo...
    }

    public function delete ($id = null)
    {
        // delete todo...
    }
}

The implementation above exposes the following routes:, (*4)

  • GET /api/todo/<id>
  • PUT /api/todo/<id>
  • POST /api/todo/
  • DELETE /api/todo/<id>

As you see, the implemented methods corresponds to HTTP methods. If your controller extends from RestfulController, you're able to access the Silex Application instance in your methods with $this->app; and the corresponding Request with $this->request., (*5)

Your method can either return an array, which is returned as JsonResponse; or any kind of Symfony\Component\HttpFoundation\Response instance., (*6)

Configuration

If the default namespace Api\Controller doesn't fit your needs, just overwrite the configuration in the constructor:, (*7)

php $app->mount('/api', new RestfulControllerProvider(array( 'namespace' => 'My\Controller\Namespace' )));, (*8)

Your derived class of RestfulController also takes care of the correct response codes. If you want to overwrite this behaviour, the following configuration can be set:, (*9)

php $app->mount('/api', new RestfulControllerProvider(array( 'status-codes' => array( 'post' => 200 'put' => 200 ) )));, (*10)

Installation

Simply install via composer. Add the following lines to your composer.json:, (*11)

{
    "require": {
        "spanitz/silex-restful-controller": "1.0.*@dev"
    },

    "autoload": {
        "psr-4": {"Api": "src/"}
    }
}

The Versions

14/04 2014

dev-master

9999999-dev

Simple Silex Controller for implementing restful Apis

  Sources   Download

MIT

The Requires

 

by Stefan Panitz

silex

13/04 2014

1.0.0

1.0.0.0

Simple Silex Controller for implementing restful Apis

  Sources   Download

MIT

The Requires

 

by Stefan Panitz

silex