Laravel 十分钟 Blog 教程
像 Django、Rails 都有个 《** 10分钟Blog教程》,还没看到 php 的这种让初学者顿时肾上腺素激增立马跟打了鸡血似的的教程。难道学 php 的童鞋尽皆高贵沉着冷静,不屑做这种图样图森的事儿?
罢了罢了,这些降低逼格的事儿就由我来做吧,所幸我只是个初学者,也不怕被人骂菜鸟。
0. 前言
设计方案
其实也没有什么设计,就是一个最最简单的博客,可以增删改查文章(Post)
为图方便,数据库就用 sqlite,反正 Laravel 更换数据库方便,如果不爽可以自行更改成其他数据库。
环境安装
1. 新建项目
先新建个 Blog 项目
laravel new blog
然后更新安装依赖包
cd blog
composer install
然后把数据库由默认的 mysql 更改为 sqlite
app/config/database.php:29
'default' => 'sqlite',
启动服务器吧 (有 php 内置服务器就是好)
php artisan serve
注意:有莫名其妙的 bug 时,不妨重启服务器试试。
2. 创建 posts 表
这个时候就开始体现 Laravel 的方便之处了:通过命令行生成 migration 文件,而且是使用 php 编写,一站式解决方案。
php artisan migrate:make create_posts_table
这时就会在 app/database/migrations/ 目录下生成类似文件:
20140807062522createpoststable.php
前面编号就是创建时的时间。
里面就两个函数,up 用于更改数据库,down 用来回滚数据库。
public function up()
{
Schema::create('posts', function($table)
{
$table->increments('id');
$table->string('title');
$table->text('content');
$table->timestamps();
});
}
public function down()
{
Schema::drop('posts');
}
写好了 migration 文件就能在数据库新建 posts 表了,也是使用命令行:
php artisan migrate
如果以后需要回滚的话,可以这样
php artisan migrate:rollback
3. Post Model
然后就能新建 Post 的 Model 了,因为 Laravel 使用了 Eloquent ORM,操作数据库方便极了,用的时间长了你都不会写 sql 了。
新建文件
app/models/Post.php
<?php
class Post extends Eloquent {}
这时就能新建点儿种子数据了
app/database/seeds.php:16
public function run()
{
Eloquent::unguard();
// $this->call('UserTableSeeder');
for($i=1; $i<=10; $i++)
{
$post = new Post;
$post->title = 'post '.$i;
$post->content = 'just for test ...';
$post->save();
}
}
}
现在就把运行 seeds.php,把写入到数据库吧
php artisan db:seed
4. Post Controller
依旧是方便的命令行
php artisan controller:make PostController
以上命令新建了文件:
app/controllers/PostController.php
其中定义了5个函数框架,遵循了 RESTful
函数 | 方法 | 功能 |
---|---|---|
index | GET | 列表页 |
create | GET | 新建页 |
store | POST | 新建Post |
show | GET | 显示单个Post |
edit | GET | 编辑Post |
update | PUT | 更新 |
destroy | DELETE | 删除 |
把 PostController 加入路由表
app/routes.php
Route::get('/', 'PostController@index');
Route::resource('posts', 'PostController');
下面咱就一个函数一个函数来写
5. index
在 PostController.php 的 index 函数中取出所有 posts
public function index()
{
$posts = Post::all();
return View::make('posts.index', array('posts' => $posts));
}
然后就是 views 了,新建 app/views/posts 目录,目录下新建 index.blade.php
@foreach($posts as $post)
<h3>{{ $post->title }}</h3>
@endforeach
现在再看看 http://localhost:8000/posts
6. create
也是与上面类似过程 PostController.php 的 create 函数
public function create()
{
$post = new Post;
return View::make('posts.create', array('post' => $post));
}
新建 app/views/posts/_post.blade.php,这是一个子模板,可以嵌套到其他模板中
{{ Form::label('title', 'Title') }}
{{ Form::text('title', $post->title) }}
<br>
{{ Form::label('content', 'Content') }}
{{ Form::textarea('content', $post->content) }}
<br>
{{ Form::submit('Submit') }}
新建 app/views/posts/create.blade.php
<h3>Create New Post</h3>
{{ Form::model($post, array('action' => 'PostController@store', 'method' => 'post')) }}
@include('posts._post', array('post' => $post))
{{ Form::close() }}
其中 @include 就是表示嵌套 _post.blade.php.
现在再看看 http://localhost:8000/posts/create
不过现在还不能提交增加 post,需要完成下面的 store 函数才行.
7. store
PostController.php 的 store 函数
public function store()
{
$post = new Post;
$post->title = Input::get('title');
$post->content = Input::get('content');
if($post->save())
return Redirect::action('PostController@index');
else
return View::make('posts.create', array('post' => $post));
}
现在http://localhost:8000/posts/create提交一个新的 post,看看是不是成功了 :)
8. show
PostController.php 的 show 函数
public function show($id)
{
$post = Post::find($id);
return View::make('posts.show', array('post' => $post));
}
新建 app/views/posts/show.blade.php
<h1>{{ $post->title }}</h1>
<p>{{ $post->content }}</p>
可以通过 http://localhost:8000/posts/1 查看第一个 post
9. edit
PostController.php 的 edit 函数
public function edit($id)
{
$post = Post::find($id);
return View::make('posts.edit', array('post' => $post));
}
新建 app/views/posts/edit.blade.php
<h3>Edit Post</h3>
{{ Form::model($post, array('action' => array('PostController@update', $post->id), 'method' => 'put')) }}
@include('posts._post', array('post' => $post))
{{ Form::close() }}
查看 http://localhost:8000/posts/1/edit
不过需要完成下面的 update 函数才能提交更新
10. update
PostController.php 的 update 函数
public function update($id)
{
$post = Post::find($id);
$post->title = Input::get('title');
$post->content = Input::get('content');
if($post->save())
return Redirect::action('PostController@show', $id);
else
return View::make('posts.edit', array('post' => $post));
}
现在就能更新文章了 http://localhost:8000/posts/1/edit
11. destroy
PostController.php 的 destroy 函数
public function destroy($id)
{
$post = Post::find($id);
$post->delete();
return Redirect::action('PostController@index');
}
12. 更新 index 模板
现在功能都有了,但是页面还都是分的散单独页面,现在就在index页面将各个页面连接起来。
app/views/posts/index.blade.php
<h3>{{ link_to_action('PostController@create', 'New Post') }}</h3>
<table>
@foreach($posts as $post)
<tr>
<td>{{ $post->title }}</td>
<td>{{ link_to_action('PostController@show', 'View', array('id' => $post->id)) }}</td>
<td>{{ link_to_action('PostController@edit', "Edit", array('id' => $post->id)) }}</td>
<td>
{{ Form::open(array('action' => array('PostController@destroy', $post->id), 'method' => 'delete', 'style' => 'display:inline-block;')) }}
{{ Form::submit('Delete', array('onclick' => "return confirm('Are you sure?');")) }}
{{ Form::close() }}
</td>
@endforeach
</table>
13. 结语
呵呵~ 够简单吧(似乎称为简陋更恰当), 本来还想连 comments 一起加上,但是写的已经太长,以后的重复部分太多,太啰嗦,思路是一致的,就到此为止吧。
可以在 github 上查看项目 github.com/adousj/laravel-blog。 master 分支上我已经添加了 comments 功能,本教程的代码在 stage/1-posts 分支。