# Slim 4 CSRF Protection?

**URL:** <https://discourse.slimframework.com/t/slim-4-csrf-protection/3435>\
**Category:** Questions\
**Created:** [September 6, 2019, 7:44am UTC](https://discourse.slimframework.com/t/slim-4-csrf-protection/3435 "2019-09-06T07:44:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brainstorm](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@brainstorm](https://discourse.slimframework.com/u/brainstorm)\
**Post date:** [September 6, 2019, 7:44am UTC](https://discourse.slimframework.com/t/slim-4-csrf-protection/3435/1 "2019-09-06T07:44:27Z")

</div>

Hello, How works CSRF protection in Slim v4?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [September 6, 2019, 9:19am UTC](https://discourse.slimframework.com/t/slim-4-csrf-protection/3435/2 "2019-09-06T09:19:30Z")

</div>

Take a look a the “official” [Slim-CSRF](https://github.com/slimphp/Slim-Csrf) package. Unfortunately it doesn’t support Slim 4 yet: [https://github.com/slimphp/Slim-Csrf/pull/104](https://github.com/slimphp/Slim-Csrf/pull/104)

You could also take a look at my Slim 4 CSRF package: [https://github.com/odan/csrf](https://github.com/odan/csrf)

**Update** : Maybe you don’t need CSRF protection anymore: [https://scotthelme.co.uk/csrf-is-really-dead/](https://scotthelme.co.uk/csrf-is-really-dead/)

---

<div class="post-metadata">

**Author:** ![abhie](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@abhie](https://discourse.slimframework.com/u/abhie)\
**Post date:** [April 19, 2020, 1:03pm UTC](https://discourse.slimframework.com/t/slim-4-csrf-protection/3435/3 "2020-04-19T13:03:35Z")

</div>

> Solution: use it **without Middleware**

Instead of using a separate middleware for CSRF Attack Protection, we should use it directly in our bootstrapper file like this

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/e/e7973f254bfb8e42d66fca67f8d6d7b52170f5f9.png)

Now you can use these csrf fields in you template files like this  
 ![image](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/c/c5817b9a6e34d544473a6f595e62c78849385ac5.png)

> Ps: update this block as per your requirements or you can move these all to different file as well

Adding Twig _v3_ Globals for _csrf_ fields will give you this error in Slim _4_  
**Unable to add global “csrf” as the runtime or the extensions have already been initialized.**  
To fix these I implemented this [answer](https://discourse.slimframework.com/t/slim-4-twig-globals/3464/10).

Thanks & Regards  
[shindea890@gmail.com](mailto:shindea890@gmail.com)
