Dylan Buth
Dylan Buth

Reputation: 1666

Namespace, class not loading

I am new to namespaces. Using laravel.

my folder structure is

 app
   -lib
     -Services
         -Users
            -UserCreator.php
   -controllers
     -SiteController.php
 composer.json

My problem is it says Class Services/Users/UserCreator does not exist everytime I try and load it.

UserCreator.php:

 <?php

 namespace Services\Users;

 class UserCreator {

 }

SiteController.php

 <?php

 use Services\Users\UserCreator;

 // more code here...

 if ($validator->fails()) {
        echo 'fail';
    } else {
        $userCreator = App::make('Services/Users/UserCreator');
        if ($userCreator::create(Input::all())) {
            return Redirect::to('/')->with('message', 'Account Created');
        } else {
            return Redirect::to('/error');
        }
    }
  }

Composer.json

 "autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ],
    "psr-4": {
        "Services\\Users\\": "app/lib/Services/Users/UserCreator"
    }
}

I only included what I thought you would need. Let me know if you need anything else. Thanks!

Upvotes: 0

Views: 81

Answers (1)

peterm
peterm

Reputation: 92785

In your composer.json change psr-4 section to this

"psr-4": {
    "Services\\": "app/lib/Services"
}

Also change

$userCreator = App::make('Services/Users/UserCreator');

to

$userCreator = App::make('Services\Users\UserCreator');
                                  ^     ^

Upvotes: 2

Related Questions