Laravelでお問い合わせフォームを作成する
『Laravel』を使ってカンタンなお問い合わせフォームを作ってみた
https://liginc.co.jp/366540
参考ブログではLaradockを使っていますが、このブログではLaravelのみで行います。
Laravelをインストールする
モデルクラスの作成
お問い合わせ用のモデルクラスを作成します
1 |
php artisan make:model Models/Contact |
app/Models/Contact.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Contact extends Model { protected $fillable = [//モデルが受け取るデータのフィルター 'name', 'email', 'message', ]; } |
コントローラークラスの作成
お問い合わせ用のコントローラクラスを作成します
1 |
php artisan make:controller ContactController |
app/Http/Controllers/ContactController.php
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 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Contact; class ContactController extends Controller { public function form() { return view('form'); } public function confirm(Request $request) { $contact = new Contact($request->all()); return view('confirm', compact('contact')); } public function process(Request $request) { return view('complete'); } } |
ルーティングの設定
routes/web.php
1 2 3 |
Route::get('/contact', 'ContactController@form'); Route::post('/contact/confirm', 'ContactController@confirm'); Route::post('/contact/process', 'ContactController@process'); |
ビューの作成
お問い合わせ用のビューを作成します。
resources/viewsにhoge.blade.phpを作成していく
resources/views/form.blade.php(入力画面)
1 2 3 4 5 6 7 8 |
<input type="text" name="name"> <input type="text" name="email"> <textarea name="message"></textarea> <input type="submit" value="Confirm"> </form> |
resources/views/confirm.blade.php(確認画面)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<form action="{{action('ContactController@process')}}" method="post"> {{$contact->name}} {{$contact->email}} {{$contact->message}} <input type="submit" value="Submit"> @foreach($contact->getAttributes() as $key => $value) <input type="hidden" name="{{$key}}" value="{{$value}}"> @endforeach </form> |
resources/views/complete.blade.php(完了画面)
1 |
Complete |
サーバーを起動してみる
1 |
php artisan serve |
入力画面を表示する
http://127.0.0.1:8000/contact
名前入力、メール入力、テキスト入力、ボタンが表示される
バリデーションの実装
Laravelではバリデーション機能が簡単に実装できます。
app/Http/Controllers/ContactController.php
1 2 3 4 5 6 7 8 9 10 11 12 |
public function confirm(Request $request) { $this->validate($request, [ 'name' => 'required', 'email' => 'required|email', 'message' => 'present', ]); $contact = new Contact($request->all()); return view('confirm', compact('contact')); } |
エラーメッセージは $errorsを使用
resources/views/form.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
@if ($errors->any()) <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> @endif <form action="{{action('ContactController@confirm')}}" method="post"> {{csrf_field()}} <input type="text" name="name" value="{{old('name')}}"> <input type="text" name="email" value="{{old('email')}}"> <textarea name="message">{{old('message')}}</textarea> <input type="submit" value="Confirm"> </form> |
「入力画面」にもどるボタンを作成する
1 2 3 |
<input type="submit" name="action" value="back"> <input type="submit" name="action" value="submit"> |
app/Http/Controllers/ContactController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public function process(Request $request) { // ※要バリデーション $action = $request->get('action', 'back'); // 二つ目は初期値です。 $input = $request->except('action'); // そして、入力内容からは取り除いておきます。 if($action === 'submit') { // メール送信処理などを実装 return view('complete'); } else { // 戻る return redirect()->action('ContactController@form') ->withInput($input); } } |
バリデーションの種類を確認する
resources/lang/en/validation.php
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
'accepted' => 'The :attribute must be accepted.', 'active_url' => 'The :attribute is not a valid URL.', 'after' => 'The :attribute must be a date after :date.', 'after_or_equal' => 'The :attribute must be a date after or equal to :date.', 'alpha' => 'The :attribute may only contain letters.', 'alpha_dash' => 'The :attribute may only contain letters, numbers, and dashes.', 'alpha_num' => 'The :attribute may only contain letters and numbers.', 'array' => 'The :attribute must be an array.', 'before' => 'The :attribute must be a date before :date.', 'before_or_equal' => 'The :attribute must be a date before or equal to :date.', 'between' => [ 'numeric' => 'The :attribute must be between :min and :max.', 'file' => 'The :attribute must be between :min and :max kilobytes.', 'string' => 'The :attribute must be between :min and :max characters.', 'array' => 'The :attribute must have between :min and :max items.', ], 'boolean' => 'The :attribute field must be true or false.', 'confirmed' => 'The :attribute confirmation does not match.', 'date' => 'The :attribute is not a valid date.', 'date_format' => 'The :attribute does not match the format :format.', 'different' => 'The :attribute and :other must be different.', 'digits' => 'The :attribute must be :digits digits.', 'digits_between' => 'The :attribute must be between :min and :max digits.', 'dimensions' => 'The :attribute has invalid image dimensions.', 'distinct' => 'The :attribute field has a duplicate value.', 'email' => 'The :attribute must be a valid email address.', 'exists' => 'The selected :attribute is invalid.', 'file' => 'The :attribute must be a file.', 'filled' => 'The :attribute field must have a value.', 'image' => 'The :attribute must be an image.', 'in' => 'The selected :attribute is invalid.', 'in_array' => 'The :attribute field does not exist in :other.', 'integer' => 'The :attribute must be an integer.', 'ip' => 'The :attribute must be a valid IP address.', 'ipv4' => 'The :attribute must be a valid IPv4 address.', 'ipv6' => 'The :attribute must be a valid IPv6 address.', 'json' => 'The :attribute must be a valid JSON string.', 'max' => [ 'numeric' => 'The :attribute may not be greater than :max.', 'file' => 'The :attribute may not be greater than :max kilobytes.', 'string' => 'The :attribute may not be greater than :max characters.', 'array' => 'The :attribute may not have more than :max items.', ], 'mimes' => 'The :attribute must be a file of type: :values.', 'mimetypes' => 'The :attribute must be a file of type: :values.', 'min' => [ 'numeric' => 'The :attribute must be at least :min.', 'file' => 'The :attribute must be at least :min kilobytes.', 'string' => 'The :attribute must be at least :min characters.', 'array' => 'The :attribute must have at least :min items.', ], 'not_in' => 'The selected :attribute is invalid.', 'numeric' => 'The :attribute must be a number.', 'present' => 'The :attribute field must be present.', 'regex' => 'The :attribute format is invalid.', 'required' => 'The :attribute field is required.', 'required_if' => 'The :attribute field is required when :other is :value.', 'required_unless' => 'The :attribute field is required unless :other is in :values.', 'required_with' => 'The :attribute field is required when :values is present.', 'required_with_all' => 'The :attribute field is required when :values is present.', 'required_without' => 'The :attribute field is required when :values is not present.', 'required_without_all' => 'The :attribute field is required when none of :values are present.', 'same' => 'The :attribute and :other must match.', 'size' => [ 'numeric' => 'The :attribute must be :size.', 'file' => 'The :attribute must be :size kilobytes.', 'string' => 'The :attribute must be :size characters.', 'array' => 'The :attribute must contain :size items.', ], 'string' => 'The :attribute must be a string.', 'timezone' => 'The :attribute must be a valid zone.', 'unique' => 'The :attribute has already been taken.', 'uploaded' => 'The :attribute failed to upload.', 'url' => 'The :attribute format is invalid.', |