# Request/response customization

**URL:** <https://discourse.slimframework.com/t/request-response-customization/5493>\
**Category:** Questions\
**Created:** [January 25, 2023, 2:34pm UTC](https://discourse.slimframework.com/t/request-response-customization/5493 "2023-01-25T14:34:29Z")\
**Posts on this page:** 1\
**Showing post:** 3

<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:** [January 27, 2023, 7:17am UTC](https://discourse.slimframework.com/t/request-response-customization/5493/3 "2023-01-27T07:17:13Z")

</div>

as someone that went the “custom” request / response route. (and had a fight with odan 😛 ) i can safely say… dont bother with custom request / response. it WILL make your life harder (ive gone back to “mostly” vanilla slim as it really really just works)

> [@Slim 4 - Custom Request (extending the existing Request)](https://discourse.slimframework.com/t/slim-4-custom-request-extending-the-existing-request/4868/10):
>
> 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 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); retu…

what i now use instead is a middleware that sets up the “parameters” onto the normal `$request->getAttribute()`

```auto
// TODO: sanitize the inputs
        foreach ((array)$request->getQueryParams() as $key => $value) {
            $request = $request->withAttribute("GET." . $key, $value);
        }
        foreach ((array)$request->getParsedBody() as $key => $value) {
            $request = $request->withAttribute("POST." . $key, $value);
        }

        foreach ((array)$route->getArguments() as $key => $value) {
            $request = $request->withAttribute("PARAM." . $key, $value);
        }

```

then usage in the controller is simply `$request->getAttribute("POST.name");`

also this might be of some use to you as well

> [@Type hinting $request-\>getAttribute("woof") / data from middleware](https://discourse.slimframework.com/t/type-hinting-request-getattribute-woof-data-from-middleware/5453):
>
> what is the preferred way to pass data from the middleware down to the route handler? the 3 options i can think of: middleware+ -\>withAttribute("woof",new WoofObject(...)) and in route /\*\* @var $woof WoofObject \*/ $woof-\>getAttribute("woof"); (ie type hinting the variable holding -\>getAttribute wherever its used with /\*\* @var - its usually not just a simple new object, im just using it as an eg here) not use the middleware in a traditional sense but to have an object that accepts the fu…

as for the response…

go with something like

> <https://github.com/odan/slim4-skeleton/tree/master/src/Renderer>
>
> //github.com/odan/slim4-skeleton/tree/master/src/Renderer

```auto
use System\Responders\Responder;
class JobsController {
    function __construct(
        protected Responder $responder,
    ) { }

    public function __invoke(Request $request, Response $response): Response {

    	// ...

    	return $this->responder->withResponse(
            response: $response,
            data: $data,
            errors: $errors,
        );
    }
}

```

this lets you use your container (php-di?) in the responder as well. gives you a TON of extra options for a few bits of extra boiler plate. but it makes it super easy to switch our responders then

slim middleware is super super super strong.

---

_[View the full topic](https://discourse.slimframework.com/t/request-response-customization/5493)._
