2
2
---
3
3
4
4
适配器的作用是实现web框架context与GoAdmin自身context的转换。
5
- 制作一个adapter需要实现以下方法 :
5
+ 开发一个adapter需要实现以下接口方法 :
6
6
7
7
``` go
8
8
package adapter
@@ -12,62 +12,45 @@ import (
12
12
" github.com/GoAdminGroup/go-admin/template/types"
13
13
)
14
14
15
- // WebFrameWork is a interface which is used as an adapter of
16
- // framework and goAdmin. It must implement two methods. Use registers
17
- // the routes and the corresponding handlers. Content writes the
18
- // response to the corresponding context of framework.
19
15
type WebFrameWork interface {
20
- Use (interface {}, []plugins.Plugin ) error
21
- Content (interface {}, types.GetPanelFn )
16
+ // 返回web框架的名字
17
+ Name () string
18
+
19
+ // Use 方法将插件中的路由和控制器映射关系插入到web框架中,第一个参数为web框架引擎
20
+ Use (app interface {}, plugins []plugins.Plugin ) error
21
+
22
+ // Content 方法将回调参数返回的面板html写入到web框架的context中,也就是第一个参数
23
+ Content (ctx interface {}, fn types.GetPanelFn , navButtons ...types .Button )
24
+
25
+ // User 方法返回认证用户模型
26
+ User (ctx interface {}) (models.UserModel , bool )
27
+
28
+ // AddHandler 增加路由与控制器到web框架中
29
+ AddHandler (method, path string , handlers context.Handlers )
30
+
31
+ // 禁止web框架日志输出
32
+ DisableLog ()
33
+
34
+ // 静态目录文件服务器
35
+ Static (prefix, path string )
36
+
37
+ // 辅助函数
38
+ // ================================
39
+
40
+ SetApp (app interface {}) error
22
41
SetConnection (db.Connection )
23
42
GetConnection () db.Connection
24
43
SetContext (ctx interface {}) WebFrameWork
25
44
GetCookie () (string , error )
26
45
Path () string
27
46
Method () string
28
- PjaxHeader () string
47
+ FormParam () url.Values
48
+ IsPjax () bool
29
49
Redirect ()
30
50
SetContentType ()
31
51
Write (body []byte )
32
52
CookieKey () string
33
53
HTMLContentType () string
34
- Name () string
35
- User (ci interface {}) (models.UserModel , bool )
36
- SetApp (app interface {}) error
37
- AddHandler (method, path string , plug plugins.Plugin )
38
- }
39
- ```
40
-
41
- ## Use
42
-
43
- ** Use** 接收两个参数,第一个参数类型为** interface{}** ,是web框架的context,第二参数为插件数组。返回值是一个** error** 。
44
- ** Use** 的作用是利用传入的插件数组,将web框架的路由与将插件数组中的控制器方法关联起来,实现web框架与GoAdmin插件方法的对应。比如插件example中:
45
-
46
- ``` go
47
- " /admin/example" => ShowExample (ctx *context.Context )
48
- ```
49
-
50
- 通过``` Use ``` 的处理后将注入到web框架中。
51
-
52
- ## Content
53
-
54
- ** Content** 方法接收两个参数,第一个参数类型为** interface{}** ,是web框架的context,第二参数为** types.GetPanel** 类型。
55
-
56
- 类型如下:
57
-
58
- ``` go
59
- type GetPanel func (ctx interface {}) (Panel , error )
60
- ```
61
-
62
- **Content**的作用就是自定义页面的时候,传入web框架的context,然后往web框架的context写入自定页面内容的返回。
63
-
64
- ## init
65
-
66
- **init**方法往engine中注入该适配器,从而能够被别人去使用。
67
-
68
- ```go
69
- func init() {
70
- engine.Register (new (Beego))
71
54
}
72
55
```
73
56
0 commit comments