1
+ {% raw %}
1
2
# 13.3 controller设计
2
3
3
4
传统的MVC框架大多数是基于Action设计的后缀式映射,然而,现在Web流行REST风格的架构。尽管使用Filter或者rewrite能够通过URL重写实现REST风格的URL,但是为什么不直接设计一个全新的REST风格的 MVC框架呢?本小节就是基于这种思路来讲述如何从头设计一个基于REST风格的MVC框架中的controller,最大限度地简化Web应用的开发,甚至编写一行代码就可以实现“Hello, world”。
@@ -17,7 +18,7 @@ MVC设计模式是目前Web应用开发中最常见的架构模式,通过分
17
18
Layout []string
18
19
TplExt string
19
20
}
20
-
21
+
21
22
type ControllerInterface interface {
22
23
Init(ct *Context, cn string) //初始化上下文和子类名称
23
24
Prepare() //开始执行之前的一些处理
@@ -31,7 +32,7 @@ MVC设计模式是目前Web应用开发中最常见的架构模式,通过分
31
32
Finish() //执行完成之后的处理
32
33
Render() error //执行完method对应的方法之后渲染页面
33
34
}
34
-
35
+
35
36
那么前面介绍的路由add函数的时候是定义了ControllerInterface类型,因此,只要我们实现这个接口就可以,所以我们的基类Controller实现如下的方法:
36
37
37
38
func (c *Controller) Init(ct *Context, cn string) {
@@ -42,43 +43,43 @@ MVC设计模式是目前Web应用开发中最常见的架构模式,通过分
42
43
c.Ct = ct
43
44
c.TplExt = "tpl"
44
45
}
45
-
46
+
46
47
func (c *Controller) Prepare() {
47
-
48
+
48
49
}
49
-
50
+
50
51
func (c *Controller) Finish() {
51
-
52
+
52
53
}
53
-
54
+
54
55
func (c *Controller) Get() {
55
56
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
56
57
}
57
-
58
+
58
59
func (c *Controller) Post() {
59
60
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
60
61
}
61
-
62
+
62
63
func (c *Controller) Delete() {
63
64
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
64
65
}
65
-
66
+
66
67
func (c *Controller) Put() {
67
68
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
68
69
}
69
-
70
+
70
71
func (c *Controller) Head() {
71
72
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
72
73
}
73
-
74
+
74
75
func (c *Controller) Patch() {
75
76
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
76
77
}
77
-
78
+
78
79
func (c *Controller) Options() {
79
80
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
80
81
}
81
-
82
+
82
83
func (c *Controller) Render() error {
83
84
if len(c.Layout) > 0 {
84
85
var filenames []string
@@ -108,10 +109,10 @@ MVC设计模式是目前Web应用开发中最常见的架构模式,通过分
108
109
}
109
110
return nil
110
111
}
111
-
112
+
112
113
func (c *Controller) Redirect(url string, code int) {
113
114
c.Ct.Redirect(code, url)
114
- }
115
+ }
115
116
116
117
上面的controller基类已经实现了接口定义的函数,通过路由根据url执行相应的controller的原则,会依次执行如下:
117
118
@@ -125,21 +126,21 @@ MVC设计模式是目前Web应用开发中最常见的架构模式,通过分
125
126
上面beego框架中完成了controller基类的设计,那么我们在我们的应用中可以这样来设计我们的方法:
126
127
127
128
package controllers
128
-
129
+
129
130
import (
130
131
"github.com/astaxie/beego"
131
132
)
132
-
133
+
133
134
type MainController struct {
134
135
beego.Controller
135
136
}
136
-
137
+
137
138
func (this *MainController) Get() {
138
139
this.Data["Username"] = "astaxie"
139
140
this.Data["Email"] = "[email protected] "
140
141
this.TplNames = "index.tpl"
141
142
}
142
-
143
+
143
144
上面的方式我们实现了子类MainController,实现了Get方法,那么如果用户通过其他的方式(POST/HEAD等)来访问该资源都将返回403,而如果是Get来访问,因为我们设置了AutoRender=true,那么在执行完Get方法之后会自动执行Render函数,就会显示如下界面:
144
145
145
146
![ ] ( images/13.4.beego.png?raw=true )
@@ -161,3 +162,4 @@ index.tpl的代码如下所示,我们可以看到数据的设置和显示都
161
162
* [ 目录] ( < preface.md > )
162
163
* 上一章: [ 自定义路由器设计] ( < 13.2.md > )
163
164
* 下一节: [ 日志和配置设计] ( < 13.4.md > )
165
+ {% endraw %}
0 commit comments