首页 > 解决方案 > 为什么我在 Laravel 5.5.43 中收到“找不到类 'App\Providers\Form'”错误?

问题描述

今天早上我使用以下 Composer 命令安装了 Laravel 5.5.43:

composer create-project laravel/laravel project-name

然后我在项目文件夹中运行以下命令来安装 Laravel Collective:

composer require "laravelcollective/html":"^5.4.0"

providers然后我在下面添加了以下内容config/app.php

Collective\Html\HtmlServiceProvider::class,

aliases我在下面添加了以下内容config/app.php

'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,

之后,我想创建自定义表单组件,所以我运行了以下 Artisan 命令:

php artisan make:provider FormServiceProvider

在那个新的提供程序文件中,我在方法中添加了以下行boot

Form::component('customText', 'components.form.text', ['name', 'value' => null, 'attributes' => []]);

最后,我在providersin 中添加了以下内容config/app.php

App\Providers\FormServiceProvider::class,

当我这样做并从浏览器刷新 Laravel 实例时,我收到以下错误:

Class 'App\Providers\Form' not found

但是,如果我在文件顶部添加以下内容FormServiceProvider.php,则它可以工作:

use Collective\Html\FormFacade AS Form;

我了解名称空间的概念以及为什么这会使文件中的Form::component方法中的boot方法正常工作,但我不明白为什么我需要将该use行添加到文件中。

文件中数组中的'Form' => Collective\Html\FormFacade::class,行不是应该为我做的,所以我根本不需要添加行吗?我错过了什么/做错了什么/不理解?aliasesapp.phpuseFormServiceProvider.php

谢谢你。

标签: phplaravellaravel-5.5laravelcollective

解决方案


I probably should just delete this question, but the simple answer was to add the following at the top of the FormServiceProvider.php file:

use Form;

Thanks.


推荐阅读