【表单】illuminate/html与laravelcollective/html【原创】

illuminate/html 与laravelcollective/html 库的作用是:快速通过普通的 form 标签创建表单

illuminate/html 使用步骤(5.0以上已弃用)

1. 引入依赖

1
composer require illuminate/html
或者是composer.json的require里面加上:
"illuminate/html": "版本号"

而版本号的话通过命令查询:
1
composer show -all illuminate/html

2. 修改app.php

修改 config/app.php 文件:
在 providers 字段中添加 HtmlServiceProvider ,把库注册到 Laravel 中:
// 添加到 'IlluminateViewViewServiceProvider', 后面
IlluminateHtmlHtmlServiceProvider::class,

通过 aliases 字段为刚才注册的 Provider 设置别名:
1
// 添加到 'View'      => 'IlluminateSupportFacadesView', 之后
2
'Form'      => IlluminateHtmlFormFacade::class,
3
'Html'      => IlluminateHtmlHtmlFacade::class,
设置别名后,在控制器中调用的时候就不需要输入 Facade 的完整名称,只需要使用别名即可。

3. 视图中引用

视图中通过以下的代码可以生成form标签:
{!! Form::open() !!}
{!! Form::close() !!}

比如:
1
@extends('main')
2
3
@section('content')
4
    <h1>Wirte a New Article</h1>
5
6
    <hr/>
7
8
    {!! Form::open() !!}
9
10
    {!! Form::close() !!}
11
@stop

4. 运行

运行之后页面没什么变化,但是源代码中会生成form标签:
<form method="POST" action="控制器的url" accept-charset="UTF-8"><input name="_token" type="hidden" value="随机字符串"></form>

模版中那一段代码会生成了一个 form 表单,mothod 默认为 POST,action 为当前RUL,并且生成了一个隐藏域 _token ,它可以让表单提交更加安全。

注意:

illuminate/html 在Laravel 5.0以上已经被弃用了,使用laravelcollective/html来替换。以后5.0以上的版本还是用illuminate/html的话会报错:



以下是laravelcollective/html的使用步骤:

laravelcollective/html使用步骤

1. 引入依赖

1
composer require laravelcollective/html
或者是composer.json的require里面加上:
"laravelcollective/html": "版本号"

而版本号的话通过命令查询:
1
composer show -all illuminate/html

2. 修改app.php

修改 config/app.php 文件:
在 providers 字段中添加 HtmlServiceProvider ,把库注册到 Laravel 中:
// 添加到 'IlluminateViewViewServiceProvider', 后面
CollectiveHtmlHtmlServiceProvider::class,

通过 aliases 字段为刚才注册的 Provider 设置别名:
1
// 添加到 'View'      => 'IlluminateSupportFacadesView', 之后
2
'Form'      => CollectiveHtmlFormFacade::class,
3
'Html'      => CollectiveHtmlHtmlFacade::class,
设置别名后,在控制器中调用的时候就不需要输入 Facade 的完整名称,只需要使用别名即可。

3. 视图中引用

视图中通过以下的代码可以生成form标签:
{!! Form::open() !!}
{!! Form::close() !!}

比如:
1
@extends('main')
2
3
@section('content')
4
    <h1>Wirte a New Article</h1>
5
6
    <hr/>
7
8
    {!! Form::open() !!}
9
10
    {!! Form::close() !!}
11
@stop

4. 运行

运行之后页面没什么变化,但是源代码会生成form标签:
<form method="POST" action="控制器的url" accept-charset="UTF-8"><input name="_token" type="hidden" value="随机字符串"></form>

模版中那一段代码会生成了一个 form 表单,mothod 默认为 POST,action 为当前RUL,并且生成了一个隐藏域 _token ,它可以让表单提交更加安全。




原文地址:https://www.cnblogs.com/linewman/p/9918113.html