2012-11-29 52 views

回答

4

我做了同樣的事情,但我不記得我是如何設法使它工作的。我現在無法嘗試。爲了確保您有延長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); 
    } 
} 
+0

好極了,非常簡單,謝謝! –

+0

@PeterKrejci是否有效?我無法測試它... – gremo

+0

是的,沒有任何問題。 –

3

因爲這個問題仍然來自於谷歌搜索了名列前茅的,我請注意,現在幾年之後,Silex添加了一種處理方法:OPTIONS

http://silex.sensiolabs.org/doc/usage.html#other-methods

可以直接用作函數調用的動詞的當前列表是:get,postputdelete,patch,options。所以:

$app->options('/blog/{id}', function($id) { 
    // ... 
}); 

應該工作得很好。