# 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:** 1\
**Showing post:** 3

<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)

---

_[View the full topic](https://discourse.slimframework.com/t/slim-4-csrf-protection/3435)._
