mikestefanello / pagoda

Rapid, easy full-stack web development starter kit in Go
MIT License
2.09k stars 108 forks source link

Problem implementing redirect with querystring parameters #54

Closed Jimmy99 closed 3 months ago

Jimmy99 commented 8 months ago

I am struggling to implement a redirect with querystring parameters It seems the controller.Redirect can receive optional parameters which are empty interfaces. I've tried a few variations on this theme but the parameters never find their way into the URL. In this example the route defaults to "itemselect" when I would expect "itemselect?id=13&name=aaa" Anybody know how to implement this correctly?

var queryParam1 interface{} = "id=13" var queryParam2 interface{} = "name=aaa" return c.Redirect(ctx, "itemselect", queryParam1, queryParam2)

mikestefanello commented 8 months ago

Sorry for the confusion. The variadic argument in Redirect() are route parameters and not query parameters. It's easy to mix these up.

Route parameters, sometimes referred to as slugs, are for building the route dynamically.

For example, if you had a route to view a given user entity like:

router.GET("/user/:user_id", handler).Name = "user-profile"

To redirect to it, you would do:

ctr.Redirect(ctx, "user-profile", 123)

Which would redirect to /user/123.

The same route params are used in the Page.ToURL which can be called in the templates to generate route links. Using named parameters and this method is better (in my opinion) than hard-coding URLs all over your app.

As for supplying query params to Redirect(), that unfortunately is not currently supported but I don't see a reason why it cannot be added.

mikestefanello commented 3 months ago

On the template side, you no longer need to do {{call .ToURL}}. Instead, just {{url}} works now.

As for query param support, I added a new redirect package (https://github.com/mikestefanello/pagoda/commit/ca22f54c899fa87e8ff90c87bec90ecdc03299a6) which makes this very easy. You can use it to chain together a redirect request (including optional URL queries). The Redirect() method was removed from the controller (in fact, the entire controller was removed).

On the template side, it's probably best to just assemble the query string within the template and append it to the output of {{url}} (which you can do with sprig most likely).