我正在关注Laracasts Api课程,并且在某一点上我得到了这个错误
Whoops, looks like something went wrong.
ReflectionException in Container.php line 809:
Class App\Acme\Transformers\LessonTransformer does not exist
我在app \ Acme \ Transformers \ Transformer.php
中创建了一个抽象类<?php
namespace App\Acme\Transformers;
abstract class Transformer {
//transformCollection the lessons data and return only requried fields
public function transformCollection($items) {
return array_map([$this, 'transform'], $items);
}
//transform the lessons data and return only requried fields of perticular id
public abstract function transform($item);
}
和app \ Acme \ Transformers \ LessonTransformer.php
<? php
namespace App\Acme\Transformers;
class LessonTransformer extends Transformer {
public function transform($lesson) {
return [
'title' => $lesson['title'],
'body' => $lesson['body'],
'active' => (boolean) $lesson['completed']
];
}
}
我的控制器是LessonsController.php
<?php
namespace App\Http\Controllers;
use App\lesson;
use Response;
use Illuminate\Http\Request;
use App\Acme\Transformers\LessonTransformer;
class LessonsController extends Controller {
protected $lessonTransformer;
function __construct(LessonTransformer $lessonTransformer) {
$this->lessonTransformer = $lessonTransformer;
}
//fetch all and pass a metadata 'data'
public function index() {
$lessons = Lesson::all();
return Response::json([
'data' => $this->lessonTransformer->transform($lessons)
], 200);
}
//fetch by id
public function show($id) {
$lesson = Lesson::find($id);
if(! $lesson) {
return Response::json([
'error' => [
'message' => 'No Response Please Try Again'
]
], 404);
}
return Response::json([
'data' => $this->lessonTransformer->transform($lesson)
], 200);
}
}
我不知道我错过了什么 期待急需的帮助
谢谢
答案 0 :(得分:1)
正如我们从错误消息中看到的那样,问题与Abstract class Transformer无关,它还无法到达那个类,因为它无法找到LessonTransformer类?
Class App\Acme\Transformers\LessonTransformer does not exist
查看你的类似乎所有命名空间都很好,如果你使用PSR4,没有必要来执行composer dumpautoload,它会自动找到它。
但是找不到你的课,通常会发生这种情况,因为:
1)文件放错位置(是否在正确的目录中?)。
2)文件名称不正确。
3)文件中有错误,PHP无法将其理解为类。