Passing object from middleware to controller

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.

Yes, dependency injection :slight_smile:

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

1 Like

Can you give an example, please ?

Here is an example middleware.

OK, so injection is better than session ?

Dependency injection and sessions are completely different concepts. You may read my blog post about Slim 4 and dependency injection.

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.

Hello,

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

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.

Ok, cool :slight_smile:

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

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.

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 and fetched as data object within a user session middleware.

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 ?

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

Yes, this is due to the shared nothing architecture 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.

OK, thank you ! It really helps !