Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 links with CSRF token
    primarykey
    data
    text
    <p>I can't found a documentation about how to generate a link with the CSRF token, like in Symfony 1.4: </p> <pre><code>link_to(__('Delete'), url_for('ntw-delete', $network), array('confirm' =&gt; 'Are you sure?', 'method' =&gt; 'delete')) </code></pre> <p><strong>UPDATED:</strong> I created a twig extension for that. Maybe it will help to someone</p> <p><code>src/UmbrellaWeb/Bundle/ExtraTwigBundle/Twig/LinkExtension.php</code></p> <pre><code>&lt;?php namespace UmbrellaWeb\Bundle\ExtraTwigBundle\Twig; use Twig_Extension; use Twig_Function_Method; use Twig_Environment; use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface; class LinkExtension extends Twig_Extension { protected $csrfProvider; public function __construct(CsrfProviderInterface $csrfProvider) { $this-&gt;csrfProvider = $csrfProvider; } public function getFunctions() { return array( 'link_to' =&gt; new Twig_Function_Method($this, 'linkToFunction', array( 'is_safe' =&gt; array('html') )) ); } /** * Build a link with anchor * * @param string $path * @param string $title * @param array $options Available options: * string 'confirm' - Text for the popup * string 'method' - HTTP Method: post, delete, put * string 'csrfIntention' - CSRF intention. If empty then no CSRF. Not used for GET requests * string 'csrfField' - CSRF field name. _token by default * bool 'escape' - escape title, TRUE by default */ public function linkToFunction($path,$title,array $options = array()) { $default = array( 'csrf_intention' =&gt; '', 'csrf_field' =&gt; '_token', 'escape' =&gt; TRUE ); $options = array_merge($default,$options); $ecape = $options['escape']; unset($options['escape']); $return = '&lt;a href="%s"%s&gt;%s&lt;/a&gt;'; $return = sprintf($return, htmlspecialchars($path), $this-&gt;_tagOptions($this-&gt;_options2javascript($options)), ($ecape)?htmlspecialchars($title):$title ); return $return; } function _options2javascript($options) { // confirm $confirm = isset($options['confirm']) ? $options['confirm'] : ''; unset($options['confirm']); // method $method = isset($options['method']) ? $options['method'] : false; unset($options['method']); // CSRF Intention $csrfIntention = isset($options['csrf_intention']) ? $options['csrf_intention'] : false; unset($options['csrf_intention']); // CSRF field name $csrfField = isset($options['csrf_field']) ? $options['csrf_field'] : false; unset($options['csrf_field']); $onclick = isset($options['onclick']) ? $options['onclick'] : ''; if ($confirm &amp;&amp; $method) { $options['onclick'] = $onclick . 'if (' . $this-&gt;_confirmJsFunction($confirm) . ') { ' . $this-&gt;_methodJsFunction($method,$csrfIntention,$csrfField) . ' };return false;'; } else if ($confirm) { if ($onclick) { $options['onclick'] = 'if (' . $this-&gt;_confirmJsFunction($confirm) . ') { return ' . $onclick . '} else return false;'; } else { $options['onclick'] = 'return ' . $this-&gt;_confirmJsFunction($confirm) . ';'; } } else if ($method) { $options['onclick'] = $onclick . $this-&gt;_methodJsFunction($method,$csrfIntention,$csrfField) . 'return false;'; } return $options; } function _confirmJsFunction($confirm) { return "confirm('".$this-&gt;_escapeJs($confirm)."')"; } /** * Escape carrier returns and single and double quotes for Javascript segments. */ function _escapeJs($javascript = '') { $javascript = preg_replace('/\r\n|\n|\r/', "\\n", $javascript); $javascript = preg_replace('/(["\'])/', '\\\\\1', $javascript); return $javascript; } function _methodJsFunction($method,$csrfIntention,$csrfField) { $function = "var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'post'; f.action = this.href;"; //put, delete HTTP methods if ('post' != strtolower($method)) { $function .= "var m = document.createElement('input'); m.setAttribute('type', 'hidden'); "; $function .= sprintf("m.setAttribute('name', '_method'); m.setAttribute('value', '%s'); f.appendChild(m);", strtolower($method)); } // CSRF protection if ($csrfIntention) { /** * @todo isCsrfEnabled() - check a global config */ if (TRUE) { $function .= "var m = document.createElement('input'); m.setAttribute('type', 'hidden'); "; $function .= sprintf("m.setAttribute('name', '%s'); m.setAttribute('value', '%s'); f.appendChild(m);", $csrfField, $this-&gt;csrfProvider-&gt;generateCsrfToken($csrfIntention)); } } $function .= "f.submit();"; return $function; } function _tagOptions(array $options = array()) { $html = ''; foreach ($options as $key =&gt; $value) { $html .= ' ' . $key . '="' . htmlspecialchars($value) . '"'; } return $html; } public function getName() { return 'umbrellaweb_link'; } } </code></pre> <p><code>services.xml</code> </p> <pre><code>&lt;container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"&gt; &lt;services&gt; &lt;service id="umbrellaweb.twig.link_extension" class="UmbrellaWeb\Bundle\ExtraTwigBundle\Twig\LinkExtension"&gt; &lt;tag name="twig.extension" /&gt; &lt;argument type="service" id="form.csrf_provider" /&gt; &lt;/service&gt; &lt;/services&gt; &lt;/container&gt; </code></pre> <p>Now in twig you can use:</p> <pre><code>{{ link_to(path('jk_aa_admin_delete',{'id' : admin.id}),'&lt;img src="del_icon.png"/&gt;', {'escape':false,'method':'delete','csrf_intention':'delete-admin', 'confirm':'Are you sure?'}) }} </code></pre> <p>I controller:</p> <pre><code>//check CSRF token if (FALSE === $this-&gt;get('form.csrf_provider')-&gt;isCsrfTokenValid('delete-admin', $request-&gt;get('_token'))) { throw new AccessDeniedHttpException('Invalid CSRF token.'); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload