An anchor <a href="#">
will always use a GET request. There is no way to modify that. But, it is possible to fake a PUT or DELETE request using Symfony.
Symfony Forms will fake the verb by adding a hidden field to forms.
<input type="hidden" name="_method" value="PUT" />
Then internally it checks if the verb is POST, checks for the _method
parameter and changes the verb from POST to PUT. This only works for HTML forms using the POST method.
The same can be done for GET verbs, but it requires using an Event Listener. Here is an example:
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernel;
class VerbListener
{
public function onKernelRequest ( GetResponseEvent $event )
{
$request = $event->getRequest();
if ( HttpKernel::MASTER_REQUEST === $event->getRequestType()
&& $request->getMethod() === 'GET' )
{
if ( $request->query->get('_method') === 'PUT' ) {
$request->setMethod( 'PUT' );
}
}
}
}
And the service must be registered with a higher priority than the router_listener
service, which matches the route to a controller.
services:
acme.verb.listener:
class: Acme\Bundle\Listener\VerbListener
tags:
- { name: kernel.event_listener,
event: kernel.request,
method: onKernelRequest,
priority: 100 }
The link can now be generated with the _method
parameter
<a href="foo?_method=PUT"></a>