04.路由
# 01.动态路由和正则路由
# 1.1 控制器
package controllers
import (
"fmt"
"github.com/astaxie/beego"
)
type GoodsController struct {
beego.Controller // 控制器必须要继承 beego.Controller
}
func (c *GoodsController) GetGoods() {
p := c.Ctx.Input.Param(":id")
fmt.Println(p) // 111
c.Ctx.WriteString("获取商品")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 1.2 配置路由
func init() {
// 1.普通路由: http://127.0.0.1:8080/goods/
beego.Router("/goods", &controllers.GoodsController{},"get:GetGoods")
// 2.常用动态路由: http://127.0.0.1:8080/goods/111
beego.Router("/goods/:id", &controllers.GoodsController{},"get:GetGoods")
// 3.匹配 cms_123.html 这样的 url :id = 123
beego.Router("/goods/cms_:id([0-9]+).html", &controllers.GoodsController{},"get:GetGoods")
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 02.路由跳转
func (c *ArticleController) ArticleJson() {
c.Ctx.Redirect(302, "/article/xml")
return
}
1
2
3
4
2
3
4
上次更新: 2024/3/13 15:35:10