Skip to main content

Controllers

A Controller is nothing but a function to handle the HTTP requests to the matching route.

Here is an example for a controller function to list users

#file: controllers/users.go
package controllers

import (
"git.smarteching.com/goffee/core"
)

func ListUsers(c *core.Context) *core.Response {
//The logic for listing users
}

Organizing controllers

The best way to organize controllers is to think of them like controller's actions in MVC. You can simply group all controllers of the same resource in one file and prefix the name of the controller with the name of the resource like: CreateUsers, ListUsers and so on. here is an example: Imaging we are creating an app for handling todos, in this app we have users and todos here is how we can organize the controllers of both the users and the todos

1- The controllers of users related requests goes to the file controllers/users.go

#file: controllers/users.go
package controllers

import (
"git.smarteching.com/goffee/core"
)

func CreateUsers(c *core.Context) *core.Response {
// the logic here...
}

func ListUsers(c *core.Context) *core.Response {
// the logic here...
}

func DeleteUsers(c *core.Context) *core.Response {
// the logic here...
}

1- The controllers of todos related requests goes to the file controllers/todos.go

package controllers

import (
"git.smarteching.com/goffee/core"
)

func CreateTodos(c *core.Context) *core.Response {
// the logic here...
}

func ListTodos(c *core.Context) *core.Response {
// the logic here...
}

func DeleteTodos(c *core.Context) *core.Response {
// the logic here...
}