vendredi 24 juin 2016

Laravel can't find class in namespace

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 AppLibraryTransformersLessonTransformer does not exist

Any ideas what could be wrong?

AppHttpControllersLessonsController.php

namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateHttpResponse;
use AppLibraryTransformersTransformer;
use AppLibraryTransformersLessonTransformer;
use AppHttpRequests;
use AppLesson;
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);
    }
} }

AppLibraryTransformersLessonTransformer

<?php
namespace AppLibraryTransformers;

public class LessonTransformer extends Transformer {

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

AppLibraryTransformersTransformer

<?php
namespace AppLibraryTransformers;

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.

Update My code can be found on github: https://github.com/isak-glans/laravelproblem

Aucun commentaire:

Enregistrer un commentaire