Hey everyone,
I've been working on a project using the SLIM API framework in PHP, and I'm facing a slight roadblock. I need to find a way to share variables across multiple PHP files, but I want these variables to be specific to each API request.
Let me explain this in a bit more detail. In my project, I have multiple PHP files that handle different API endpoints. Each endpoint requires some common variables, such as user authentication details or configuration settings. However, I want these variables to be unique to each API request.
I've tried using global variables, but they persist across different requests, which is not what I want. I want the variables to be reset or initialized afresh for each request. Is there any way to achieve this?
I've heard about middleware in SLIM API, but I'm not sure if that's the right approach or if it can solve my problem. I'd really appreciate if someone could shed some light on this matter and guide me towards the right direction.
Thanks in advance for your help!

Hey,
I encountered a similar requirement during the development of my project using SLIM API. In order to share variables across multiple PHP files but maintain request-specific values, I employed a different approach utilizing the SLIM container.
The container is a dependency injection container provided by SLIM, which allows you to register and retrieve values across different parts of your application. It acts as a centralized storage for variables that can be easily accessed whenever needed.
To get started, you can initialize the container in your main API file, like this:
With this setup, you have registered 'authToken' and 'config' variables in the container, which can then be accessed easily within any route handler.
For example, in one of your route handlers:
By utilizing the container, you can easily share variables across multiple files while ensuring that they are request-specific. This way, you don't have to worry about using global variables or creating a custom middleware.
I hope this alternative approach helps you solve your problem. If you have any further questions, feel free to ask!