mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
完善示例,改进ghttp控制器方法注册
This commit is contained in:
@ -425,6 +425,13 @@ func (s *Server)BindControllerRest(pattern string, c Controller) error {
|
||||
// 绑定控制器方法,pattern支持http method
|
||||
// pattern的格式形如:/user/list, put:/user, delete:/user
|
||||
// 这种方式绑定的控制器每一次请求都会初始化一个新的控制器对象进行处理,对应不同的请求会话
|
||||
func (s *Server)BindControllerMethod(pattern string, c Controller, method string) error {
|
||||
return s.bindHandlerItem(pattern, HandlerItem{reflect.ValueOf(c).Elem().Type(), method, nil})
|
||||
// 第三个参数methods支持多个方法注册,多个方法以英文“,”号分隔
|
||||
func (s *Server)BindControllerMethod(pattern string, c Controller, methods string) error {
|
||||
for _, method := range strings.Split(methods, ",") {
|
||||
item := HandlerItem{reflect.ValueOf(c).Elem().Type(), strings.TrimSpace(method), nil}
|
||||
if err := s.bindHandlerItem(pattern, item); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -91,10 +91,12 @@ func (d *Domain) BindControllerRest(pattern string, c Controller) error {
|
||||
}
|
||||
|
||||
// 绑定控制器方法
|
||||
func (d *Domain) BindControllerMethod(pattern string, c Controller, method string) error {
|
||||
func (d *Domain) BindControllerMethod(pattern string, c Controller, methods string) error {
|
||||
for domain, _ := range d.m {
|
||||
if err := d.s.BindControllerMethod(pattern + "@" + domain, c, method); err != nil {
|
||||
return err
|
||||
for _, method := range strings.Split(methods, ",") {
|
||||
if err := d.s.BindControllerMethod(pattern + "@" + domain, c, strings.TrimSpace(method)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@ -1,9 +1,6 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"gitee.com/johng/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
import "gitee.com/johng/gf/g/net/ghttp"
|
||||
|
||||
// 初始化控制器对象,并绑定操作到Web Server
|
||||
func init() {
|
||||
|
||||
29
geg/frame/mvc/controller/demo/method.go
Normal file
29
geg/frame/mvc/controller/demo/method.go
Normal file
@ -0,0 +1,29 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"gitee.com/johng/gf/g/net/ghttp"
|
||||
"gitee.com/johng/gf/g/frame/gmvc"
|
||||
)
|
||||
|
||||
type ControllerMethod struct {
|
||||
gmvc.Controller
|
||||
}
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindControllerMethod("/method", &ControllerMethod{}, "Name, Age")
|
||||
}
|
||||
|
||||
func (c *ControllerMethod) Name() {
|
||||
c.Response.WriteString("John")
|
||||
}
|
||||
|
||||
func (c *ControllerMethod) Age() {
|
||||
c.Response.WriteString("18")
|
||||
}
|
||||
|
||||
func (c *ControllerMethod) Info() {
|
||||
c.Response.WriteString("Info")
|
||||
}
|
||||
|
||||
|
||||
|
||||
14
geg/frame/mvc/controller/demo/object.go
Normal file
14
geg/frame/mvc/controller/demo/object.go
Normal file
@ -0,0 +1,14 @@
|
||||
package demo
|
||||
|
||||
import "gitee.com/johng/gf/g/net/ghttp"
|
||||
|
||||
type Object struct {}
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindObject("/object", &Object{})
|
||||
}
|
||||
|
||||
func (o *Object) Show(s *ghttp.Server, r *ghttp.ClientRequest, w *ghttp.ServerResponse) {
|
||||
w.WriteString("It's show time bibi!")
|
||||
}
|
||||
|
||||
15
geg/frame/mvc/controller/demo/object_rest.go
Normal file
15
geg/frame/mvc/controller/demo/object_rest.go
Normal file
@ -0,0 +1,15 @@
|
||||
package demo
|
||||
|
||||
import "gitee.com/johng/gf/g/net/ghttp"
|
||||
|
||||
// 测试绑定对象
|
||||
type ObjectRest struct {}
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindObjectRest("/object-rest", &ObjectRest{})
|
||||
}
|
||||
|
||||
func (o *ObjectRest) Get(s *ghttp.Server, r *ghttp.ClientRequest, w *ghttp.ServerResponse) {
|
||||
w.WriteString("It's show time bibi!")
|
||||
}
|
||||
|
||||
@ -1,9 +1,8 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"gitee.com/johng/gf/g/frame/gmvc"
|
||||
|
||||
"gitee.com/johng/gf/g/net/ghttp"
|
||||
"gitee.com/johng/gf/g/frame/gmvc"
|
||||
)
|
||||
|
||||
// 测试控制器
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
|
||||
"gitee.com/johng/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
// 测试绑定对象
|
||||
type T struct {}
|
||||
|
||||
// 初始化控制器对象,并绑定操作到Web Server
|
||||
func init() {
|
||||
// 绑定对象,对象中的公开方法将会自动绑定到指定的URI末尾
|
||||
// ghttp.GetServer().BindObject("/test", &T{})
|
||||
// 只有localhost域名下才能访问该对象,
|
||||
// 对应URL为:http://localhost:8199/test/show
|
||||
// 通过该地址将无法访问到内容:http://127.0.0.1:8199/test/show
|
||||
ghttp.GetServer().Domain("localhost").BindObject("/test", &T{})
|
||||
}
|
||||
|
||||
// 用于对象映射
|
||||
func (t *T) Show(s *ghttp.Server, r *ghttp.ClientRequest, w *ghttp.ServerResponse) {
|
||||
w.WriteString("It's show time bibi!")
|
||||
}
|
||||
|
||||
@ -1,9 +1,8 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
|
||||
"gitee.com/johng/gf/g/frame/gmvc"
|
||||
"gitee.com/johng/gf/g/net/ghttp"
|
||||
"gitee.com/johng/gf/g/frame/gmvc"
|
||||
)
|
||||
|
||||
// 定义业务相关的控制器对象,
|
||||
@ -19,6 +18,11 @@ func init() {
|
||||
ghttp.GetServer().BindController("/user", &ControllerUser{})
|
||||
}
|
||||
|
||||
// 定义操作逻辑 - 展示姓名
|
||||
func (c *ControllerUser) Name() {
|
||||
c.Response.WriteString("John")
|
||||
}
|
||||
|
||||
// 定义操作逻辑 - 展示模板
|
||||
func (c *ControllerUser) Info() {
|
||||
c.View.Assign("name", "john")
|
||||
|
||||
Reference in New Issue
Block a user