5
5
package fiber
6
6
7
7
import (
8
+ "fmt"
9
+
8
10
"github.com/valyala/bytebufferpool"
9
11
)
10
12
11
13
// Redirect is a struct to use it with Ctx.
12
14
type Redirect struct {
13
- // Embed ctx
14
- c * DefaultCtx
15
- status int // Default: StatusFound
15
+ c * DefaultCtx // Embed ctx
16
+ status int // Status code of redirection. Default: StatusFound
17
+ messages Map // Flash messages
16
18
}
17
19
18
20
// A config to use with Redirect().Route()
@@ -26,8 +28,9 @@ type RedirectConfig struct {
26
28
// Return default Redirect reference.
27
29
func newRedirect (c * DefaultCtx ) * Redirect {
28
30
return & Redirect {
29
- c : c ,
30
- status : StatusFound ,
31
+ c : c ,
32
+ status : StatusFound ,
33
+ messages : make (Map , 0 ),
31
34
}
32
35
}
33
36
@@ -39,6 +42,14 @@ func (r *Redirect) Status(code int) *Redirect {
39
42
return r
40
43
}
41
44
45
+ // You can send flash messages by using With().
46
+ // They will be sent as a cookie.
47
+ func (r * Redirect ) With (key string , value any ) * Redirect {
48
+ r .messages [key ] = value
49
+
50
+ return r
51
+ }
52
+
42
53
// Redirect to the URL derived from the specified path, with specified status.
43
54
func (r * Redirect ) To (location string ) error {
44
55
r .c .setCanonical (HeaderLocation , location )
@@ -62,6 +73,22 @@ func (r *Redirect) Route(name string, config ...RedirectConfig) error {
62
73
return err
63
74
}
64
75
76
+ // Flash messages
77
+ if len (r .messages ) > 0 {
78
+ messageText := bytebufferpool .Get ()
79
+ defer bytebufferpool .Put (messageText )
80
+
81
+ for k , v := range r .messages {
82
+ messageText .WriteString ("k:" + k + ":" + fmt .Sprint (v ) + "," )
83
+ }
84
+
85
+ r .c .Cookie (& Cookie {
86
+ Name : "fiber_flash" ,
87
+ Value : messageText .String (),
88
+ SessionOnly : true ,
89
+ })
90
+ }
91
+
65
92
// Check queries
66
93
if len (cfg .Queries ) > 0 {
67
94
queryText := bytebufferpool .Get ()
0 commit comments