bcen/silex-dispatcher

此包已被废弃且不再维护。未建议替代包。

一个Silex插件

0.5.1 2013-07-05 19:03 UTC

README

Build Status

安装

通过 Composer

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

然后运行 $ composer.phar install

用法

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

特性

  • 类似Django的CBV控制器

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

    处理缺失方法:

    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的服务定位器/依赖注入模式

    
    $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();
        }
    }
    
  • 创建"RESTful" API的辅助程序/中间件

    
    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();
    

    内容协商:

    $ 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

    自动序列化:

    $ 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":[]}

    自动分页:

    $ 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]}

    自动405响应(缺失方法处理器):

    $ 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

    注意:记得打开URL重写!

已知问题

  • 与某些其他解析器不兼容
  • FilterControllerEvent::getController 会返回一个闭包,因为 SilexCbvControllerResolver 使用闭包包装了实际的类控制器实例。

测试

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

许可

Silex-Dispatcher 采用MIT许可。