首先让我们修改路由,能够增加一个文章的发布。
然后修改控制器
public function create() {
return view('articles.create');
}
我们返回一个视图,新建这个视图。我们当然可以直接使用HTML建立表单,但我们有功能更好的办法。我们使用一个开源库,Jeffrey Way 开发的illuminate\html。安装依赖库:
composer require illuminate/html
laravel的库需要注册到laravel中才能使用。在 config/app.php 中,我们可以看到 laravel 提供的 provider 字段,这里描述了laravel的库功能。在Laravel Framewirk Service Providers... 最后添加我们新增的 HtmlProvider
'Illuminate\Html\HtmlServiceProvider',
我们不希望使用 Illuminate\Html\FromFacade 这么长的名字来引入,我们需要简短的名字。在当前的 app.php 中找到 aliases 段,在最后添加别名。
'Form' => 'Illuminate\Html\FormFacade',
'Html' => 'Illuminate\Html\HtmlFacade',
OK,现在我们来创建视图,views/articles/create.blade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@
extends
(
'layout'
)
@section(
'content'
)
<h1>Write a New Article</h1>
<hr/>
{{--使用我们添加的 illuminate\html 开源库--}}
{!! Form::open() !!}
{!! Form::close() !!}
@stop
|
访问 /articles/create 看到了错误,Why? 让我们测试一下,到底是哪里出了问题。在控制器中做出下面的修改:
1
2
3
4
5
6
7
|
public
function
show(
$id
) {
dd(
'show'
);
$article
= Article::findOrFail(
$id
);
return
view(
'articles.show'
, compact(
'article'
));
}
|
没错,你没看错,就是在 show 方法中添加 dd() 方法,这个方法简单的输出一个信息然后死掉。我们再来访问 /articles/create ,你看到了什么,你看到输出了 show 。
为什么我们访问 create 结果路由给了我们 show ? 我们来查看一下路由,到底发生了什么。
Route::get('articles', 'ArticlesController@index');
Route::get('articles/{id}', 'ArticlesController@show');
Route::get('articles/create', 'ArticlesController@create');
上面是我们的路由,注意到 articles/{id} 意味着这是一个通配符,所有在 articles/ 后面的东西都会匹配,你知道了么?我们的 /articles/create 也被他匹配了。OMG!
解决方案就是调整顺序:
Route::get('articles', 'ArticlesController@index');
Route::get('articles/create', 'ArticlesController@create');
Route::get('articles/{id}', 'ArticlesController@show');
也就是从特殊到普通,以后的路由设置中要时刻注意这个问题。现在我们在访问 articles/create 一切OK了。
在浏览器中查看一下源代码,你会发现不仅生成了 method 和 action 同时生成了一个隐藏的 _token 字段作为服务器对窗体的验证,避免黑客的伪造攻击。
让我们修改我们的视图,添加字段:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
@
extends
(
'layout'
)
@section(
'content'
)
<h1>Write a New Article</h1>
<hr/>
{{--使用我们添加的 illuminate\html 开源库--}}
{!! Form::open() !!}
<div
class
=
"form-group"
>
{!! Form::label(
'title'
,
'Title:'
) !!}
{!! Form::text(
'title'
, null, [
'class'
=>
'form-control'
]) !!}
</div>
<div
class
=
"form-group"
>
{!! Form::label(
'body'
,
'Body:'
) !!}
{!! Form::textarea(
'body'
, null, [
'class'
=>
'form-control'
]) !!}
</div>
<div
class
=
"form-group"
>
{!! Form::submit(
'Add Article'
, [
'class'
=>
'btn btn-primary form-control'
]) !!}
</div>
{!! Form::close() !!}
@stop
|
当表单提交的时候,实际上是使用 post 方法提交到 articles/create 上的,但根据RESTful的习惯,我们希望能够 post 到 /articles 上,我们来修改视图的表单方法,设定提交的路径。
{!! Form::open(['url' => 'articles']) !!}
然后我们在路由中处理表单提交事件。
我们来处理控制器
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//注意:将下面的 use 语句删除,我们使用 facade 接口中的 Request
//use App\Http\Requests\Request;
//引入下面的命名空间中的 Request
use
Illuminate\Support\Facades\Request;
public
function
store() {
//使用 Illuminate\Html\Request 来返回全部的表单输入字段
$input
= Request::all();
//我们直接返回$input,来看一下
return
$input
;
}
|
request获取数据的方式------> $input
= Request::all();
我们可以直接看到输入表单的json结果。如果只需要 title 字段的值,则可以使用 Request::get('titel') 。
如何添加到数据库中呢?借助模型,我们可以直接采用下面的方法,
1
|
Article::create(
$input
);
|
就这么简单,就是这么任性
如果没有忘记 Mass Assignment,在我们的模型中我们定义了 $fillable 数组,来定义那些字段可以直接在 create 的时候直接填充。
修改控制器,添加到模型中,并存储到数据库。
1
2
3
4
5
6
|
public
function
store() {
$input
= Request::all();
Article::create(
$input
);
return
redirect(
'articles'
);
}
|
添加一条记录试试,非常棒。但别忘了。我们还有一个字段叫做 published_at ,让我们来处理它。
1
2
3
4
5
6
7
8
|
public
function
store() {
$input
= Request::all();
$input
[
'published_at'
] = Carbon::now();
Article::create(
$input
);
return
redirect(
'articles'
);
}
|
添加新纪录在测试一下。
还有一个问题,新添加的应该显示在最前面,我们来修改以下控制器。
1
2
3
4
5
6
7
8
9
|
public
function
index() {
//倒序获取文章
//可以这样
//$articles = Article::orderBy('published_at', 'desc')->get();
//简单方式,当然还有 oldest()
$articles
= Article::latest(
'published_at'
)->get();
return
view(
'articles.index'
, compact(
'articles'
));
}
|