我正在使用 Silex framework用于模拟 REST 服务器。我需要为 OPTIONS http 方法创建 uri,但是 Application
类只提供 PUT、GET、POST 和 DELETE 方法。是否可以添加和使用自定义 http 方法?
最佳答案
我做了同样的事情,但我不太记得我是如何让它发挥作用的。我现在无法尝试。当然,您必须扩展 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);
}
}
关于php - Silex - 选项方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13622377/