I want to implement a user tracking system, after searching I realized that I have to do this in the middleware. The problem I had was that I could only add values to the response header in the answers that were in the middleware. And after saving their information, delete them in the middleware response. Please guide me how to add custom data to the middleware response, thanks
For this, I found 2 examples that work in json mode, but in the case that I want to transfer the user to the route, I have to use headers.
Sample code for json response:
return $response->json([
'status' => $status,
'code' => $code,
'errors' => [$errors],
], $status);
I did exactly the same system in my project and without using headers, please see the example below
for example: We plan to create a tracking system for each user's performance in the software so that its reports are stored in the system.
Migration:
RoleController:
ResponseTrait:
tip: In the middleware response, you will receive 2 types of responses, which are made of 2 classes (Illuminate\Http\Response, Illuminate\Http\RedirectResponse), so we must create 2 custom classes for this purpose and extend these 2 classes.
Create Custom Response:
1- Class One:
2- Class Two:
We create the User Tracking storage trait
Now we go to build our custom middleware
Custom middleware:
The point of this guide is that you should create a custom class of your response and extend it from (Illuminate\Http\Response, Illuminate\Http\RedirectResponse) classes. With this trick, you can send and process any value you like. Good luck