Skip to content

Commit af153e8

Browse files
author
cg33
committed
update
1 parent 04855ca commit af153e8

File tree

5 files changed

+194
-125
lines changed

5 files changed

+194
-125
lines changed

SUMMARY.md

-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,5 @@
7373
- [插件](zh/development/plugins.md)
7474
- [模板开发](zh/development/template/template.md)
7575
- [模板介绍](zh/development/template/template.md)
76-
- [表单组件开发](zh/development/template/form.md)
7776
- [组件开发](zh/development/template/components.md)
7877
- [发展规划](zh/plan.md)

zh/development/adapter.md

+28-45
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
---
33

44
适配器的作用是实现web框架context与GoAdmin自身context的转换。
5-
制作一个adapter需要实现以下方法
5+
开发一个adapter需要实现以下接口方法
66

77
```go
88
package adapter
@@ -12,62 +12,45 @@ import (
1212
"github.com/GoAdminGroup/go-admin/template/types"
1313
)
1414

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.
1915
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
2241
SetConnection(db.Connection)
2342
GetConnection() db.Connection
2443
SetContext(ctx interface{}) WebFrameWork
2544
GetCookie() (string, error)
2645
Path() string
2746
Method() string
28-
PjaxHeader() string
47+
FormParam() url.Values
48+
IsPjax() bool
2949
Redirect()
3050
SetContentType()
3151
Write(body []byte)
3252
CookieKey() string
3353
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))
7154
}
7255
```
7356

zh/development/plugins.md

+49-15
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,65 @@
11
# 插件开发
22
---
33

4-
一个插件需要实现对应的三个接口,如:
4+
一个插件需要实现对应的四个接口,如:
55

66
```go
7-
// Plugin as one of the key components of goAdmin has three
8-
// methods. GetRequest return all the path registered in the
9-
// plugin. GetHandler according the url and method return the
10-
// corresponding handler. InitPlugin init the plugin which do
11-
// something like init the database and set the config and register
12-
// the routes. The Plugin must implement the three methods.
7+
// 插件是GoAdmin的重要组成部分。不同插件有不同的功能特性。
138
type Plugin interface {
149

15-
// 以下这两个接口内容基本是固定的,可参考后面的example插件,照写即可
16-
17-
// 获取请求
18-
GetRequest() []context.Path
19-
2010
// 获取控制器方法
21-
GetHandler(url, method string) context.Handlers
11+
GetHandler() context.HandlerMap
2212

2313
// 初始化插件,接口框架的服务列表
24-
InitPlugin(services service.List)
14+
InitPlugin(services service.List)
15+
16+
// 插件名字
17+
Name() string
18+
19+
// 插件路由前缀
20+
Prefix() string
21+
}
22+
```
23+
24+
新建一个自己的插件必须继承```plugin.Base```。如以下例子:
25+
26+
```go
27+
package example
28+
29+
import (
30+
c "github.com/GoAdminGroup/go-admin/modules/config"
31+
"github.com/GoAdminGroup/go-admin/modules/service"
32+
"github.com/GoAdminGroup/go-admin/plugins"
33+
)
34+
35+
type Example struct {
36+
*plugins.Base
37+
}
38+
39+
func NewExample() *Example {
40+
return &Example{
41+
Base: &plugins.Base{PlugName: "example", URLPrefix: "example"},
42+
}
43+
}
44+
45+
func (e *Example) InitPlugin(srv service.List) {
46+
e.InitBase(srv)
47+
e.App = e.initRouter(c.Prefix(), srv)
48+
}
49+
50+
func (e *Example) initRouter(prefix, srv service.List) {
51+
52+
app := context.NewApp()
53+
route := app.Group(prefix)
54+
// 加入认证中间件
55+
route.GET("/show/me/something", auth.Middleware(db.GetConnection(srv)), func(ctx *context.Context){
56+
// 控制器逻辑
57+
})
58+
return app
2559
}
2660
```
2761

28-
参考例子:<br>
62+
更多源码:<br>
2963

3064
- [开发一个源码插件](https://github.com/GoAdminGroup/go-admin/blob/master/plugins/example/example.go)
3165
- [开发一个二进制插件](https://github.com/GoAdminGroup/go-admin/blob/master/plugins/example/go_plugin/main.go)

0 commit comments

Comments
 (0)