fiber icon indicating copy to clipboard operation
fiber copied to clipboard

🚀 v3 Request: Better Redirection Methods

Open efectn opened this issue 2 years ago • 0 comments

Feature Description

At the moment, there're 3 methods to redirect: Redirect(), RedirectToRoute(), RedirectBack(). They're useful methods but they make Ctx more mess. To solve this, we should combine them into the one main method as Redirect()

Also, we can add flash messages to make redirection more efficent like Laravel: https://laravel.com/docs/9.x/redirects#redirecting-with-flashed-session-data.

Additional Context (optional)

This feature is also similar to @sujit-baniya's https://github.com/sujit-baniya/flash package.

Code Snippet (optional)

package main

import "github.com/gofiber/fiber/v2"
import "log"

func main() {
  app := fiber.New()

  app.Get("/test", func(c fiber.Ctx) {
    // Simple methods
    c.Redirect().To("https://google.com")
    c.Redirect().Route("homepage")
    c.Redirect().Back()

    // With flash messages
    c.Redirect().WithSuccess("test", "test2", "...").Route("homepage")
    c.Redirect().WithError("test", "test2", "...").Route("homepage")
    c.Redirect().With("message", "There's an unknown error!").Back()
    
    // may be a method to set status?
    c.Redirect().Status(302).Route("index")
  })

  log.Fatal(app.Listen(":3000"))
}

Checklist:

  • [X] I agree to follow Fiber's Code of Conduct.
  • [X] I have checked for existing issues that describe my suggestion prior to opening this one.
  • [X] I understand that improperly formatted feature requests may be closed without explanation.

efectn avatar Aug 08 '22 14:08 efectn