# Slim 4 - Custom Request (extending the existing Request)

**URL:** <https://discourse.slimframework.com/t/slim-4-custom-request-extending-the-existing-request/4868>\
**Category:** Questions\
**Created:** [June 25, 2021, 9:18am UTC](https://discourse.slimframework.com/t/slim-4-custom-request-extending-the-existing-request/4868 "2021-06-25T09:18:23Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![awstam](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@awstam](https://discourse.slimframework.com/u/awstam)\
**Post date:** [June 28, 2021, 8:29am UTC](https://discourse.slimframework.com/t/slim-4-custom-request-extending-the-existing-request/4868/10 "2021-06-28T08:29:56Z")

</div>

> [@dunkoh](#):
>
> We currently have something similar where we have a custom request handler that extends the slim/psr7 request so that we can have a function that has access to the request so $\_POST values obtained via getQueryParams can be purified before used in code.

the more i think about your use case the more i think this “could” be handled via a middleware and inject an attribute into the normal request

something along the lines of:  
Middleware whatevers

```auto
public function __invoke(Request $request, RequestHandler $handler): Response {
        $post = array();
        foreach ((array)$request->getParsedBody() as $key=>$value){
            $post[$key]=funky_moneky_dance_santize($value);
        }
        $request->withAttribute("post",$post);
        return $handler->handle($request);
    }

```

or just return the post as an argument but that could get a bit confusing if you have a get and a post that are the same type thing hence why im separating them out into their own here `post` “namespace”.

then just in your route

```auto
 $request->getAttribute('post')['moonshine'] ?? null;

```

this way you dont need to extend any existing files etc.

* * *

edit: adding my motivation for the question in OP to its own post just to get my post count up (more like cause this post is about answering @dunkoh’s post whereas mine is more a general at the thread post)

---

_[View the full topic](https://discourse.slimframework.com/t/slim-4-custom-request-extending-the-existing-request/4868)._
