@@ -33,6 +33,14 @@ const (
33
33
var (
34
34
// supported http methods.
35
35
HTTPMETHOD = []string {"get" , "post" , "put" , "delete" , "patch" , "options" , "head" }
36
+ // these beego.Controller's methods shouldn't reflect to AutoRouter
37
+ exceptMethod = []string {"Init" , "Prepare" , "Finish" , "Render" , "RenderString" ,
38
+ "RenderBytes" , "Redirect" , "Abort" , "StopRun" , "UrlFor" , "ServeJson" , "ServeJsonp" ,
39
+ "ServeXml" , "Input" , "ParseForm" , "GetString" , "GetStrings" , "GetInt" , "GetBool" ,
40
+ "GetFloat" , "GetFile" , "SaveToFile" , "StartSession" , "SetSession" , "GetSession" ,
41
+ "DelSession" , "SessionRegenerateID" , "DestroySession" , "IsAjax" , "GetSecureCookie" ,
42
+ "SetSecureCookie" , "XsrfToken" , "CheckXsrfCookie" , "XsrfFormHtml" ,
43
+ "GetControllerAndAction" }
36
44
)
37
45
38
46
type controllerInfo struct {
@@ -221,8 +229,8 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
221
229
// Add auto router to ControllerRegistor.
222
230
// example beego.AddAuto(&MainContorlller{}),
223
231
// MainController has method List and Page.
224
- // visit the url /main/list to exec List function
225
- // /main/page to exec Page function.
232
+ // visit the url /main/list to execute List function
233
+ // /main/page to execute Page function.
226
234
func (p * ControllerRegistor ) AddAuto (c ControllerInterface ) {
227
235
p .enableAuto = true
228
236
reflectVal := reflect .ValueOf (c )
@@ -235,7 +243,32 @@ func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
235
243
p .autoRouter [firstParam ] = make (map [string ]reflect.Type )
236
244
}
237
245
for i := 0 ; i < rt .NumMethod (); i ++ {
238
- p .autoRouter [firstParam ][rt .Method (i ).Name ] = ct
246
+ if ! utils .InSlice (rt .Method (i ).Name , exceptMethod ) {
247
+ p .autoRouter [firstParam ][rt .Method (i ).Name ] = ct
248
+ }
249
+ }
250
+ }
251
+
252
+ // Add auto router to ControllerRegistor with prefix.
253
+ // example beego.AddAutoPrefix("/admin",&MainContorlller{}),
254
+ // MainController has method List and Page.
255
+ // visit the url /admin/main/list to execute List function
256
+ // /admin/main/page to execute Page function.
257
+ func (p * ControllerRegistor ) AddAutoPrefix (prefix string , c ControllerInterface ) {
258
+ p .enableAuto = true
259
+ reflectVal := reflect .ValueOf (c )
260
+ rt := reflectVal .Type ()
261
+ ct := reflect .Indirect (reflectVal ).Type ()
262
+ firstParam := strings .Trim (prefix , "/" ) + "/" + strings .ToLower (strings .TrimSuffix (ct .Name (), "Controller" ))
263
+ if _ , ok := p .autoRouter [firstParam ]; ok {
264
+ return
265
+ } else {
266
+ p .autoRouter [firstParam ] = make (map [string ]reflect.Type )
267
+ }
268
+ for i := 0 ; i < rt .NumMethod (); i ++ {
269
+ if ! utils .InSlice (rt .Method (i ).Name , exceptMethod ) {
270
+ p .autoRouter [firstParam ][rt .Method (i ).Name ] = ct
271
+ }
239
272
}
240
273
}
241
274
0 commit comments