2017年9月29日 星期五

Laravel,package,collective,取代input標籤的寫法


安裝package



https://laravelcollective.com/docs/5.2/html



//檔案:composer.json
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.4.*",
"laravel/tinker": "~1.0",
"laravelcollective/html":"^5.2.0"
}


//下指令
composer update


//檔案:config/app.php
'providers' => [
Collective\Html\HtmlServiceProvider::class,


//檔案:config/app.php
'aliases' => [
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,


<!-- 檔案:resources/views/posts/create.blade.php -->
@extends('layouts.app')
@section('content')

<h1>發表文章</h1>
{!! Form::open() !!}
<input type="text" name="title" placeholder="寫下標題">
{{csrf_field()}}
<!-- name要跟表格欄位名稱一樣 -->
<input type="submit" name="submit">
</form>

@endsection


瀏覽器輸入:posts/create



呈現



<form method="POST" action="http://你設定的host/posts/create" accept-charset="UTF-8">
<input name="_token" type="hidden" value="一串亂數">
<input type="text" name="title" placeholder="寫下標題">
<input type="hidden" name="_token" value="cnVmIQ0o6a5aB1SB3ZEFWq8QByZ93Kd06VlujPk5">
<input type="submit" name="submit">
</form>


<!-- 檔案:resources/views/posts/edit.blade.php -->
@extends('layouts.app')
@section('content')

<h1>編輯文章</h1>
{!! Form::model($post,
['method'=>'PATCH','action'=>['PostsController@update',$post->id]]) !!}
{{csrf_field()}}
<div class="form-group">
{!! Form::label('title','標題:')!!}
{!! Form::text('title',null,['class'=>'form-control'])!!}
</div>
{!! Form::submit('編輯',['class'=>'btn btn-info'])!!}
{!! Form::close()!!}

{!! Form::open([
'method'=>'DELETE',
'action'=>['PostsController@destroy',$post->id]]) !!}
{{csrf_field()}}
{!! Form::submit('刪文',['class'=>'btn btn-danger'])!!}
{!! Form::close()!!}

@endsection


沒有留言:

張貼留言