I was reading through the "Faking the method with _method" section on the following URL: http://symfony.com/doc/current/cookbook/routing/method_parameters.html
After some experimentation, I realized that the documentation is either unclear or inaccurate. One can only override the HTTP method with the _method parameter if the browser uses POST (and not GET) to make the request.
Symfony\Component\HttpFoundation\Request
public function getMethod()
{
if (null === $this->method) {
$this->method = strtoupper($this->server->get('REQUEST_METHOD', 'GET'));
if ('POST' === $this->method) {
if ($method = $this->headers->get('X-HTTP-METHOD-OVERRIDE')) {
$this->method = strtoupper($method);
} elseif (self::$httpMethodParameterOverride) {
$this->method = strtoupper($this->request->get('_method', $this->query->get('_method', 'POST')));
}
}
}
return $this->method;
}
In our application, we'd like to override the HTTP method for GET requests, because we're using JSONP. In our case, this is not a security issue as requests are signed using a CSRF token.
I found a solution for this in the "Overriding the Request" section here:
http://symfony.com/doc/current/components/http_foundation/introduction.html
This would involve making a sub-class of Symfony\Component\HttpFoundation\Request, over-riding the getMethod() method, and setting it using Request::setFactory().
use Symfony\Component\HttpFoundation\Request;
Request::setFactory(function (
array $query = array(),
array $request = array(),
array $attributes = array(),
array $cookies = array(),
array $files = array(),
array $server = array(),
$content = null
) {
return SpecialRequest::create(
$query,
$request,
$attributes,
$cookies,
$files,
$server,
$content
);
});
$request = Request::createFromGlobals();
My question is:
The only place I can see to do this is in app.php / app_dev.php / app_test.php. E.g.:
require_once __DIR__.'/../app/AppKernel.php';
$kernel = new AppKernel($env, $is_debug);
$kernel->loadClassCache();
// When using the HttpCache, you need to call the method in your front controller instead of relying on the configuration parameter
//Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
Would this be the right place to do this?
I looked at kernel events, such as kernel.request, but at this point it seems too late for the event to be useful:
"1) The kernel.request Event Typical Purposes: To add more information to the Request, initialize parts of the system, or return a Response if possible (e.g. a security layer that denies access)."
http://symfony.com/doc/current/components/http_kernel/introduction.html
Any advice would be appreciated. Thank you.