Search⌘ K
AI Features

First Controller: The Notes Index Page

Explore how to build the first controller and route for a notes index page using Beego and Golang. Understand creating custom handler functions, fetching data with ORM, and rendering templates to establish the foundational interaction in your web application.

Route creation

In this lesson, we will create the first route for our Beego application. Routes are essential in web applications because they define how incoming requests should be handled. By creating this route, we establish a path for users to access specific functionalities or resources within our application, ensuring that their requests are directed to the appropriate handlers and controllers. This foundational step enables users to seamlessly interact with our application.

Let’s start by defining a new route in the file routers/route.go.

Go (1.18.2)
package routers
import (
"beego_notes/controllers"
beego "github.com/beego/beego/v2/server/web"
)
func init() {
beego.Router("/", &controllers.MainController{})
beego.Router("/notes", &controllers.NotesController{}, "get:NotesIndex")
}

In the above code:

  • Line 10: This line registers the route for the notes page. The notes page is served by the NotesController controller. The get:NotesIndex method is the action that is executed when the notes ...