1.自动匹配
最后更新于:2022-04-02 04:53:13
在项目中
新建admin模块,在新建模块下
新建controllers文件夹,在新建文件夹下
新建router.go
目录结构:
~~~
project
|
|-- admin
| |--controllers
| `-- user.go
|-- conf
| `-- app.conf
|-- controllers
| `-- default.go
|-- main.go
|-- models
|-- routers
| `-- router.go
|-- static
| |-- css
| |-- img
| `-- js
|-- tests
| `-- default_test.go
`-- views
`-- index.tpl
~~~
自动匹配
~~~
|-- routers
| `-- router.go
~~~
代码:
~~~
package routers
import (
admin "project/admin/controllers"
"github.com/astaxie/beego"
)
func init() {
// 自动匹配
beego.AutoRouter(&admin.UserController{})
}
~~~
~~~
|-- admin
| |--controllers
| `-- user.go
~~~
代码:
~~~
package admin
import (
"fmt"
"github.com/astaxie/beego"
)
type UserController struct {
beego.Controller
}
func (this *UserController) Index() {
this.Ctx.WriteString("这是自动匹配路由 user/index")
}
func (this *UserController) Test() {
values := this.Ctx.Input.Params()
str := fmt.Sprintln(values)
this.Ctx.WriteString("这是自动匹配路由 user/test , values is " + str)
}
~~~
浏览器访问:
http://127.0.0.1:8080/user/index
浏览器输出:
这是自动匹配路由 user/index
浏览器访问:
http://127.0.0.1:8080/user/test/user1/123456/28
浏览器输出:
这是自动匹配路由 user/test , values is map[:splat:user1/123456/28 0:user1 1:123456 2:28]
';