Silex Method - OPTIONS

I am using the Silex framework for a mocking REST server. I need to create uri for the OPTIONS http method, but the Application class only offers methods for PUT, GET, POST and DELETE. Can I add and use my own http method?

+6
source share
2 answers

I did the same, but I don’t remember very well how I managed to get it to work. I can’t try it right now. Of course you should extend the 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'); } } 

And then use it in your regular Application class:

 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); } } 
+4
source

Since this question is still highly rated by Google search engines, I want to note that now, after a few years, Silex has added a handler method for OPTIONS

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

The current list of verbs that can be used as function calls are: get , post , put , delete , patch , OPTIONS . So:

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

It should work fine.

+3
source

All Articles