2017 © Pedro Peláez
 

library silex-dispatcher

A Silex plugin

image

bcen/silex-dispatcher

A Silex plugin

  • Friday, August 16, 2013
  • by bcen
  • Repository
  • 0 Watchers
  • 7 Stars
  • 365 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 19 Versions
  • 0 % Grown

The README.md

Silex-Dispatcher

Build Status, (*1)

Installation

Via Composer:, (*2)

{
    "require": {
        "bcen/silex-dispatcher": "0.5.*"
    }
}

Then run $ composer.phar install, (*3)

Usage

$app->register(new \SDispatcher\SDispatcherServiceProvider());

Features

  • Django-alike CBV controller, (*4)

    class HomeController
    {
        public function get(Request $req)
        {
            return 'Hi, '.$req->getClientIp();
        }
    
        public function post(Request $req)
        {
            return 'This is a post';
        }
    }
    
    $app->match('/', 'HomeController');
    

    Handle missing method:, (*5)

    class HomeController
    {
        public function get(Request $req)
        {
        }
    
        public function handleMissingMethod(Request $req)
        {
            // HEAD, OPTIONS, PUT, POST everything goes here
            // except GET, which is handle by the above method.
        }
    }
    
  • Hybrid-alike Service Locator/Dependency Injection pattern, (*6)


    $app['my_obj'] = function () { $obj = new \stdClass; $obj->message = 'hi'; return $obj; }; class HomeController implements \SDispatcher\Common\RequiredServiceMetaProviderInterface { public function __construct(\stdClass $obj) { var_dump($obj); } public function get($req) { return 'Hi, '.$req->getClientIp(); } public static function getRequiredServices() { return array('my_obj'); } } $app->match('/', 'HomeController'); // or by annotation use SDispatcher\Common\Annotation as REST; /** * @REST\RequiredServices("my_obj") */ class HomeController { public function __construct(\stdClass $obj) { var_dump($obj); } public function get($req) { return 'Hi, '.$req->getClientIp(); } }
  • Helpers/Middlewares for creating "RESTful" API, (*7)


    require __DIR__ . '/vendor/autoload.php'; class NumberListResource { public function get() { return array(1, 2, 3, 4, 5, 6); } } class NumberDetailResource { public function get(Request $req, $nid) { return new \SDispatcher\DataResponse(array( 'id' => $nid, 'value' => 'some value', 'filters' => $req->query->all(), )); } } $app = new \Silex\Application(); $app->register(new \SDispatcher\SDispatcherServiceProvider()); $app['controllers']->setOption('sdispatcher.route.rest', true); $app->match('/numbers', 'NumberListResource'); $app->match('/numbers/{nid}', 'NumberDetailResource'); $app->run();

    Content Negotiation:, (*8)

    $ curl local.domain.org/api-test/numbers/1 -H "Accept:application/xml" -i
    HTTP/1.1 406 Not Acceptable
    Date: Sat, 18 May 2013 00:28:58 GMT
    Server: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
    X-Powered-By: PHP/5.4.7
    Cache-Control: no-cache
    Content-Length: 0
    Content-Type: text/html; charset=UTF-8
    

    Automated Serialization:, (*9)

    $ curl local.domain.org/api-test/numbers/1 -H "Accept:application/json" -i
    HTTP/1.1 200 OK
    Date: Sat, 18 May 2013 00:29:30 GMT
    Server: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
    X-Powered-By: PHP/5.4.7
    Cache-Control: no-cache
    Content-Length: 50
    Content-Type: application/json
    
    {"id":"some_id","value":"some value","filters":[]}
    

    Automated Pagination:, (*10)

    $ curl local.domain.org/api-test/numbers
    {"meta":{"offset":0,"limit":20,"total":6,"prevLink":null,"nextLink":null},"objects":[1,2,3,4,5,6]}
    

    Automated 405 Response for missing method handler:, (*11)

    $ curl local.domain.org/api-test/numbers -i -X POST
    HTTP/1.1 405 Method Not Allowed
    Date: Sat, 18 May 2013 01:21:20 GMT
    Server: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
    X-Powered-By: PHP/5.4.7
    Cache-Control: no-cache
    Content-Length: 0
    Content-Type: text/html; charset=UTF-8
    

    NOTE: Remember to turn on URL rewrite!!, (*12)

Known Issues:

  • Incompaitible with some other resolvers
  • ~~FilterControllerEvent::getController will return a closure due to SilexCbvControllerResolver wraps the actual class controller instance with closure.~~

Testing

$ composer.phar install --dev
$ vendor/bin/phpspec
$ vendor/bin/behat

License

Silex-Dispatcher is licensed under the MIT license., (*13)

The Versions