我正在尝试使用以下的抽象类,我收到一个错误说:
Class'Acme \ Transformers \ 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']
];
}
}
<?php
namespace Acme\Transformers;
abstract class Transformer{
public function transformCollection($items){
return array_map([$this, 'transform'], $items);
}
abstract public function transform($item);
}
<?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)
{
//
}
}
{
"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
}
答案 0 :(得分:0)
不应该
use App\Acme\Transformers\LessonTransformer;
是这个
use Acme\Transformers\LessonTransformer;
...
答案 1 :(得分:0)
您已在以下命名空间下定义了变换器类:
namespace Acme\Transformers;
但您要导入LessonTransformer
:
use App\Acme\Transformers\LessonTransformer
哪个是不同的命名空间。所以你需要修改它:
use Acme\Transformers\LessonTransformer