commandantp
commandantp

Reputation: 967

Laravel: Created a service but class is not found

Created the following file:

File: App\Services\Custom\Auth\AuthService.php

Name space: App\Services\Custom\Auth

Class name: AuthCustom

Method inside: foo()

In my controller I'm trying to call the foo method from the Service I created. App\Services\Custom\Auth\AuthService\AuthCustom::foo()

Why does it keep returning Class 'App\Services\Custom\Auth\Authservice\AuthCustom' not found

What am I doing wrong?

Thanks!!

EDIT: I added this in the composer.json and run composer dump-autoload without errors. And it works!

"autoload": {
    "classmap": [
        "database",
        "app/Services/Custom/Auth/AuthService.php"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},

Upvotes: 1

Views: 789

Answers (3)

patricus
patricus

Reputation: 62228

Your namespace does not match your directory structure. If your class is in App\Services\Custom\Auth\AuthService.php, then your namespace needs to be App\Services\Custom\Auth. If you really want your namespace to be App\Custom\Auth, then your file needs to be App\Custom\Auth\AuthService.php.

Once you fix this, make sure you do a composer dump-autoload on the command line.

Upvotes: 2

mkbrv
mkbrv

Reputation: 407

Laravel needs an big file with all your php files required, usually generated via calling either artisan or composer with : php artisan dump-autoload / composer dump-autoload

It just regenerates the list of all classes that need to be included in the project (autoload_classmap.php). Ideal for when you have a new class inside your project.

More details: http://developed.be/2014/08/29/composer-dump-autoload-laravel/

Upvotes: 0

schellingerht
schellingerht

Reputation: 5796

It seems that you didn't run composer dump-autoload or php composer.phar dump-autoload.

The composer.json is very important for autoloading!

Upvotes: 0

Related Questions