ユーザ用ツール

サイト用ツール


サイドバー

このページの翻訳:



最近の更新



Tag Cloud

11_php:02_framework:01_laravel:26_laravel_form_builder

26 Laravel FormBuilder

インストール

composer require kris/laravel-form-builder

From class 作成

本当にシンプルなFormをFormBuilderで作成してみる。
Formをsubmitすると、ただリクエストを表示するだけ。

php artisan make:form Forms/SongForm --fields="name:text, lyrics:textarea, publish:checkbox"

app/Forms/SongForm.php

<?php

namespace App\Forms;

use Kris\LaravelFormBuilder\Form;

class SongForm extends Form
{
    public function buildForm()
    {
        $this
            ->add('name', 'text')
            ->add('lyrics', 'textarea')
            ->add('publish', 'checkbox')
            ->add('submit', 'submit');
    }
}

コントローラ作成

php artisan make:controller SongsController

app/Http/Controllers/SongsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Kris\LaravelFormBuilder\FormBuilder;

class SongsController extends Controller
{
    public function create(FormBuilder $formBuilder, Request $request)
    {
        $form = $formBuilder->create('App\Forms\SongForm', [
            'method' => 'POST',
            'url' => route('song'),
        ]);
        $req = $request->all();

        return view('song', compact('form', 'req'));
    }
}

route追加

GET/POST 共に受け取れるように設定

routes/web.php

Route::any('song','SongsController@create')->name('song');

view追加

resources/views/song.blade.php

<!-- resources/views/song.blade.php -->

<!DOCTYPE HTML>
<html>
<head>
    <title>FormBuilder</title>
</head>
<body>
    <h1>FormBuilder TEST</h1>

@section('content')
    {!! form($form) !!}
    @foreach ($req as $k => $v)
        <li>{{ $k .' : '.$v }}</li>
    @endforeach
@endsection
@yield('content')

</body>
</html>

エラー

Using version ^1.20 for kris/laravel-form-builder
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
  - Installing laravelcollective/html (v5.8.0): Downloading (100%)
  - Installing kris/laravel-form-builder (1.20.0): Downloading (100%)
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

In MemcachedConnector.php line 69:

  Class 'Memcached' not found

対応

.envでmemocachedを有効にしているとエラーが出ので

SESSION_DRIVER=memcached
↓
SESSION_DRIVER=file
11_php/02_framework/01_laravel/26_laravel_form_builder.txt · 最終更新: 2019/04/01 22:15 by matsui