Beego create simple Route and use GET method to display page content

7/23/2017 8:25 PM

In below example we are creating a simple About Us page. For which we are creating a new controller, set the route for controller, call Get method to load the page and content for that page.

Step 1:

Go to the views folder [src\demoProject\views] and create basic layout which we can use in all CMS pages. - Create homelayout.html write below code in head section.


{{.Title}}
- Add below code in body section.
 {{.LayoutContent}}
 
View layout - Now Create another file aboutTemplate.html with below code.

{{.Name}}

{{str2html .Content}}
{{.Keywords}}, {{.Description}}, {{.Title}}, {{.URL}}, {{.Name}}, {{str2html .Content}} are diffrent dynamic variables values. Values for such variables going to be initialize at time when Controller load respective Get Method. {{.LayoutContent}} loads the template file.

Step 2:

Go to the controllers folder [src\demoProject\controllers] and create common.go file in which we can import common packages and load default layout of view. - Write below code in common.go
package controllers

import (
	"github.com/astaxie/beego"
)

type Common struct {
	beego.Controller
}

func (this *Common) Prepare() {
	this.Layout = "homelayout.html"
}
- Now create AboutController this we can use to display a simple About Us page. - Write below code in about.go
package controllers

type AboutController struct {
	Common
}

func (this *AboutController) Get() {	
	this.TplName = "aboutTemplate.html"		
	this.Content()
}

func (this *AboutController) Content() {
	this.Data["URL"] = "/about"	
	this.Data["Title"] = "Software Leader in India | Technology Service Provider | Software Company"
	this.Data["Description"] = "Microsoft Corporation India is one of the fastest growing subsidiaries of Microsoft Corporation.  Microsoft in India,  is the leading technology service provider."
	this.Data["Keywords"] = "microsoft"
	this.Data["Name"] = "Impacting lives, transforming businesses"
	this.Data["Content"] = "At Microsoft, we are on a mission to empower every person and every organization on the planet to achieve more. Our way forward is to build best-in-class platforms and productivity services for a mobile-first, cloud-first world. We’ll also keep innovating to reinvent productivity & business processes, build the intelligent cloud platform and create more personal computing solutions."
}
In about AboutController we have Get() method which is loads the aboutTemplate.html and at same time Content function will pass data to various variables. The values of this.Data["URL"], this.Data["Title"], this.Data["Description"], this.Data["Keywords"], this.Data["Name"], this.Data["Content"] will display on aboutTemplate.html and homelayout.html.

Step 3:

Go to the routers folder [src\demoProject\routers] and open the router.go file. Add below line in router.go to call the AboutController's Get Method. - The updated code in router.go is as follow:
package routers

import (
	"demoProject/controllers"
	"github.com/astaxie/beego"
)

func init() {
    beego.Router("/", &controllers.MainController{})
    beego.Router("/about", &controllers.AboutController{})
}

Step 4:

Now using commond prompt go to the [src\demoProject] folder and run the commond "bee run watchall" - In browser now hit the URL http://127.0.0.1:8080/about Run Bee About Us