0

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!

1 Answer 1

2

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');
                                  ^     ^
Sign up to request clarification or add additional context in comments.

Comments

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.