I'm having an issue in laravel 4.1. I'm following a tutorial series. I have "Acme/Transformers" folder in "app" directory which has two class "Transformer.php" and "LessonTransformer.php". When i tried to access "LessonTransformer.php" in LessonController class of Controller directory i'm getting the following issue.
ReflectionException
Class Acme\Transformers\LessonTransformer does not exist
I updated my composer.json file like following.
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/Acme"
],
"psr-0":{
"Acme" : "app/Acme"
}
and ran
"composer dump-autoload -o"
but it made no change in the output. I have no ideas what's going on. Please help me.
Here's is my LessonsController.php class
use Acme\Transformers\LessonTransformer;
class LessonsController extends \BaseController {
/**
* @var Acme\Transformers\LessonTransformer
*/
protected $lessonTransformer;
function __construct(LessonTransformer $lessonTransformer){
$this->lessonTransformer = $lessonTransformer;
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$lessons = Lesson::all();
return Response::json([
'lessons' => $this->lessonTransformer->transformCollection($lessons->toArray())
], 200);
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$lesson = Lesson::find($id);
if(!$lesson){
return Response::json([
'error' => [
'mesage' => 'Lessons does not exits'
]
], 404);
}
return Response::json([
'lesson' => $this->lessonTransformer->transform($lesson)
], 200);
}
/**
* Map over the lesson collection and cast it in an array,
* so that we can map over it.
*
* Transform a collection of lesson
*
* @param array $lessons
* @return array
*/
public function transformCollection(array $lessons){
return array_map([$this,'transform'], $lessons);
}
/**
* Transform a single object(lesson)
* Instead of returning everything, we return some selected data.
* We replaced the keys that exposed the DB structure with some
* other keys because if we wish to change the keys of data in
* future then we can change it without affecting the DB structure.
*
* @param array $lesson
* @return array
*/
public function transform($lesson){
return [
'title' => $lesson['title'],
'body' => $lesson['body'],
'active' => (boolean)$lesson['some_bool']
];
}
}