Laravel 5.4: Class 'Form' not found エラーが出る

What?

  • illuminate/html を使って、フォームを作ろうとすると、Class 'Form' not found (View: /home/vagrant/Code/laravel/resources/views/articles/create.blade.php) エラーが出る

Why?

  • Laravel 5.2 から仕様がちょっと変わった
    • illuminate/html ではなく、 laravelcollective/htmlを使う
  • Collective\Html\HtmlServiceProvider::class,HTMLを大文字にしてた。。。

How to?

  • composer require laravelcollective/htmlする
$ composer require laravelcollective/html
Using version ^5.4 for laravelcollective/html
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
The compiled services file has been removed.
laravel vagrant master *%$ composer dump-autoload -o
Generating optimized autoload files
  • config/app.php
'providers' => [
    ...
    Collective\HTML\HtmlServiceProvider::class,
    ...
],

...

'aliases' => [
    ...
    'Form' => Collective\Html\FormFacade::class,
    'Html' => Collective\Html\HtmlFacade::class,
    ...
],
  • ん?できない。
    • エラーが変わった
FatalThrowableError in ProviderRepository.php line 201:
Class 'Collective\HTML\HtmlServiceProvider' not found
  • なんでだよ。。。ちゃんとインストールしてるよ。マッピングされてるよ。。キャッシュもクリアしてオートロードできてるはずなんだよ。。
$ ls -la vendor/laravelcollective/html/src/HtmlServiceProvider.php
-rw-r--r-- 1 vagrant vagrant 1489 Mar  9 15:10 vendor/laravelcollective/html/src/HtmlServiceProvider.php
$ composer info | grep laravel
laravel/framework                     v5.4.15 The Laravel Framework.
laravel/tinker                        v1.0.0  Powerful REPL for the Laravel framework.
laravelcollective/html                v5.4.1  HTML and Form Builders for the Laravel Framework
$ grep -r HtmlServiceProvider vendor/composer/
vendor/composer/autoload_classmap.php:    'Collective\\Html\\HtmlServiceProvider' => $vendorDir . '/laravelcollective/html/src/HtmlServiceProvider.php',
vendor/composer/autoload_static.php:        'Collective\\Html\\HtmlServiceProvider' => __DIR__ . '/..' . '/laravelcollective/html/src/HtmlServiceProvider.php',
  • 3日悩んだ結果。
'providers' => [
    ...
    Collective\Html\HtmlServiceProvider::class,
    ...
],
  • 答え: HTMLじゃなくてHtmlな。。。
    • ディレクトリ名なので大文字小文字が厳密に判断される
      • composerを使って、オートロードしているので、以下と一緒じゃなきゃだめ
$ less vendor/composer/installed.json
        "autoload": {
            "psr-4": {
                "Collective\\Html\\": "src/"
            },

次、困ったときは順番にこれをやる

  • Issue composer require “laravelcollective/html”:“^5.3.0”.
  • Verify that the vendor/laravelcollective/html directory exists.
  • Verify that your composer.json file has been updated and there’s an entry for laravelcollective/html package in your dependencies (require block).
  • Add Collective\Html\HtmlServiceProvider::class to your providers array of config/app.php.
  • Add ‘Form’ => Collective\Html\FormFacade::class and ‘Html’ => Collective\Html\HtmlFacade::class to your aliases array.
  • Confirm that the API exists: Issue a php artisan tinker command and try playing with Form:: APIs. See if throws any errors.

知見

  • ハマったら、キャッシュクリアしたり、再インストールしたりする(今回あんま関係なかったけど)
$ composer dump-autoload
$ composer update
$ composer clear-cache
$ php artisan route:clear
$ php artisan cache:clear
$ php artisan config:clear
$ composer remove laravelcollective/html
$ composer require laravelcollective/html

参考