根椐常见的PHP web开发过程,以下是我所期望的使用框架进行开发的过程:
初始化项目目录结构和配置文件 (可使用脚本进行初始化)
编写入口文件
package main
import "./controllers"
func main() {
app := App.New("conf.ini") //引入配置文件
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
app.Dispatch() //需要传递一些参数
})
}
编写controller
package controllers
import "models"
type Control{
*App //匿名组合框架对象,以便直接使用框架的方法
}
//实现作用于所有controller的前置拦置,比如进行权限检查
func (this *Control) PreControl{
}
//具体的每一个controller方法(对应到Path)
func (this *Control) Action1(){
//调用Valid进行参数检查
//调用models进行业务逻辑处理
User:= models.NewUser()
//如果业务逻辑是对单表简单的增删改查,可不编写service层,直接用框架提供的dao方法
User:= this.NewMySQLDao("user")
//可根椐需要进行输出,比如输出格式化的json
this.RespJson(User.Lst())
//或使用模板输出
this.Render("a.tpl",User.Lst())
}
如果有需要,编写models,实现service层
package models
type M struct{ //组合框架提供的dao对象方法
*MySQLDao
*MemcacheDao
}
func NewUser() *M{
mysql:= NewMySQLDao()
return &M{mysql}
}
//实现service方法
func (this *M)GetCache(){
//从缓存读,如有,直接返回
//缓存没有,从数据库读,并放回缓存
}
如有需要,编写模板