Commit 9539480b authored by Fabien Potencier's avatar Fabien Potencier

moved controller method configuration from Application to Controller

It's more consistent with the way we manage other aspects of the controller
and it allows to change the method programatically after the creation of the
controller.

Before:

    $app->match('/', function () { echo 'foo'; }, 'GET|POST');

After:

    $app->match('/', function () { echo 'foo'; })->method('GET|POST');
parent 74372712
This changelog references all backward incompatibilities as we introduce them:
* 2011-08-08: The controller method configuration is now done on the Controller itself
Before:
$app->match('/', function () { echo 'foo'; }, 'GET|POST');
After:
$app->match('/', function () { echo 'foo'; })->method('GET|POST');
......@@ -199,10 +199,17 @@ can also call ``match``, which will match all methods.
::
$app->put('/blog', function () {
$app->match('/blog', function () {
...
});
You can then restrict the allowed methods via the ``method`` method::
$app->match('/blog', function () {
...
})
->method('PATCH');
.. note::
The order in which the routes are defined is significant. The first
......
......@@ -117,20 +117,12 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
*
* @param string $pattern Matched route pattern
* @param mixed $to Callback that returns the response when matched
* @param string $method Matched HTTP methods, multiple can be supplied,
* delimited by a pipe character '|', eg. 'GET|POST'.
*
* @return Silex\Controller
*/
public function match($pattern, $to, $method = null)
public function match($pattern, $to)
{
$requirements = array();
if ($method) {
$requirements['_method'] = $method;
}
$route = new Route($pattern, array('_controller' => $to), $requirements);
$route = new Route($pattern, array('_controller' => $to));
$controller = new Controller($route);
$this['controllers']->add($controller);
......@@ -147,7 +139,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
*/
public function get($pattern, $to)
{
return $this->match($pattern, $to, 'GET');
return $this->match($pattern, $to)->method('GET');
}
/**
......@@ -160,7 +152,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
*/
public function post($pattern, $to)
{
return $this->match($pattern, $to, 'POST');
return $this->match($pattern, $to)->method('POST');
}
/**
......@@ -173,7 +165,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
*/
public function put($pattern, $to)
{
return $this->match($pattern, $to, 'PUT');
return $this->match($pattern, $to)->method('PUT');
}
/**
......@@ -186,7 +178,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
*/
public function delete($pattern, $to)
{
return $this->match($pattern, $to, 'DELETE');
return $this->match($pattern, $to)->method('DELETE');
}
/**
......
......@@ -109,6 +109,19 @@ class Controller
return $this;
}
/**
* Sets the requirement for the HTTP method.
*
* @param string $method The HTTP method name. Multiple methods can be supplied,
* delimited by a pipe character '|', eg. 'GET|POST'.
*/
public function method($method)
{
$this->route->setRequirement('_method', $method);
return $this;
}
/**
* Sets the requirement of HTTP (no HTTPS) on this controller.
*/
......
......@@ -118,7 +118,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$app->match('/bar', function () {
return 'bar';
}, 'GET|POST');
})->method('GET|POST');
$app->get('/resource', function () {
return 'get resource';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment