The main function of router is to connect request URL and handler. Beego wrapped `Controller`, so it connects request URL and `ControllerInterface`. The `ControllerInterface` has following methods:
type ControllerInterface interface {
Init(ct *Context, cn string)
...
...
@@ -122,58 +121,65 @@ In development mode, you have following effects:
`beego.Controller` implemented all of them, so you just use this struct as anonymous field in your controller struct. Of course you have to overload corresponding methods for more specific usages.
用户可以通过如下的方式进行路由设置:
Users can use following ways to register route rules:
After you setting static directory, when users visit `/images/login/login.png`,Beego accesses `images/login/login.png` in related to your application directory. One more example, if users visit `/static/img/logo.png`, Beego accesses file `public/img/logo.png`.
## 过滤和中间件
beego支持自定义过滤中间件,例如安全验证,强制跳转等
##Filter and middleware
Beego supports customized filter and middleware, such as security verification, force redirect, etc.
如下例子所示,验证用户名是否是admin,应用于全部的请求:
Here is an example of verify user name of all requests, check if it's admin.
var FilterUser = func(w http.ResponseWriter, r *http.Request) {
if r.URL.User == nil || r.URL.User.Username() != "admin" {
This method executes when client sends request as GET method, 403 as default status code. Users overload this method for customized handle process of GET method.
This method executes when client sends request as POST method, 403 as default status code. Users overload this method for customized handle process of POST method.
This method executes when client sends request as DELETE method, 403 as default status code. Users overload this method for customized handle process of DELETE method.
This method executes when client sends request as PUT method, 403 as default status code. Users overload this method for customized handle process of PUT method.
This method executes when client sends request as HEAD method, 403 as default status code. Users overload this method for customized handle process of HEAD method.
This method executes when client sends request as PATCH method, 403 as default status code. Users overload this method for customized handle process of PATCH method.
This method executes when client sends request as OPTIONS method, 403 as default status code. Users overload this method for customized handle process of OPTIONS method.
This method executes after corresponding method finished, empty as default. User overload this method for more usages like close database, clean data, etc.
- Render() error
这个函数主要用来实现渲染模板,如果beego.AutoRender为true的情况下才会执行。
This method is for rendering template, it executes automatically when you set beego.AutoRender to true.
所以通过子struct的方法重写,用户就可以实现自己的逻辑,接下来我们看一个实际的例子:
Overload all methods for all customized logic processes, let's see an example: