1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 17:21:02 +00:00

beego:fix router expge

This commit is contained in:
astaxie 2014-06-18 23:32:47 +08:00
parent c3a07555c4
commit 085c362ffb
5 changed files with 24 additions and 3 deletions

View File

@ -428,7 +428,9 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin
if c.routerType == routerTypeBeego && c.controllerType.Name() == controllName { if c.routerType == routerTypeBeego && c.controllerType.Name() == controllName {
find := false find := false
if _, ok := HTTPMETHOD[strings.ToUpper(methodName)]; ok { if _, ok := HTTPMETHOD[strings.ToUpper(methodName)]; ok {
if m, ok := c.methods[strings.ToUpper(methodName)]; ok && m == strings.ToUpper(methodName) { if len(c.methods) == 0 {
find = true
} else if m, ok := c.methods[strings.ToUpper(methodName)]; ok && m == strings.ToUpper(methodName) {
find = true find = true
} else if m, ok = c.methods["*"]; ok && m == methodName { } else if m, ok = c.methods["*"]; ok && m == methodName {
find = true find = true
@ -504,6 +506,9 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin
} }
} }
if l.regexps.MatchString(regurl) { if l.regexps.MatchString(regurl) {
if url == "/" {
return true, url + regurl + tourl(params)
} else {
return true, url + "/" + regurl + tourl(params) return true, url + "/" + regurl + tourl(params)
} }
} }
@ -511,6 +516,7 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin
} }
} }
} }
}
return false, "" return false, ""
} }

View File

@ -99,6 +99,7 @@ func TestUrlFor2(t *testing.T) {
handler := NewControllerRegister() handler := NewControllerRegister()
handler.Add("/v1/:v/cms_:id(.+)_:page(.+).html", &TestController{}, "*:List") handler.Add("/v1/:v/cms_:id(.+)_:page(.+).html", &TestController{}, "*:List")
handler.Add("/v1/:v(.+)_cms/ttt_:id(.+)_:page(.+).html", &TestController{}, "*:Param") handler.Add("/v1/:v(.+)_cms/ttt_:id(.+)_:page(.+).html", &TestController{}, "*:Param")
handler.Add("/:year:int/:month:int/:title/:entid", &TestController{})
if handler.UrlFor("TestController.List", ":v", "za", ":id", "12", ":page", "123") != if handler.UrlFor("TestController.List", ":v", "za", ":id", "12", ":page", "123") !=
"/v1/za/cms_12_123.html" { "/v1/za/cms_12_123.html" {
Info(handler.UrlFor("TestController.List")) Info(handler.UrlFor("TestController.List"))
@ -109,6 +110,12 @@ func TestUrlFor2(t *testing.T) {
Info(handler.UrlFor("TestController.Param")) Info(handler.UrlFor("TestController.Param"))
t.Errorf("TestController.List must equal to /v1/za_cms/ttt_12_123.html") t.Errorf("TestController.List must equal to /v1/za_cms/ttt_12_123.html")
} }
if handler.UrlFor("TestController.Get", ":year", "1111", ":month", "11",
":title", "aaaa", ":entid", "aaaa") !=
"/1111/11/aaaa/aaaa" {
Info(handler.UrlFor("TestController.Get"))
t.Errorf("TestController.Get must equal to /1111/11/aaaa/aaaa")
}
} }
func TestUserFunc(t *testing.T) { func TestUserFunc(t *testing.T) {

View File

@ -160,6 +160,13 @@ func (t *Tree) addseg(segments []string, route interface{}, wildcards []string,
} else { } else {
regexpStr = "/" + regexpStr regexpStr = "/" + regexpStr
} }
} else if reg != "" {
for _, w := range params {
if w == "." || w == ":" {
continue
}
regexpStr = "/([^/]+)" + regexpStr
}
} }
t.wildcard.addseg(segments[1:], route, append(wildcards, params...), reg+regexpStr) t.wildcard.addseg(segments[1:], route, append(wildcards, params...), reg+regexpStr)
} else { } else {

View File

@ -24,6 +24,7 @@ func init() {
routers = append(routers, testinfo{"/:name/*.*", "/nice/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}}) routers = append(routers, testinfo{"/:name/*.*", "/nice/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}})
routers = append(routers, testinfo{"/:name/test/*.*", "/nice/test/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}}) routers = append(routers, testinfo{"/:name/test/*.*", "/nice/test/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}})
routers = append(routers, testinfo{"/v1/shop/:id:int", "/v1/shop/123", map[string]string{":id": "123"}}) routers = append(routers, testinfo{"/v1/shop/:id:int", "/v1/shop/123", map[string]string{":id": "123"}})
routers = append(routers, testinfo{"/:year:int/:month:int/:id/:endid", "/1111/111/aaa/aaa", map[string]string{":year": "1111", ":month": "111", ":id": "aaa", ":endid": "aaa"}})
routers = append(routers, testinfo{"/v1/shop/:id/:name", "/v1/shop/123/nike", map[string]string{":id": "123", ":name": "nike"}}) routers = append(routers, testinfo{"/v1/shop/:id/:name", "/v1/shop/123/nike", map[string]string{":id": "123", ":name": "nike"}})
routers = append(routers, testinfo{"/v1/shop/:id/account", "/v1/shop/123/account", map[string]string{":id": "123"}}) routers = append(routers, testinfo{"/v1/shop/:id/account", "/v1/shop/123/account", map[string]string{":id": "123"}})
routers = append(routers, testinfo{"/v1/shop/:name:string", "/v1/shop/nike", map[string]string{":name": "nike"}}) routers = append(routers, testinfo{"/v1/shop/:name:string", "/v1/shop/nike", map[string]string{":name": "nike"}})

View File

@ -248,7 +248,7 @@ func NewWithFilter(urlPrefix string, store cache.Cache) *Captcha {
cpt := NewCaptcha(urlPrefix, store) cpt := NewCaptcha(urlPrefix, store)
// create filter for serve captcha image // create filter for serve captcha image
beego.AddFilter(cpt.URLPrefix+":", "BeforeRouter", cpt.Handler) beego.InsertFilter(cpt.URLPrefix+":", beego.BeforeRouter, cpt.Handler)
// add to template func map // add to template func map
beego.AddFuncMap("create_captcha", cpt.CreateCaptchaHtml) beego.AddFuncMap("create_captcha", cpt.CreateCaptchaHtml)