Home > Article > Backend Development > About Laravel global data processing
In laravel, we may process auth information through middleware, or we may design some business logic processing
After these data are processed in the middleware, where should they be stored?
For example, an object like userObject, or just a variable. Subsequent logic (service layer, model layer, view layer) may be used.
That’s it Where should the data obtained and processed in the middleware be placed. (In one request)
When not using a framework, you usually define a super variable for storage. It seems that it is not recommended to use global variables in MVC. So in Laravel, where are they usually placed?
I know it can be stored under the session app container. But it doesn’t feel like the best solution.
In laravel, we may process auth information through middleware, or we may design some business logic processing
After these data are processed in the middleware, where should they be stored?
For example, an object like userObject, or just a variable. Subsequent logic (service layer, model layer, view layer) may be used.
That’s it Where should the data obtained and processed in the middleware be placed. (In one request)
When not using a framework, you usually define a super variable for storage. It seems that it is not recommended to use global variables in MVC. So in Laravel, where are they usually placed?
I know it can be stored under the session app container. But it doesn’t feel like the best solution.
Middleware is just a pipeline used to filter requests/responses. If the data should be stored in the database, use Eloquent or directly use the DB facade. If the data should be stored, store the session...
Store in memory. For example, for the current user, when you call Auth::user
or other codes related to the current authenticated user, the corresponding UserModel may be triggered to get the user object. If it has not been generated in this request, it will be retrieved from the database or It is fetched from the cache and stored in the memory of this request.
All other variables, providers, bindings, dependency injection, aliases, etc. are all based on this logic. And it has a very flexible loading method to improve performance. Such as routing, configuration caching and loading, such as defer.
This set is based on a core thing of Laravel: container. The official documents talk about these concepts from the very beginning. For documents, see Core Concepts
In Laravel's customizable bootstrap startup process, the first step is usually to load the Application, which is a large container. You can dd(app())
anywhere to view the data loading status in the current app.