<!-- app/views/form.blade.php --> {{ Form::open(array('url' => 'our/target/route')) }} {{ Form::close() }}
以上內容將編譯爲(自動加入 _token
):php
<form method="POST" action="http://demo.dev/our/target/route" accept-charset="UTF-8"> <input name="_token" type="hidden" value="83KCsmJF1Z2LMZfhb17ihvt9ks5NEcAwFoRFTq6u"> </form>
{{ Form::open(array( 'url' => 'our/target/route', 'method' => 'GET', 'accept-charset' => 'ISO-8859-1' )) }}
POST
GET
PUT
DELETE
默認爲 POST
。{{ Form::open(array( 'url' => 'our/target/route', 'method' => 'DELETE' )) }}
{{ Form::open(array( 'url' => 'our/target/route', 'files' => true )) }}
{{ Form::open(array( 'route' => 'my_route' )) }}
{{ Form::open(array( 'action' => 'MyController@myAction' )) }}
{{ Form::label('first_name', 'First Name', array('class' => 'f_class')) }}
以上內容將編譯爲:安全
<label for="first_name" class="f_class">First Name</label>
{{ Form::text('first_name', 'Taylor Otwell', array('id' => 'first_name')) }}
以上內容將編譯爲:app
<input name="first_name" type="text" value="Taylor Otwell" id="first_name">
若是有同名的 label
則 id
會自動補完,無需額外定義:post
{{ Form::label('first_name', 'First Name') }} {{ Form::text('first_name', 'Taylor Otwell') }}
{{ Form::label('description', 'Description') }} {{ Form::textarea(‘description', 'Best field ever!') }}
{{ Form::label('secret', 'Super Secret') }} {{ Form::password('secret') }}
{{ Form::label('pandas_are_cute', 'Are pandas cute?') }} {{ Form::checkbox('pandas_are_cute', '1', true) }}
{{ Form::open(array('url' => 'my/route')) }} {{ Form::label('panda_colour', 'Pandas are?') }} {{ Form::radio('panda_colour', 'red', true) }} Red {{ Form::radio('panda_colour', 'black') }} Black {{ Form::radio('panda_colour', 'white') }} White {{ Form::close() }}
{{ Form::label('panda_colour', 'Pandas are?') }} {{ Form::select('panda_colour', array( 'red' => 'Red', 'black' => 'Black', 'white' => 'White' ), 'red') }}
分組下拉列表:ui
{{ Form::label('bear', 'Bears are?') }} {{ Form::select('bear', array( 'Panda' => array( 'red' => 'Red', 'black' => 'Black', 'white' => 'White' ), 'Character' => array( 'pooh' => 'Pooh', 'baloo' => 'Baloo' ) ), 'black') }}
{{ Form::label('email', 'E-Mail Address') }} {{ Form::email('email', 'me@daylerees.com') }}
{{ Form::open(array( 'url' => 'my/route', 'files' => true )) }} {{ Form::label('avatar', 'Avatar') }} {{ Form::file('avatar') }} {{ Form::close() }}
{{ Form::hidden('panda', 'luishi') }}
{{ Form::submit('Save') }}
{{ Form::button('Smile') }}
{{ Form::image(asset('my/image.gif', 'submit')) }}
{{ Form::reset('Clear') }}
// app/macros.php Form::macro('fullName', function($name) { return '<p>Full name: <input type="text" name="'.$name.'"></p>'; });
調用方法:url
{{ Form::fullName('my_field') }}
請使用 csrf
前置過濾器:code
// app/routes.php Route::post('/handle-form', array('before' => 'csrf', function() { // Handle our posted form data. }));
注意: 若不使用系統提供的表單起始和結束標籤 Form::open()
Form::close()
請手動添加表單 _token
。orm
<form action="{{ url('handle-form') }}" method="POST"> {{ Form::token() }} </form>
還記得第8章中提到的「舊數據的獲取方法」嗎?
使用系統提供的表單的最大的一個好處,就是系統將自動處理這些舊數據來填充表單。
完美解決了「當用戶提交錯誤,重定向到以前頁面時舊數據丟失,表單空白」的問題。而不是單純的使用JS的「回退」。csrf