# Passing object from middleware to controller

**URL:** <https://discourse.slimframework.com/t/passing-object-from-middleware-to-controller/4034>\
**Category:** Questions\
**Created:** [April 9, 2020, 12:50pm UTC](https://discourse.slimframework.com/t/passing-object-from-middleware-to-controller/4034 "2020-04-09T12:50:31Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![thierryler](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/thierryler/32/1115_2.png) [@thierryler](https://discourse.slimframework.com/u/thierryler)\
**Post date:** [April 9, 2020, 12:50pm UTC](https://discourse.slimframework.com/t/passing-object-from-middleware-to-controller/4034/1 "2020-04-09T12:50:32Z")

</div>

Hello,

I create an Foo Object in a (before) Middleware. I need to use this object later in a controller. Is it better to store this object into the container or into the session ?

In my middleware :  
$foo = new Foo(…)

$\_SESSION[‘foo’] = $foo;  
or  
$this-\>container-\>set(‘foo’, $foo);

Later, in my controller :  
public function \_\_construct(Container $container) {  
$this-\>container = $container;  
$this-\>foo = $container-\>get(‘foo’);  
or  
this-\>foo = \_SESSION[‘foo’]  
unset($\_SESSION[‘foo’]);

Or maybe there is something better…

Th.

---

<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 9, 2020, 12:57pm UTC](https://discourse.slimframework.com/t/passing-object-from-middleware-to-controller/4034/2 "2020-04-09T12:57:26Z")

</div>

> [@thierryler](#):
>
> Or maybe there is something better…

Yes, dependency injection 🙂

Injecting the container is an service locator “anti-pattern” and should be avoided.

---

<div class="post-metadata">

**Author:** ![thierryler](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/thierryler/32/1115_2.png) [@thierryler](https://discourse.slimframework.com/u/thierryler)\
**Post date:** [April 9, 2020, 1:00pm UTC](https://discourse.slimframework.com/t/passing-object-from-middleware-to-controller/4034/3 "2020-04-09T13:00:35Z")

</div>

Can you give an example, please ?

---

<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 9, 2020, 1:21pm UTC](https://discourse.slimframework.com/t/passing-object-from-middleware-to-controller/4034/4 "2020-04-09T13:21:13Z")

</div>

Here is an [example](https://github.com/odan/slim4-skeleton/blob/master/src/Middleware/UserAuthMiddleware.php) middleware.

---

<div class="post-metadata">

**Author:** ![thierryler](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/thierryler/32/1115_2.png) [@thierryler](https://discourse.slimframework.com/u/thierryler)\
**Post date:** [April 9, 2020, 3:15pm UTC](https://discourse.slimframework.com/t/passing-object-from-middleware-to-controller/4034/5 "2020-04-09T15:15:18Z")

</div>

OK, so injection is better than session ?

---

<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 9, 2020, 7:35pm UTC](https://discourse.slimframework.com/t/passing-object-from-middleware-to-controller/4034/6 "2020-04-09T19:35:25Z")

</div>

Dependency injection and sessions are completely different concepts. You may read my blog post about [Slim 4 and dependency injection](https://odan.github.io/2019/11/05/slim4-tutorial.html#container).

You should first set up the container by adding container definitions. Then declare what you need for the class in your constructor parameter list. The dependency injection container will then create and inject the required instances for you. You then have access to the session handler, the twig template Engine and so on.

---

<div class="post-metadata">

**Author:** ![thierryler](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/thierryler/32/1115_2.png) [@thierryler](https://discourse.slimframework.com/u/thierryler)\
**Post date:** [April 9, 2020, 8:50pm UTC](https://discourse.slimframework.com/t/passing-object-from-middleware-to-controller/4034/7 "2020-04-09T20:50:16Z")

</div>

Hello,

I no longer inject the container, as you suggested. It looks better 🙂

Your article is very nice.

Actually, on my middleware, I create the Foo object, and I plane to use the Foo object in the next middlewares, and in the controller. The Foo object has to be re-created for each new request.

I have many controllers/actions (HelloAction, ContactAction, etc.) that all extend MyAction. So I perform Foo in the constructor of MyAction. I means I cannot transfert Foo throught the request. So session or injection…

Th.

---

<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 9, 2020, 9:00pm UTC](https://discourse.slimframework.com/t/passing-object-from-middleware-to-controller/4034/8 "2020-04-09T21:00:55Z")

</div>

Ok, cool 🙂

May I ask what kind of object Foo is? Then I am able to give a more precise answer.

---

<div class="post-metadata">

**Author:** ![thierryler](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/thierryler/32/1115_2.png) [@thierryler](https://discourse.slimframework.com/u/thierryler)\
**Post date:** [April 10, 2020, 7:32am UTC](https://discourse.slimframework.com/t/passing-object-from-middleware-to-controller/4034/9 "2020-04-10T07:32:54Z")

</div>

Yes, any good idea will be welcome. In Foo, I store flash messages, ip, useragent, security codes, lang, etc. Some of those fields are extracted from the request, which is not provided to my controller constructor (maybe there is a way), and i plan to perform a part of the tretement in other before middlewares.

---

<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 10, 2020, 10:18am UTC](https://discourse.slimframework.com/t/passing-object-from-middleware-to-controller/4034/10 "2020-04-10T10:18:15Z")

</div>

It sounds to me like this Foo object is doing too much. I would move the responsibility for the flash message into the session handler. The other fields belong to the logged in user. This user session data could be [created at login time](https://github.com/odan/slim4-skeleton/blob/master/src/Action/Login/LoginSubmitAction.php#L54) and fetched as data object within a [user session middleware](https://github.com/odan/slim4-skeleton/blob/master/src/Middleware/UserAuthMiddleware.php).

---

<div class="post-metadata">

**Author:** ![thierryler](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/thierryler/32/1115_2.png) [@thierryler](https://discourse.slimframework.com/u/thierryler)\
**Post date:** [April 11, 2020, 6:39am UTC](https://discourse.slimframework.com/t/passing-object-from-middleware-to-controller/4034/11 "2020-04-11T06:39:30Z")

</div>

I guest you’re right.

So I understand that this type of data is better in session, and helpers/actions should be injected.

Another question: the injected objects are recreated for each new request, are they ? And nothing is shared beetwin users/clients, right ?

---

<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 11, 2020, 5:19pm UTC](https://discourse.slimframework.com/t/passing-object-from-middleware-to-controller/4034/12 "2020-04-11T17:19:42Z")

</div>

> the injected objects are recreated for each new request, are they

Yes, this is due to the [shared nothing architecture](https://tideways.com/profiler/blog/php-shared-nothing-architecture-the-benefits-and-downsides) of PHP itself. It has nothing to do with Slim.

> And nothing is shared beetwin users/clients, right ?

As long as you don’t use session cookies, the request is “stateless” in PHP, like the web.  
If you use cookies, then it’s not stateless anymore and PHP will recover the state of the **session** in the next request. Your container and all objects will be recreated for each request. Depending on your application (middleware) you can read data from the session and pass / copy some data from it to another object, e.g a User Authentication component.

---

<div class="post-metadata">

**Author:** ![thierryler](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/thierryler/32/1115_2.png) [@thierryler](https://discourse.slimframework.com/u/thierryler)\
**Post date:** [April 11, 2020, 7:13pm UTC](https://discourse.slimframework.com/t/passing-object-from-middleware-to-controller/4034/13 "2020-04-11T19:13:13Z")

</div>

OK, thank you ! It really helps !
