2

Problem. When I instantiate an class object from an another namespace (within my controller) I get this error:

ReflectionException in Container.php line 794:
Class App\Library\Transformers\LessonTransformer does not exist

Any ideas what could be wrong?

App\Http\Controllers\LessonsController.php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Library\Transformers\Transformer;
use App\Library\Transformers\LessonTransformer;
use App\Http\Requests;
use App\Lesson;
class LessonsController extends Controller{
protected $lessonTransformer;

function __construct(LessonTransformers $lessonTransformer)
{
    $this->lessonTransformer = $lessonTransformer;
}

//
public function index()
{
    $lessons =  Lesson::all();

    return Response()->json([
        'data' => $this->lessonTransformer->transformCollection($lessons->all() )
    ], 200);
}

public function show($id)
{
    $lesson = Lesson::find($id);

    if ( ! $lesson )
    {
        return Response()->json([
            'error' => [
                'message' => 'Lesson does not exist'
            ]
        ], 404);
    }
} }

App\Library\Transformers\LessonTransformer

<?php
namespace App\Library\Transformers;

public class LessonTransformer extends Transformer {

    public function transform($lesson)
    {
        return [
            'title'     => $lesson['title'],
            'body'      => $lesson['body'],
            'active'    => (boolean) $lesson['some_bool']
        ];
    }
}

App\Library\Transformers\Transformer

<?php
namespace App\Library\Transformers;

public abstract class Transformer {
    public function transformCollection(array $item)
    {
        return array_map([$this, 'transform'], $item->toArray());
    }

    public abstract function transform($item);
}

Composer.json

I have also tried to change composer.json like this, but without success:

"autoload": {
        "classmap": [
            "database",
            "App/Library"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php",
            "App/Library"
        ]
    },

Sidenote. I am using Laravel 5.2.

5
  • Please do not modify the composer file unless you know what you're doing - revert those changes Commented Jun 18, 2016 at 15:01
  • Okey. Though I tried reverting those changes, but still no change. Commented Jun 18, 2016 at 15:04
  • Looks like a slight typo in your constructor - shouldn't LessonTransformers not have the trailing s? Don't think this explains the error though. Commented Jun 18, 2016 at 15:45
  • Have an answer in progress.. Commented Jun 18, 2016 at 17:19
  • Why can't you instantiate with new LessonTransformers() ? Commented Jun 18, 2016 at 17:21

1 Answer 1

2

LessonsController

Transformer

You had set visibility on a class which does not take visibility.

LessonTransformer

Also note that you did not append .php on LessonTransformer.php. This is what we call "a big issue".

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, you saved my day! :) May I ask what addon/program you are using to find those errors?
It seems like Laravel demands that the namespace:ing is exactly as the folder path. Or am I wrong?
You're welcome. I'm just using the laravel errors supplied, but it is easier to debug when I have the code in front of me, there is no chance in hell anyone could have told you to check the name of your file. Look up about namespacing, as much info as you can find - it took me a few reads to get to grips with the first time. There were other issues with the code as well as you could see - but the major one is .php missing on the file
I did not understand it was a submit, so I have changed my code locally by watching the images your published. About the submit, no need to delete it, I will delete the rep tomorrow or so. Again, thanks Jonathan!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.