Skip to content

Commit 9032cf6

Browse files
committed
minor #20975 [Security] Add usage example for IsCsrfTokenValid attribute on controller classes (santysisi)
This PR was submitted for the 7.3 branch but it was merged into the 7.2 branch instead. Discussion ---------- [Security] Add usage example for IsCsrfTokenValid attribute on controller classes This PR updates the documentation to demonstrate how the `IsCsrfTokenValid` attribute can be applied directly to a controller class. By applying this attribute at the class level, CSRF token validation will be automatically executed for all routes defined within the controller, ensuring consistent security across all actions without needing to annotate each method individually. The example includes the appropriate use statements and a sample controller implementation for clarity. Commits ------- f97b8d5 [Security] iscsrftokenvalid-attribute-controller-usage
2 parents e12256c + f97b8d5 commit 9032cf6

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

security/csrf.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,20 @@ Suppose you want a CSRF token per item, so in the template you have something li
281281
<button type="submit">Delete item</button>
282282
</form>
283283

284+
In addition :class:`Symfony\\Component\\Security\\Http\\Attribute\\IsCsrfTokenValid`
285+
attribute can be applied to a controller class.
286+
This will cause the CSRF token validation to be executed for all routes defined within the controller::
287+
288+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
289+
use Symfony\Component\Security\Http\Attribute\IsCsrfTokenValid;
290+
// ...
291+
292+
#[IsCsrfTokenValid('controller')]
293+
final class FooController extends AbstractController
294+
{
295+
// ...
296+
}
297+
284298
The :class:`Symfony\\Component\\Security\\Http\\Attribute\\IsCsrfTokenValid`
285299
attribute also accepts an :class:`Symfony\\Component\\ExpressionLanguage\\Expression`
286300
object evaluated to the id::

0 commit comments

Comments
 (0)