Beego(2):路由设置及使用

1、闭包路由

package routers

import (
    "beegoDemo/controllers"

    "github.com/beego/beego/v2/server/web/context"

    beego "github.com/beego/beego/v2/server/web"
)

// 闭包路由
func init() {
    // 闭包路由
    beego.Get("/test", func(c *context.Context) {
        c.Input.Context.WriteString("我是test")
    })
}

2、RESTFul 全匹配路由

package routers

import (
    "beegoDemo/controllers"
    "beegoDemo/middleware"

    "github.com/beego/beego/v2/server/web/context"

    beego "github.com/beego/beego/v2/server/web"
)

func init() {
    // 全匹配,方法名应该为Get,Post,Put,Patch,Delete
    beego.Router("/", &controllers.MainController{})
}

对应的 MainController

package controllers

import (
    beego "github.com/beego/beego/v2/server/web"
)

type MainController struct {
    beego.Controller
}

func (c *MainController) Get() {
    c.Data["Website"] = "beego.me"
    c.Data["Email"] = "astaxie@gmail.com"
    c.TplName = "index.tpl"
}
func (c *MainController) Post() {
    c.Ctx.WriteString("Post")
}
func (c *MainController) Delete() {
    c.Ctx.WriteString("Delete")
}
func (c *MainController) Patch() {
    c.Ctx.WriteString("Patch")
}
func (c *MainController) Put() {
    c.Ctx.WriteString("Put")
}

3、自定义路由

package routers

import (
    "beegoDemo/controllers"
    "beegoDemo/middleware"

    "github.com/beego/beego/v2/server/web/context"

    beego "github.com/beego/beego/v2/server/web"
)

func init() {
    // ex:http://localhost:8080/article/?name=测试
    // 要放上面
    beego.Router("/article/", &controllers.ArticleController{}, "get:Index")
    // ex:http://localhost:8080/article/1
    beego.Router("/article/?:id", &controllers.ArticleController{}, "get:Show")
    // ex:http://localhost:8080/article/1
    beego.Router("/article/?:id", &controllers.ArticleController{}, "delete:Delete")
    // ex:http://localhost:8080/article/
    beego.Router("/article/", &controllers.ArticleController{}, "post:Create")
}

对应的 ArticleController

package controllers

import (
    beego "github.com/beego/beego/v2/server/web"
)

type ArticleController struct {
    beego.Controller
}

func (c *ArticleController) Show() {
    c.Ctx.WriteString("我是show方法参数:" + c.Ctx.Input.Param(":id"))
}

func (c *ArticleController) Index() {
    c.Ctx.WriteString("我是list方法参数:" + c.GetString("name"))
}

func (c *ArticleController) Delete() {
    c.Ctx.WriteString("我是delete方法参数:" + c.GetString(":id"))
}

func (c *ArticleController) Create() {
    c.Ctx.WriteString("我是create方法参数:" + c.GetString("name"))
}

4、路由命名空间、过滤器

package routers

import (
    "beegoDemo/controllers"
    "beegoDemo/middleware"

    "github.com/beego/beego/v2/server/web/context"

    beego "github.com/beego/beego/v2/server/web"
)

func init() {
    // 命名空间
    ns := beego.NewNamespace("v1",
        beego.NSBefore(middleware.AuthFilter),
        beego.NSRouter("/user/?:id", &controllers.UserController{}, "get:Show"),
    )
    // 注册命令空间
    beego.AddNamespace(ns)
}

过滤器相当于中间件。NsBefore相当于在路由之前执行

过滤器代码

package middleware

import (
    "fmt"

    "github.com/beego/beego/v2/server/web/context"
)

func AuthFilter(ctx *context.Context) {
    fmt.Println("我是中间件")
}

UserController 

package controllers

import (
    beego "github.com/beego/beego/v2/server/web"
)

type UserController struct {
    beego.Controller
}

func (c *UserController) Show() {
    c.Ctx.WriteString("用户ID:" + c.Ctx.Input.Param(":id"))
}

5、注解路由

package routers

import (
    "beegoDemo/controllers"
    "beegoDemo/middleware"

    "github.com/beego/beego/v2/server/web/context"

    beego "github.com/beego/beego/v2/server/web"
)

func init() {
    // 注解模式
    beego.Include(&controllers.MessageController{})
}

MessageController

package controllers

import beego "github.com/beego/beego/v2/server/web"

type MessageController struct {
    beego.Controller
}

func (c *MessageController) URLMapping() {
    c.Mapping("Show", c.Show)
}

// @router /message/:id [get]
func (c *MessageController) Show() {
    c.Ctx.WriteString("MessageID" + c.Ctx.Input.Param(":id"))
}

 

posted @ 2022-05-01 19:00  wish_yang  阅读(561)  评论(0编辑  收藏  举报