Я пытаюсь использовать абстрактный класс для следующего, и я получаю сообщение об ошибке:
Класс 'Acme \ Transformers \ LessonTransformer' не существует.
Я также добавил класс контроллера на случай, если с этим что-то не так. Кто-нибудь может дать мне знать, что я здесь пропускаю?
LessonTransformer
<?php
namespace Acme\Transformers;
class LessonTransformer extends Transformer {
public function transform($lesson){
return [
'title'=> $lesson['title'],
'body' => $lesson['body'],
'active' => (boolean) $lesson['some_bool']
];
}
}
Transformer
<?php
namespace Acme\Transformers;
abstract class Transformer{
public function transformCollection($items){
return array_map([$this, 'transform'], $items);
}
abstract public function transform($item);
}
LessonsController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Acme\Transformers\LessonTransformer;
use App\Lesson as Lesson;
class LessonsController extends Controller
{
protected $lessonTransformer;
function __construct(LessonTransformer $lessonTransformer){
$this->lessonTransformer = $lessonTransformer;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
// 1. All is bad
// 2. No way to attach meta data
// 3. Linking the Db structure to api output
// 4. No way to signal headers or response codes
// return Lesson::all();
$lessons = Lesson::all();
return response()->json([
'data' => $this->lessonTransformer->transformCollection($lessons->all())
], 200);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$lesson = Lesson::find($id);
if(!$lesson){
return response()->json([
'error' => [
'message' => 'lesson does not exist'
]
], 404);
}
return response()->json([
'data' => $this->lessonTransformer->transform($lesson)
],200);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
composer.json
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": "^7.1.3",
"fideloper/proxy": "^4.0",
"fzaninotto/faker": "^1.7",
"laravel/framework": "5.6.*",
"laravel/tinker": "^1.0"
},
"require-dev": {
"filp/whoops": "^2.0",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^2.0",
"phpunit/phpunit": "^7.0"
},
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/acme"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"dont-discover": [
]
}
},
"scripts": {
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate"
],
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover"
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
},
"minimum-stability": "dev",
"prefer-stable": true
}