如何在 laravel 5.3 中实现摘要 class

How to implement Abstract class in laravel 5.3

我正在学习 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 中创建了一个摘要 class。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);
    }

}

我不知道我错过了什么 期待急需的帮助

谢谢

正如我们从错误消息中看到的那样,问题与抽象 class Transformer 无关,它无法达到 class 因为它无法找到LessonTransformer class?

Class App\Acme\Transformers\LessonTransformer does not exist

查看您的 classes 似乎所有命名空间都很好,如果您使用的是 PSR4,不需要 执行 composer dumpautoload,它会自动找到它.

但是没有找到您的 class,这通常是因为:

1) 文件放错地方了(在正确的目录中吗?)。

2) 文件命名不正确。

3) 您的文件有误,PHP 无法将其理解为 class。