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クラスを定義しました。ですので、ここではこのインターフェースを実装すれば十分です。基底クラスのContorollerの実装は以下のようなメソッドになります:
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