Dependencies.php vs Middleware

I have a question for which there may not be a definitive/correct answer but I’d figure I’d try my luck to get some insightful opinions:

I have a dependencies.php which initializes things like

  • DB Connection (*)
  • Session (*)
  • Logger (*)
  • CSRF (*)
  • Mailer (*)
  • 500/404 handlers
  • etc.

I also have a bunch of middlewares which set up the following:

  • Role based Auth Checking
  • Custom class loader
  • Caching
  • View/Twig initialization

My problem is that the stuff in dependencies.php is just a long list of raw PHP code (ie: no classes, just a list of PHP commands), while the middlewares are nicely encapsulated classes. It seems to me that just about anything done in dependencies.php (basically the stuff marked with a (*)) could all be moved to middleware which (I think) would result in a nicer architecture since I’d be able to avoid the raw PHP stuff in dependencies.php.

Does anybody have any opinions/advice regarding such architecture decisions? Am I overzealous here? Is there a practical reason for keeping the raw PHP code in dependencies.php?

Thanks in advance!

I opted to wrap dependencies and settings in a config class
the class load parameters from an external file
I use .json for readability and easy transformation in php objects

IMHO you should not “misapply” the middleware as a “factory” or as a replacement for the container. A middleware is a different concept and is responsible for the the request/response related things. The container (in dependencies.php) is responsible to create and configure the required objects (some kind of a factory). I would recommend: Better leave it as it is.

1 Like

I opted to wrap dependencies and settings in a config class the class load parameters from an external file I use .json for readability and easy transformation in php objects

That’s a good idea … thank you for that

IMHO you should not “misapply” the middleware as a “factory” or as a replacement for the container. A middleware is a different concept and is responsible for the the request/response related things. The container (in dependencies.php) is responsible to create and configure the required objects (some kind of a factory). I would recommend: Better leave it as it is.

Yeah, I agree with that sentiment as well. Thank you for your feedback.

1 Like