Format and remove 13.5.md spaces
This commit is contained in:
332
zh/13.5.md
332
zh/13.5.md
@@ -26,173 +26,173 @@
|
|||||||
博客主要的路由规则如下所示:
|
博客主要的路由规则如下所示:
|
||||||
```Go
|
```Go
|
||||||
|
|
||||||
//显示博客首页
|
//显示博客首页
|
||||||
beego.Router("/", &controllers.IndexController{})
|
beego.Router("/", &controllers.IndexController{})
|
||||||
//查看博客详细信息
|
//查看博客详细信息
|
||||||
beego.Router("/view/:id([0-9]+)", &controllers.ViewController{})
|
beego.Router("/view/:id([0-9]+)", &controllers.ViewController{})
|
||||||
//新建博客博文
|
//新建博客博文
|
||||||
beego.Router("/new", &controllers.NewController{})
|
beego.Router("/new", &controllers.NewController{})
|
||||||
//删除博文
|
//删除博文
|
||||||
beego.Router("/delete/:id([0-9]+)", &controllers.DeleteController{})
|
beego.Router("/delete/:id([0-9]+)", &controllers.DeleteController{})
|
||||||
//编辑博文
|
//编辑博文
|
||||||
beego.Router("/edit/:id([0-9]+)", &controllers.EditController{})
|
beego.Router("/edit/:id([0-9]+)", &controllers.EditController{})
|
||||||
|
|
||||||
```
|
```
|
||||||
## 数据库结构
|
## 数据库结构
|
||||||
数据库设计最简单的博客信息
|
数据库设计最简单的博客信息
|
||||||
```sql
|
```sql
|
||||||
|
|
||||||
CREATE TABLE entries (
|
CREATE TABLE entries (
|
||||||
id INT AUTO_INCREMENT,
|
id INT AUTO_INCREMENT,
|
||||||
title TEXT,
|
title TEXT,
|
||||||
content TEXT,
|
content TEXT,
|
||||||
created DATETIME,
|
created DATETIME,
|
||||||
primary key (id)
|
primary key (id)
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
## 控制器
|
## 控制器
|
||||||
IndexController:
|
IndexController:
|
||||||
|
|
||||||
```Go
|
```Go
|
||||||
|
|
||||||
type IndexController struct {
|
type IndexController struct {
|
||||||
beego.Controller
|
beego.Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IndexController) Get() {
|
func (this *IndexController) Get() {
|
||||||
this.Data["blogs"] = models.GetAll()
|
this.Data["blogs"] = models.GetAll()
|
||||||
this.Layout = "layout.tpl"
|
this.Layout = "layout.tpl"
|
||||||
this.TplNames = "index.tpl"
|
this.TplNames = "index.tpl"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
ViewController:
|
ViewController:
|
||||||
|
|
||||||
```Go
|
```Go
|
||||||
|
|
||||||
type ViewController struct {
|
type ViewController struct {
|
||||||
beego.Controller
|
beego.Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ViewController) Get() {
|
func (this *ViewController) Get() {
|
||||||
id, _ := strconv.Atoi(this.Ctx.Input.Params[":id"])
|
id, _ := strconv.Atoi(this.Ctx.Input.Params[":id"])
|
||||||
this.Data["Post"] = models.GetBlog(id)
|
this.Data["Post"] = models.GetBlog(id)
|
||||||
this.Layout = "layout.tpl"
|
this.Layout = "layout.tpl"
|
||||||
this.TplNames = "view.tpl"
|
this.TplNames = "view.tpl"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
NewController
|
NewController
|
||||||
```Go
|
```Go
|
||||||
|
|
||||||
type NewController struct {
|
type NewController struct {
|
||||||
beego.Controller
|
beego.Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *NewController) Get() {
|
func (this *NewController) Get() {
|
||||||
this.Layout = "layout.tpl"
|
this.Layout = "layout.tpl"
|
||||||
this.TplNames = "new.tpl"
|
this.TplNames = "new.tpl"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *NewController) Post() {
|
func (this *NewController) Post() {
|
||||||
inputs := this.Input()
|
inputs := this.Input()
|
||||||
var blog models.Blog
|
var blog models.Blog
|
||||||
blog.Title = inputs.Get("title")
|
blog.Title = inputs.Get("title")
|
||||||
blog.Content = inputs.Get("content")
|
blog.Content = inputs.Get("content")
|
||||||
blog.Created = time.Now()
|
blog.Created = time.Now()
|
||||||
models.SaveBlog(blog)
|
models.SaveBlog(blog)
|
||||||
this.Ctx.Redirect(302, "/")
|
this.Ctx.Redirect(302, "/")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
EditController
|
EditController
|
||||||
```Go
|
```Go
|
||||||
|
|
||||||
type EditController struct {
|
type EditController struct {
|
||||||
beego.Controller
|
beego.Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *EditController) Get() {
|
func (this *EditController) Get() {
|
||||||
id, _ := strconv.Atoi(this.Ctx.Input.Params[":id"])
|
id, _ := strconv.Atoi(this.Ctx.Input.Params[":id"])
|
||||||
this.Data["Post"] = models.GetBlog(id)
|
this.Data["Post"] = models.GetBlog(id)
|
||||||
this.Layout = "layout.tpl"
|
this.Layout = "layout.tpl"
|
||||||
this.TplNames = "edit.tpl"
|
this.TplNames = "edit.tpl"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *EditController) Post() {
|
func (this *EditController) Post() {
|
||||||
inputs := this.Input()
|
inputs := this.Input()
|
||||||
var blog models.Blog
|
var blog models.Blog
|
||||||
blog.Id, _ = strconv.Atoi(inputs.Get("id"))
|
blog.Id, _ = strconv.Atoi(inputs.Get("id"))
|
||||||
blog.Title = inputs.Get("title")
|
blog.Title = inputs.Get("title")
|
||||||
blog.Content = inputs.Get("content")
|
blog.Content = inputs.Get("content")
|
||||||
blog.Created = time.Now()
|
blog.Created = time.Now()
|
||||||
models.SaveBlog(blog)
|
models.SaveBlog(blog)
|
||||||
this.Ctx.Redirect(302, "/")
|
this.Ctx.Redirect(302, "/")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
DeleteController
|
DeleteController
|
||||||
```Go
|
```Go
|
||||||
|
|
||||||
type DeleteController struct {
|
type DeleteController struct {
|
||||||
beego.Controller
|
beego.Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *DeleteController) Get() {
|
func (this *DeleteController) Get() {
|
||||||
id, _ := strconv.Atoi(this.Ctx.Input.Params[":id"])
|
id, _ := strconv.Atoi(this.Ctx.Input.Params[":id"])
|
||||||
blog := models.GetBlog(id)
|
blog := models.GetBlog(id)
|
||||||
this.Data["Post"] = blog
|
this.Data["Post"] = blog
|
||||||
models.DelBlog(blog)
|
models.DelBlog(blog)
|
||||||
this.Ctx.Redirect(302, "/")
|
this.Ctx.Redirect(302, "/")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
## model层
|
## model层
|
||||||
```Go
|
```Go
|
||||||
|
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"github.com/astaxie/beedb"
|
"github.com/astaxie/beedb"
|
||||||
_ "github.com/ziutek/mymysql/godrv"
|
_ "github.com/ziutek/mymysql/godrv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Blog struct {
|
type Blog struct {
|
||||||
Id int `PK`
|
Id int `PK`
|
||||||
Title string
|
Title string
|
||||||
Content string
|
Content string
|
||||||
Created time.Time
|
Created time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetLink() beedb.Model {
|
||||||
|
db, err := sql.Open("mymysql", "blog/astaxie/123456")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
|
orm := beedb.New(db)
|
||||||
|
return orm
|
||||||
|
}
|
||||||
|
|
||||||
func GetLink() beedb.Model {
|
func GetAll() (blogs []Blog) {
|
||||||
db, err := sql.Open("mymysql", "blog/astaxie/123456")
|
db := GetLink()
|
||||||
if err != nil {
|
db.FindAll(&blogs)
|
||||||
panic(err)
|
return
|
||||||
}
|
}
|
||||||
orm := beedb.New(db)
|
|
||||||
return orm
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAll() (blogs []Blog) {
|
func GetBlog(id int) (blog Blog) {
|
||||||
db := GetLink()
|
db := GetLink()
|
||||||
db.FindAll(&blogs)
|
db.Where("id=?", id).Find(&blog)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetBlog(id int) (blog Blog) {
|
func SaveBlog(blog Blog) (bg Blog) {
|
||||||
db := GetLink()
|
db := GetLink()
|
||||||
db.Where("id=?", id).Find(&blog)
|
db.Save(&blog)
|
||||||
return
|
return bg
|
||||||
}
|
}
|
||||||
|
|
||||||
func SaveBlog(blog Blog) (bg Blog) {
|
func DelBlog(blog Blog) {
|
||||||
db := GetLink()
|
db := GetLink()
|
||||||
db.Save(&blog)
|
db.Delete(&blog)
|
||||||
return bg
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func DelBlog(blog Blog) {
|
|
||||||
db := GetLink()
|
|
||||||
db.Delete(&blog)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## view层
|
## view层
|
||||||
@@ -200,75 +200,75 @@ DeleteController
|
|||||||
layout.tpl
|
layout.tpl
|
||||||
```html
|
```html
|
||||||
|
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>My Blog</title>
|
<title>My Blog</title>
|
||||||
<style>
|
<style>
|
||||||
#menu {
|
#menu {
|
||||||
width: 200px;
|
width: 200px;
|
||||||
float: right;
|
float: right;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<ul id="menu">
|
<ul id="menu">
|
||||||
<li><a href="/">Home</a></li>
|
<li><a href="/">Home</a></li>
|
||||||
<li><a href="/new">New Post</a></li>
|
<li><a href="/new">New Post</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
{{.LayoutContent}}
|
{{.LayoutContent}}
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
```
|
```
|
||||||
|
|
||||||
index.tpl
|
index.tpl
|
||||||
```html
|
```html
|
||||||
|
|
||||||
<h1>Blog posts</h1>
|
<h1>Blog posts</h1>
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
{{range .blogs}}
|
{{range .blogs}}
|
||||||
<li>
|
<li>
|
||||||
<a href="/view/{{.Id}}">{{.Title}}</a>
|
<a href="/view/{{.Id}}">{{.Title}}</a>
|
||||||
from {{.Created}}
|
from {{.Created}}
|
||||||
<a href="/edit/{{.Id}}">Edit</a>
|
<a href="/edit/{{.Id}}">Edit</a>
|
||||||
<a href="/delete/{{.Id}}">Delete</a>
|
<a href="/delete/{{.Id}}">Delete</a>
|
||||||
</li>
|
</li>
|
||||||
{{end}}
|
{{end}}
|
||||||
</ul>
|
</ul>
|
||||||
```
|
```
|
||||||
view.tpl
|
view.tpl
|
||||||
```html
|
```html
|
||||||
|
|
||||||
<h1>{{.Post.Title}}</h1>
|
<h1>{{.Post.Title}}</h1>
|
||||||
{{.Post.Created}}<br/>
|
{{.Post.Created}}<br/>
|
||||||
|
|
||||||
{{.Post.Content}}
|
{{.Post.Content}}
|
||||||
```
|
```
|
||||||
new.tpl
|
new.tpl
|
||||||
```html
|
```html
|
||||||
|
|
||||||
<h1>New Blog Post</h1>
|
<h1>New Blog Post</h1>
|
||||||
<form action="" method="post">
|
<form action="" method="post">
|
||||||
标题:<input type="text" name="title"><br>
|
标题:<input type="text" name="title"><br>
|
||||||
内容:<textarea name="content" colspan="3" rowspan="10"></textarea>
|
内容:<textarea name="content" colspan="3" rowspan="10"></textarea>
|
||||||
<input type="submit">
|
<input type="submit">
|
||||||
</form>
|
</form>
|
||||||
```
|
```
|
||||||
edit.tpl
|
edit.tpl
|
||||||
```html
|
```html
|
||||||
|
|
||||||
<h1>Edit {{.Post.Title}}</h1>
|
<h1>Edit {{.Post.Title}}</h1>
|
||||||
|
|
||||||
<h1>New Blog Post</h1>
|
<h1>New Blog Post</h1>
|
||||||
<form action="" method="post">
|
<form action="" method="post">
|
||||||
标题:<input type="text" name="title" value="{{.Post.Title}}"><br>
|
标题:<input type="text" name="title" value="{{.Post.Title}}"><br>
|
||||||
内容:<textarea name="content" colspan="3" rowspan="10">{{.Post.Content}}</textarea>
|
内容:<textarea name="content" colspan="3" rowspan="10">{{.Post.Content}}</textarea>
|
||||||
<input type="hidden" name="id" value="{{.Post.Id}}">
|
<input type="hidden" name="id" value="{{.Post.Id}}">
|
||||||
<input type="submit">
|
<input type="submit">
|
||||||
</form>
|
</form>
|
||||||
```
|
```
|
||||||
## links
|
## links
|
||||||
* [目录](<preface.md>)
|
* [目录](<preface.md>)
|
||||||
|
|||||||
Reference in New Issue
Block a user