# Accessing request outside of route

**URL:** <https://discourse.slimframework.com/t/accessing-request-outside-of-route/4004>\
**Category:** Questions\
**Created:** [April 3, 2020, 2:58pm UTC](https://discourse.slimframework.com/t/accessing-request-outside-of-route/4004 "2020-04-03T14:58:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabswt](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/fabswt/32/1110_2.png) [@fabswt](https://discourse.slimframework.com/u/fabswt)\
**Post date:** [April 3, 2020, 2:58pm UTC](https://discourse.slimframework.com/t/accessing-request-outside-of-route/4004/1 "2020-04-03T14:58:24Z")

</div>

I’m still confused by the structure of Slim 4 and its decoupling of everything.

The upgrade guide says:

> ## Changes to Container
> 
> Slim no longer has a Container so you need to supply your own. If you were relying on request or response being in the container, then you need to either set them to a container yourself, or refactor. Also, `App::__call()` method has been removed, so accessing a container property via `$app->key_name()` no longer works.

I need to access the request (specifically, the POST data) within the context of a class’s method.

In Slim 2, I could do:

```php
global $app;
$req = $app->request();

```

In Slim 3, I could do:

```php
global $app;
$request = $app->getContainer()->get('request')

```

What’s the Slim 4 way to access it?

FYI, I’m creating my container like so:

```php
$container = new \DI\Container();
AppFactory::setContainer($container);
$app = AppFactory::create();
$container = $app->getContainer();

```

Thanks for the insight.

---

<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:** [April 3, 2020, 3:32pm UTC](https://discourse.slimframework.com/t/accessing-request-outside-of-route/4004/2 "2020-04-03T15:32:12Z")

</div>

The request and response objects are context-specific and do not belong in the container.  
For this reason, these objects were removed from the container in Slim 4.

---

<div class="post-metadata">

**Author:** ![fabswt](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/fabswt/32/1110_2.png) [@fabswt](https://discourse.slimframework.com/u/fabswt)\
**Post date:** [April 3, 2020, 9:14pm UTC](https://discourse.slimframework.com/t/accessing-request-outside-of-route/4004/3 "2020-04-03T21:14:39Z")

</div>

I can appreciate that, but then how do you access the request when you need it inside of a class?

---

<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:** [April 3, 2020, 9:24pm UTC](https://discourse.slimframework.com/t/accessing-request-outside-of-route/4004/4 "2020-04-03T21:24:17Z")

</div>

The question is what kind of class? A controller, single action controller class or a middleware class?

---

<div class="post-metadata">

**Author:** ![fabswt](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/fabswt/32/1110_2.png) [@fabswt](https://discourse.slimframework.com/u/fabswt)\
**Post date:** [April 3, 2020, 9:36pm UTC](https://discourse.slimframework.com/t/accessing-request-outside-of-route/4004/5 "2020-04-03T21:36:08Z")

</div>

A user model (specifically `class User extends \RedBeanPHP\SimpleModel`, in its own namespace `Model`.) It has a method that checks the POST data to see if its correct and generates flash messages when it’s not.

---

<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:** [April 3, 2020, 9:45pm UTC](https://discourse.slimframework.com/t/accessing-request-outside-of-route/4004/6 "2020-04-03T21:45:08Z")

</div>

But a model is another layer in MVC and should handly only the business logic and data access logic and not the http specific tasks. Only a controller or a middleware is responsible for the http request / response specific tasks. You could pass the data (but not the request object) from the controller to your model instead. Then your model (layer) is decoupled from the controller (layer) and better testable.

---

<div class="post-metadata">

**Author:** ![maurobonfietti](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/maurobonfietti/32/1114_2.png) [@maurobonfietti](https://discourse.slimframework.com/u/maurobonfietti)\
**Post date:** [April 5, 2020, 10:53am UTC](https://discourse.slimframework.com/t/accessing-request-outside-of-route/4004/7 "2020-04-05T10:53:58Z")

</div>

Hi guys!

I think in Slim 4 you can make something like this, to get the request object.

```
    $container = new \DI\Container();
    AppFactory::setContainer($container);
    $app = AppFactory::create();
    $container = $app->getContainer();

    ...
    ...
    ...

    // Post Endpoint Example
    $app->post('/hi', function ($request, $response, array $args) {
        $input = $request->getParsedBody();
        $response->getBody()->write(json_encode($input));

        return $response->withHeader('Content-Type', 'application/json')->withStatus(200);
    });

```

So the first param is the $request object.  
Then, for example you can get the request data with: $request-\>getParsedBody();  
Lastly, you could pass the $input to check the POST data to your model etc, etc.
