Error: Class Does not exist in Laravel
Error: Class Does not exist in Laravel
我遇到以下错误
InvalidArgumentException
在 FormBuilder.php
第 39 行:
名称为 App\Http\Controllers\App\Forms\SongForm
的表单 class 不存在。
于 Laravel、
SongsController.php class
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use Kris\LaravelFormBuilder\FormBuilder;
class SongsController extends BaseController {
public function create(FormBuilder $formBuilder)
{
$form = $formBuilder->create(App\Forms\SongForm::class, [
'method' => 'POST',
'url' => route('song.store')
]);
return view('song.create', compact('form'));
}
public function store(FormBuilder $formBuilder)
{
$form = $formBuilder->create(App\Forms\SongForm::class);
if (!$form->isValid()) {
return redirect()->back()->withErrors($form->getErrors())->withInput();
}
// Do saving and other things...
}
}
SongForm.php
<?php
namespace App\Forms;
use Kris\LaravelFormBuilder\Form;
class SongForm extends Form
{
public function buildForm()
{
$this
->add('name', 'text', [
'rules' => 'required|min:5'
])
->add('lyrics', 'textarea', [
'rules' => 'max:5000'
])
->add('publish', 'checkbox');
}
}
routes.php
Route::get('songs/create', [
'uses' => 'SongsController@create',
'as' => 'song.create'
]);
Route::post('songs', [
'uses' => 'SongsController@store',
'as' => 'song.store'
]);
而且我不知道问题出在哪里,因为该文件存在于项目文件夹中。
错误解释
这里:
$form = $formBuilder->create(App\Forms\SongForm::class, [
'method' => 'POST',
'url' => route('song.store')
]);
您正在使用相对于当前命名空间的命名空间指定 class 名称:
App\Forms\SongForm::class
完整的 class 名称将从当前命名空间相对构建,即:
namespace App\Http\Controllers;
因此,您作为参数传递的 class 变为:
App\Http\Controllers\App\Forms\SongForm::class
class 不存在,所以你得到错误
如何解决
解决,可以指定绝对命名空间。改变这个:
App\Forms\SongForm::class
对此:
\App\Forms\SongForm::class
它应该可以工作
我遇到以下错误
InvalidArgumentException
在 FormBuilder.php
第 39 行:
名称为 App\Http\Controllers\App\Forms\SongForm
的表单 class 不存在。
于 Laravel、
SongsController.php class
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use Kris\LaravelFormBuilder\FormBuilder;
class SongsController extends BaseController {
public function create(FormBuilder $formBuilder)
{
$form = $formBuilder->create(App\Forms\SongForm::class, [
'method' => 'POST',
'url' => route('song.store')
]);
return view('song.create', compact('form'));
}
public function store(FormBuilder $formBuilder)
{
$form = $formBuilder->create(App\Forms\SongForm::class);
if (!$form->isValid()) {
return redirect()->back()->withErrors($form->getErrors())->withInput();
}
// Do saving and other things...
}
}
SongForm.php
<?php
namespace App\Forms;
use Kris\LaravelFormBuilder\Form;
class SongForm extends Form
{
public function buildForm()
{
$this
->add('name', 'text', [
'rules' => 'required|min:5'
])
->add('lyrics', 'textarea', [
'rules' => 'max:5000'
])
->add('publish', 'checkbox');
}
}
routes.php
Route::get('songs/create', [
'uses' => 'SongsController@create',
'as' => 'song.create'
]);
Route::post('songs', [
'uses' => 'SongsController@store',
'as' => 'song.store'
]);
而且我不知道问题出在哪里,因为该文件存在于项目文件夹中。
错误解释
这里:
$form = $formBuilder->create(App\Forms\SongForm::class, [
'method' => 'POST',
'url' => route('song.store')
]);
您正在使用相对于当前命名空间的命名空间指定 class 名称:
App\Forms\SongForm::class
完整的 class 名称将从当前命名空间相对构建,即:
namespace App\Http\Controllers;
因此,您作为参数传递的 class 变为:
App\Http\Controllers\App\Forms\SongForm::class
class 不存在,所以你得到错误
如何解决
解决,可以指定绝对命名空间。改变这个:
App\Forms\SongForm::class
对此:
\App\Forms\SongForm::class
它应该可以工作