Add 14.2.md syntax highlighting
This commit is contained in:
18
zh/14.2.md
18
zh/14.2.md
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
## session集成
|
## session集成
|
||||||
beego中主要有以下的全局变量来控制session处理:
|
beego中主要有以下的全局变量来控制session处理:
|
||||||
|
```Go
|
||||||
|
|
||||||
//related to session
|
//related to session
|
||||||
SessionOn bool // 是否开启session模块,默认不开启
|
SessionOn bool // 是否开启session模块,默认不开启
|
||||||
@@ -11,8 +12,9 @@ beego中主要有以下的全局变量来控制session处理:
|
|||||||
SessionGCMaxLifetime int64 // cookies有效期
|
SessionGCMaxLifetime int64 // cookies有效期
|
||||||
|
|
||||||
GlobalSessions *session.Manager //全局session控制器
|
GlobalSessions *session.Manager //全局session控制器
|
||||||
|
```
|
||||||
当然上面这些变量需要初始化值,也可以按照下面的代码来配合配置文件以设置这些值:
|
当然上面这些变量需要初始化值,也可以按照下面的代码来配合配置文件以设置这些值:
|
||||||
|
```Go
|
||||||
|
|
||||||
if ar, err := AppConfig.Bool("sessionon"); err != nil {
|
if ar, err := AppConfig.Bool("sessionon"); err != nil {
|
||||||
SessionOn = false
|
SessionOn = false
|
||||||
@@ -35,32 +37,36 @@ beego中主要有以下的全局变量来控制session处理:
|
|||||||
} else {
|
} else {
|
||||||
SessionGCMaxLifetime = 3600
|
SessionGCMaxLifetime = 3600
|
||||||
}
|
}
|
||||||
|
```
|
||||||
在beego.Run函数中增加如下代码:
|
在beego.Run函数中增加如下代码:
|
||||||
|
```Go
|
||||||
|
|
||||||
if SessionOn {
|
if SessionOn {
|
||||||
GlobalSessions, _ = session.NewManager(SessionProvider, SessionName, SessionGCMaxLifetime)
|
GlobalSessions, _ = session.NewManager(SessionProvider, SessionName, SessionGCMaxLifetime)
|
||||||
go GlobalSessions.GC()
|
go GlobalSessions.GC()
|
||||||
}
|
}
|
||||||
|
```
|
||||||
这样只要SessionOn设置为true,那么就会默认开启session功能,独立开一个goroutine来处理session。
|
这样只要SessionOn设置为true,那么就会默认开启session功能,独立开一个goroutine来处理session。
|
||||||
|
|
||||||
为了方便我们在自定义Controller中快速使用session,作者在`beego.Controller`中提供了如下方法:
|
为了方便我们在自定义Controller中快速使用session,作者在`beego.Controller`中提供了如下方法:
|
||||||
|
```Go
|
||||||
|
|
||||||
func (c *Controller) StartSession() (sess session.Session) {
|
func (c *Controller) StartSession() (sess session.Session) {
|
||||||
sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)
|
sess = GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
```
|
||||||
## session使用
|
## session使用
|
||||||
通过上面的代码我们可以看到,beego框架简单地继承了session功能,那么在项目中如何使用呢?
|
通过上面的代码我们可以看到,beego框架简单地继承了session功能,那么在项目中如何使用呢?
|
||||||
|
|
||||||
首先我们需要在应用的main入口处开启session:
|
首先我们需要在应用的main入口处开启session:
|
||||||
|
```Go
|
||||||
|
|
||||||
beego.SessionOn = true
|
beego.SessionOn = true
|
||||||
|
```
|
||||||
|
|
||||||
然后我们就可以在控制器的相应方法中如下所示的使用session了:
|
然后我们就可以在控制器的相应方法中如下所示的使用session了:
|
||||||
|
```Go
|
||||||
|
|
||||||
func (this *MainController) Get() {
|
func (this *MainController) Get() {
|
||||||
var intcount int
|
var intcount int
|
||||||
@@ -78,7 +84,7 @@ beego中主要有以下的全局变量来控制session处理:
|
|||||||
this.Data["Count"] = intcount
|
this.Data["Count"] = intcount
|
||||||
this.TplNames = "index.tpl"
|
this.TplNames = "index.tpl"
|
||||||
}
|
}
|
||||||
|
```
|
||||||
上面的代码展示了如何在控制逻辑中使用session,主要分两个步骤:
|
上面的代码展示了如何在控制逻辑中使用session,主要分两个步骤:
|
||||||
|
|
||||||
1. 获取session对象
|
1. 获取session对象
|
||||||
|
|||||||
Reference in New Issue
Block a user