我使用Silex framework模拟REST服务器。我需要为http方法创建选项uri,但是Application
类只提供PUT,GET,POST和DELETE方法。是否可以添加和使用自定义的http方法?
发布于 2012-11-29 17:37:44
我也做了同样的事情,但我不太记得我是怎么做到的。我现在不能试。当然,您必须扩展ControllerCollection
class MyControllerCollection extends ControllerCollection
{
/**
* Maps an OPTIONS request to a callable.
*
* @param string $pattern Matched route pattern
* @param mixed $to Callback that returns the response when matched
*
* @return Controller
*/
public function options($pattern, $to)
{
return $this->match($pattern, $to)->method('OPTIONS');
}
}
然后在您的自定义Application
类中使用它:
class MyApplication extends Application
{
public function __construct()
{
parent::__construct();
$app = $this;
$this['controllers_factory'] = function () use ($app) {
return new MyControllerCollection($app['route_factory']);
};
}
/**
* Maps an OPTIONS request to a callable.
*
* @param string $pattern Matched route pattern
* @param mixed $to Callback that returns the response when matched
*
* @return Controller
*/
public function options($pattern, $to)
{
return $this['controllers']->options($pattern, $to);
}
}
发布于 2016-03-04 01:09:07
由于这个问题在谷歌搜索中的排名仍然很高,我要指出的是,几年后的今天,Silex为OPTIONS
添加了一个处理程序方法
http://silex.sensiolabs.org/doc/usage.html#other-methods
当前可直接用作函数调用的动词列表为:get
、post
、put
、delete
、patch
、options
。所以:
$app->options('/blog/{id}', function($id) {
// ...
});
应该可以很好地工作。
https://stackoverflow.com/questions/13622377
复制相似问题