Routing

Gozen is grounded on the lightweight router package Go-chi, so all routing and their available functions are taken from there.


Basic routing

Navigate to routes > routes.go

package routes

import (
	"github.com/go-chi/chi/v5"
	"gozen/controllers/welcome"
)

func LoadRoutes(r *chi.Mux) {

	r.Get("/", welcome.Index)
	r.Post("/test", welcome.Test)

} //end

Simply add the controllers you need in the import declaration, and the string to its function call

Controllers

Now just add the directory and file welcome > welcome.go

package welcome

import (
	"gozen/system/templates"
	"net/http"
)

// welcome page
func Index(w http.ResponseWriter, r *http.Request) {

	// Render the template and write it to the response
	templates.Render(w, r, "welcome", nil)
}

If you wish to inject data into the view you can do so like this:-

func Test(w http.ResponseWriter, r *http.Request) {
	data := map[string]interface{} {
           "Foo": "testing",
        }
	// Render the template and write it to the response
	templates.Render(w, r, "forms", data)
}

When calling your view, you must omit the .html extension

To access the data 'Foo' in the above example you can add this in your html

{{ .Data.Foo }}

Gozen supports the main request methods

GET, POST, PUT, PATCH, DELETE

Route parameters

Sometimes you will need to capture segments of the URI within your route. For example, you may need to capture a user's token or ID from the URL. You may do so by defining route parameters:

r.Get("/hash/{token}", login.ConfirmHash)   <-- added to routes.go

token := chi.URLParam(r, "token")    <-- added to your controller

Redirect

If you are defining a route that redirects to another URI, you may use the example below. This method provides a convenient shortcut so that you do not have to define a full route or controller for performing a simple redirect:

http.Redirect(w, r, "/profile", http.StatusFound)

Accessing post data inside your controller

password := r.FormValue("password")
email := r.FormValue("email")