Go Revel - Routing(路由)
`Routing`路由控制著請(qǐng)求應(yīng)該由哪些控制器接受。
它在項(xiàng)目的`conf/routes`文件中定義。
格式為:
(METHOD) (URL Pattern) (Controller.Action)
(請(qǐng)求方法) (URL 匹配模式) (Controller.Action)
示例:
# conf/routes
# This file defines all application routes (Higher priority routes first)
GET /login App.Login # 一個(gè)簡(jiǎn)單的路徑
GET /hotels/ Hotels.Index # 匹配 /hotels 與 /hotels/ (尾部斜杠選填)
GET /hotels/:id Hotels.Show # 提取一個(gè)URI參數(shù)
WS /hotels/:id/feed Hotels.Feed # 匹配相應(yīng)的WebSockets協(xié)議
POST /hotels/:id/:action Hotels.:action # 自動(dòng)匹配相應(yīng)的action
GET /public/*filepath Static.Serve("public") # 將 /app/public 下的資源路徑映射為 /public/...
* /:controller/:action :controller.:action # 捕獲所有,自動(dòng)映射
##簡(jiǎn)單的URL路徑
GET /login App.Login
最簡(jiǎn)單的路由為精確匹配,它將`/login`轉(zhuǎn)到`App.Login`這個(gè)action
##尾部反斜杠
GET /hotels/ Hotels.Index
這里`/hotels/`與`/hotels`路徑都會(huì)被轉(zhuǎn)到`Hotels.Index`Action,對(duì)于尾部的反斜杠,不管有沒有都會(huì)同等對(duì)待。
##URL參數(shù)
GET /hotels/:id Hotels.Show
斜杠后`:`標(biāo)識(shí)的字段會(huì)被匹配并提取。這里`:id`也就是反斜杠之后的路徑將會(huì)被匹配提取并傳入至Action的方法。
例如`/hotels/123`中,123將會(huì)被提取為id。
被提取的參數(shù)會(huì)被保存至`Controller.Params`,如下方法可以在`Action`中獲取參數(shù):
func (c Hotels) Show(id int) revel.Result {
...
}
或
func (c Hotels) Show() revel.Result {
var id string = c.Params.Get("id")
...
}
或
func (c Hotels) Show() revel.Result {
var id int
c.Params.Bind(&id, "id")
...
}
##*號(hào)通配符
GET /public/*filepath Static.Serve("public")
*號(hào)必須位于匹配路徑的最末端,它將提取所有符合的字符串。例如,這里將`/public/`之后的所有字符串匹配提取。
##Websockets
WS /hotels/:id/feed Hotels.Feed
`WS`用來定義websockets協(xié)議下的路由。它指向的action必須具有如下簽名:
func (c Hotels) Feed(ws *websocket.Conn, id int) revel.Result {
...
}
即第一參數(shù)必須是`*websocket.Conn`類型。
##靜態(tài)資源
GET /public/*filepath Static.Serve("public")
GET /favicon.ico Static.Serve("public", "img/favicon.png")
路由中,通過`Static`模塊提供制定文件夾的靜態(tài)文件服務(wù)。它只會(huì)匹配一個(gè)`Static`控制器。它的Action接受兩個(gè)參數(shù):
**prefix (string)** 指向資源文件的絕對(duì)/相對(duì)路徑
**filepath (string)** 指定文件的相對(duì)路徑(相對(duì)于`prefix`)
## 固定參數(shù)
可以像靜態(tài)資源服務(wù)一樣將參數(shù)使用固定的名稱:
GET /products/:id ShowList("PRODUCT")
GET /menus/:id ShowList("MENU")
`Action`名稱后加上傳入變量的名稱,可以將匹配到的參數(shù)按順序轉(zhuǎn)為命名的參數(shù),方便在`Action`中處理。
## 自動(dòng)路由
POST /hotels/:id/:action Hotels.:action
* /:controller/:action :controller.:action
自動(dòng)匹配相應(yīng)的控制器與動(dòng)作,這里不區(qū)分大小寫。
上面示例中,第一條路由的調(diào)用規(guī)則如下:
/hotels/1/show => Hotels.Show
/hotels/2/details => Hotels.Details
第二條調(diào)用規(guī)則,自動(dòng)路由方式:
/app/login => App.Login
/users/list => Users.List
在大小寫不同時(shí),也會(huì)進(jìn)行相應(yīng)路由:
/APP/LOGIN => App.Login
/Users/List => Users.List
##反向路由
在使用revel構(gòu)建項(xiàng)目時(shí),它會(huì)根據(jù)routes文件生成`app/routes/routes.go`源碼。
可以如下格式來在代碼中只是用反向路由:
routes.Controller.Action(param1, param2)
下面實(shí)例中演示了如何調(diào)用反向路由
import (
"github.com/robfig/revel"
"project/app/routes"
)
type App struct { *revel.Controller }
// 顯示一個(gè)表單
func (c App) ViewForm(username string) revel.Result {
return c.Render(username)
}
// 處理一個(gè)提交的表單
func (c App) ProcessForm(username, input string) revel.Result {
...
if c.Validation.HasErrors() {
c.Validation.Keep()
c.Flash.Error("Form invalid. Try again.")
return c.Redirect(routes.App.ViewForm(username)) // <--- 反向路由
}
c.Flash.Success("Form processed!")
return c.Redirect(routes.App.ViewConfirmation(username, input)) // <--- 反向路由
}
posted on 2013-08-19 00:50 黑暗伯爵 閱讀(1840) 評(píng)論(0) 收藏 舉報(bào)
浙公網(wǎng)安備 33010602011771號(hào)