How to render CSRF input in twig?

前端 未结 5 1618
野性不改
野性不改 2021-02-02 06:39

I know there\'s the usual way to render CSRF token hidden input with form_rest, but is there a way to render just CSRF input itself? I\'ve overridd

5条回答
  •  时光说笑
    2021-02-02 06:52

    If you have formView object, you can render it using Twig function:

    {{ form_widget(formView._token) }} 
    

    If you haven't - you can render token without using form object directly:

    
    

    Works in Symfony 2.x and 3.x

    To validate the token you can use the following code in your controller (Symfony 3.x):

    $submittedToken = $request->request->get('token');
    
    if ($this->isCsrfTokenValid('some-name', $submittedToken)) {
        // ... do something,
    }
    

提交回复
热议问题