Skip to main content

Route Registration

Function Registration

Relevant Methods:

func (s *Server) BindHandler(pattern string, handler interface{})

The handler can be defined in the following two ways:

func(request *ghttp.Request)
func(ctx context.Context, BizRequest)(BizResponse, error)

Anonymous Function and Ordinary Function Registration

internal/cmd/cmd.go

package cmd

import (
"context"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gcmd"
)

func handler(req *ghttp.Request) {
req.Response.Writeln("<h1>Hello World From handler</h1>")
}

var (
Main = gcmd.Command{
Name: "main",
Usage: "main",
Brief: "start http server",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
s := g.Server()

// Register the route directly using an anonymous function
s.BindHandler("/hello", func(req *ghttp.Request) {
req.Response.Writeln("<h1>Hello World!</h1>")
})

// Or use a predefined function for registration
s.BindHandler("/world", handler)

s.Run()
return nil
},
}
)

After successful registration, enter http://127.0.0.1:8000/hello or http://127.0.0.1:8000/world in the browser to access the corresponding route.

Specifying HTTP Request Methods

The above method registers routes that support all HTTP request methods by default. If you need to specify a request method, you can use the following syntax:

// This route only supports GET requests
s.BindHandler("GET:/hello", func(req *ghttp.Request) {
req.Response.Writeln("<h1>Hello World! GET</h1>")
})
// This route only supports POST requests
s.BindHandler("POST:/hello", func(req *ghttp.Request) {
req.Response.Writeln("<h1>Hello World! POST</h1>")
})

You can define different request methods for the same route to implement different functionalities.

Most commonly used HTTP methods:

MethodDescription
GETUsed to retrieve data without modifying server-side data
POSTSubmits resource data to the server, often used to create new data on the server
PUTSubmits resource data to the server, often used to modify existing resource data
DELETEUsed to delete server-side resource data

Object Method Registration

You can also register routes using methods from an object.

First, define a controller named user.

internal/controller/user/user.go

package user

import "github.com/gogf/gf/v2/net/ghttp"

type Controller struct{}

func New() *Controller {
return &Controller{}
}

func (c *Controller) AddUser(r *ghttp.Request) {
r.Response.Writeln("Adding user")
}

func (c *Controller) UpdateUser(r *ghttp.Request) {
r.Response.Writeln("Updating user")
}

func (c *Controller) DeleteUser(r *ghttp.Request) {
r.Response.Writeln("Deleting user")
}

func (c *Controller) ListUser(r *ghttp.Request) {
r.Response.Writeln("User list")
}

func (c *Controller) GetUser(r *ghttp.Request) {
r.Response.Writeln("Get a user")
}

func (c *Controller) Post(r *ghttp.Request) {
r.Response.Writeln("Adding user")
}

func (c *Controller) Put(r *ghttp.Request) {
r.Response.Writeln("Updating user")
}

func (c *Controller) Delete(r *ghttp.Request) {
r.Response.Writeln("Deleting user")
}

func (c *Controller) Get(r *ghttp.Request) {
r.Response.Writeln("Get a user")
}

internal/cmd/cmd.go

package cmd

import (
"context"

// Import the user controller package
"gf_demo/internal/controller/user"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcmd"
)

var (
Main = gcmd.Command{
Name: "main",
Usage: "main",
Brief: "start http server",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
s := g.Server()

// Define the object
usercontroller := user.New()
// Bind the object method to the route
s.BindHandler("/adduser", usercontroller.AddUser)

s.Run()
return nil
},
}
)

Object Registration

Methods within objects can be registered in bulk.

Relevant methods:

func (s *Server) BindObject(pattern string, object interface{}, method ...string)

func (s *Server) BindObjectMethod(pattern string, object interface{}, method string)

func (s *Server) BindObjectRest(pattern string, object interface{})

Binding All Public Methods

internal/cmd/cmd.go

package cmd

import (
"context"
"starting/internal/controller/user"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcmd"
)

var (
Main = gcmd.Command{
Name: "main",
Usage: "main",
Brief: "start http server",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
s := g.Server()

usercontroller := user.New()
// Bind all public methods in the user controller
s.BindObject("/user", usercontroller)

s.Run()
return nil
},
}
)

Binding Specific Methods

usercontroller := user.New()
// Bind multiple methods in the user controller
s.BindObject("/user", usercontroller, "AddUser,UpdateUser")
// Bind a single method
s.BindObjectMethod("/deluser", usercontroller, "DeleteUser")

Binding Object Methods in a RESTful Manner

usercontroller := user.New()
s.BindObjectRest("/user", usercontroller)

Group Registration

You can set a common prefix for different routes, known as group routes. Group routes can be written in the following two ways:

s := g.Server()

usercontroller := user.New()
s.Group("/user", func(group *ghttp.RouterGroup) {
group.Middleware(ghttp.MiddlewareHandlerResponse)
group.Bind(
usercontroller, // Bind to the controller object
)

// Define routes using GET, POST, PUT, etc.
group.GET("/get", func(r *ghttp.Request) {
r.Response.Writeln("/user/get")
})
})

s.Run()
s := g.Server()

usercontroller := user.New()
group := s.Group("/user")
group.Middleware(ghttp.MiddlewareHandlerResponse)
group.Bind(
usercontroller, // Bind to the controller object
)

// Define routes using GET, POST, PUT, etc.
group.GET("/get", func(r *ghttp.Request) {
r.Response.Writeln("/user/get")
})

s.Run()