I'm trying to create a directory to store custom classes, so I create the directory app/ArgumentClub/Transformers
, and the class UserTransformer.php
in that folder.
I then autoload with:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-4": {
"ArgumentClub\\": "app/ArgumentClub"
}
},
And run composer dump-autoload
. And namespace like this:
<?php namespace ArgumentClub\Transformers;
class UserTransformer {
I'm calling this class within another class like this:
<?php
use Sorskod\Larasponse\Larasponse;
use ArgumentClub\Transformers;
class UsersController extends \BaseController {
...
$transformed = $this->fractal->collection($users, new UserTransformer());
But I get the error:
Class 'UserTransformer' not found
What am I doing wrong here?
You're not using the use
correctly.
use ArgumentClub\Transformers;
imports that Namespace, but doesn't import the class you want to use.
To fix it you can either extend the use
statement (which you should) to be like so:
use ArgumentClub\Transformers\UserTransformer
Or you can add the Transformers namespace to where you instantiate your UserTransformer class
$transformed = $this->fractal->collection($users, new Transformers\UserTransformer());
When you want to instantiate a namespaced class without putting the full namespace, you need to put the full class path in the use
statement.