11<?php
22
33/*
4- * Copyright (C) 2014 - 2025 , Biospex
4+ * Copyright (C) 2014 - 2026 , Biospex
5566 *
77 * This program is free software: you can redistribute it and/or modify
2222
2323use Closure ;
2424use Illuminate \Http \Request ;
25- use JavaScript ;
2625
2726class FlashHelperMessage
2827{
@@ -31,72 +30,46 @@ class FlashHelperMessage
3130 */
3231 public function handle (Request $ request , Closure $ next ): mixed
3332 {
34- $ message = [
35- 'flashType ' => '' ,
36- 'flashMessage ' => '' ,
37- 'flashIcon ' => '' ,
38- ];
39-
40- $ status = ['success ' , 'info ' , 'warning ' , 'danger ' ];
41- if (session ()->hasAny ('success ' , 'info ' , 'warning ' , 'danger ' )) {
42- foreach ($ status as $ type ) {
43- if (session ()->has ($ type )) {
44- $ message ['flashType ' ] = $ type ;
45- $ message ['flashMessage ' ] = session ($ type );
46- $ message ['flashIcon ' ] = match ($ type ) {
47- 'success ' => 'check-circle ' ,
48- 'info ' => 'info-circle ' ,
49- 'warning ' => 'exclamation-circle ' ,
50- 'danger ' => 'times-circle ' ,
51- };
52- break ;
53- }
54- }
33+ // If there's a flash message, tell the Cache middleware (running next) to skip this request
34+ if (session ()->hasAny (['success ' , 'info ' , 'warning ' , 'danger ' ])) {
35+ $ request ->attributes ->set ('laravel-responsecache.do-not-cache ' , true );
5536 }
5637
57- JavaScript:: put ( $ message );
38+ $ response = $ next ( $ request );
5839
59- return $ next ($ request);
60- }
40+ if ($ request-> isMethod ( ' GET ' )) {
41+ $ status = [ ' success ' , ' info ' , ' warning ' , ' danger ' ];
6142
62- private function create ($ message , $ type , $ icon )
63- {
64- session ()->flash ('flash_message ' , [
65- 'type ' => $ type ,
66- 'message ' => $ message ,
67- 'icon ' => $ icon ,
68- ]);
69- }
43+ foreach ($ status as $ type ) {
44+ if (session ()->has ($ type )) {
45+ // 2. Also set the header on the outgoing response for safety
46+ $ response ->headers ->set ('laravel-responsecache ' , 'do-not-cache ' );
7047
71- /**
72- * Create success message.
73- */
74- public function success ($ message )
75- {
76- $ this ->create ($ message , 'success ' , 'check-circle ' );
77- }
48+ $ payload = json_encode ([
49+ 'type ' => $ type ,
50+ 'message ' => (string ) session ($ type ),
51+ 'icon ' => match ($ type ) {
52+ 'success ' => 'check-circle ' ,
53+ 'info ' => 'info-circle ' ,
54+ 'warning ' => 'exclamation-circle ' ,
55+ 'danger ' => 'times-circle ' ,
56+ default => 'info-circle ' ,
57+ },
58+ ]);
7859
79- /**
80- * Create info message.
81- */
82- public function info ($ message )
83- {
84- $ this ->create ($ message , 'info ' , 'info-circle ' );
85- }
60+ // Create a simple session cookie (minutes=0)
61+ // No domain, No secure (for local testing), No httpOnly
62+ $ response ->withCookie (cookie ('app_flash ' , $ payload , 0 , '/ ' , null , false , false , false , 'Lax ' ));
8663
87- /**
88- * Create warning message.
89- */
90- public function warning ($ message )
91- {
92- $ this ->create ($ message , 'warning ' , 'exclamation-circle ' );
93- }
64+ session ()->forget ($ type );
9465
95- /**
96- * Create danger message.
97- */
98- public function error ($ message )
99- {
100- $ this ->create ($ message , 'danger ' , 'times-circle ' );
66+ // Forget the session key so it doesn't persist
67+ session ()->forget ($ type );
68+ break ;
69+ }
70+ }
71+ }
72+
73+ return $ response ;
10174 }
10275}
0 commit comments