Open rafig256 opened 5 months ago
Hi Rafig,
Thank you for reaching out with your issue regarding the use of toastr within an API route. I'd like to clarify a few important points that might help you.
Firstly, the yoeunes/toastr
package is no longer actively maintained and i'm focusing my efforts on PHPFlasher, which is designed to offer a more comprehensive solution for flash notifications in Laravel and other frameworks. I encourage you to transition to PHPFlasher for future projects.
Regarding your current issue, both yoeunes/toastr
and PHPFlasher
rely on the session state, which is typically not available in stateless API routes. By default, these packages register their middleware under web routes that handle sessions. This might be the reason why toastr is not functioning as expected within API routes.
I'm curious about how you plan to display the toastr notifications triggered from API routes. Are you looking to send flash notification data back as part of a JSON response, or is there another mechanism you have in mind?
Looking forward to your response.
Best regards, Younes
Hello Thank you for taking the time to answer. I will definitely use phpFlasher in future projects. As for the problem I'm facing, I'm sending data to the bank GateWay. The bank in the callback function sends the information as post, and I have to give the callback address to api.php outside of the normal paths due to csrf token limitations. The path given to the callback receives a series of data from the bank GateWay and handles the data in a controller. Then it redirects the user to a page where I tried to use toastr to send the final success or failure message.
Hello Rafig,
Thank you for providing more context on your use case. I understand the challenge of triggering flash notifications in an API environment due to the stateless nature of API routes. I have a solution that should work for your specific scenario:
Since the toastr notifications rely on session data which is available only in web routes, you can redirect the user to an intermediate web route after handling the data from the bank gateway. Here’s a step-by-step explanation:
Modify the API Route: After processing the callback data from the bank in your API route, instead of trying to flash a notification directly, redirect the user to a web route. You can pass the result (success or failure) as a query parameter in the URL.
Create an Intermediate Web Route: Set up a new route in your web.php
file that catches this redirect. This route will have access to session data.
Handle the Flash Message: In the controller handling this intermediate route, extract the query parameter from the URL to determine the type of notification to display. Use toastr or PHPFlasher to flash the notification.
Redirect to the Final View: Finally, from this intermediate route, redirect the user to the final view where the toastr notification will be displayed.
Here’s a simple example of how you can implement this:
// In your API controller
public function handleBankCallback(Request $request) {
$result = // process the bank data
return redirect()->route('intermediate.route', ['result' => $result]);
}
// In your web.php
Route::get('/intermediate', function (Request $request) {
$result = $request->query('result');
flash($result === 'success' ? 'Transaction successful' : 'Transaction failed', $result);
return redirect()->route('final.view');
})->name('intermediate.route');
// In your final view
// Display the toastr notifications as you normally would
This method ensures that your flash notifications are triggered within a context that supports session state, thereby maintaining the functionality you need without breaking the stateless nature of API routes.
Let me know if this solution works for you or if you have any further questions!
Best regards, Younes
When I use the toastr during a method that is executed by a route from within the api.php file; it does not work. It works fine by all other root