Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
How to deal with Class not found with composer

I installed composer and laravel and installed some packages
all work fine, but now I created my own class under the folder services
I gave it name space of Services like that:

namespace Services;

And the class name is UploadToImgurService

I run the composer command:

 composer dump-autoload

And in my controller I wrote:

 use Services\UploadToImgurService;

But I get this error:

 Class 'Services\UploadToImgurService' not found

What did I did wrong? Is there anything else that I should do with composer for autoloading the service class?

EDIT I found a solution
I edited y composer.json file and added to psr-4 the line

     "Services\\" : "app/services"

But why It didn't workt before? The line :

 "App\\": "app/",

was there, maybe it loaded the class but under the app namespace?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

If you are using a case sensitive file system, you will need to have the Services folder with upper case S.

about 4 years ago · Santiago Trujillo Report

0

Your idea is right, but let me try to explain how the psr-4 autoloading works.

You can define root namespaces in your composer.json file and map it to any project directory. Inside the defined directories, your classes should get the root namespace. The namespace segments after the root are build by your sub directory structure and the class name is equal to the file name (PSR-4 Autoloading).

E.g "MyNamespace\\WithSubNamespace\\": "cool/project"

cool/project/MyClass.php -> MyNamespace\WithSubNamespace\MyClass

cool/project/SubDirectory/AnotherClass.php -> MyNamespace\WithSubNamespace\SubDirectory\AnotherClass

In Laravel, the app directory is mapped to the App namespace as default. Optionally, you can change the root namespace with the command php artisan app:name [NewRootNamespaceName], but the autoloader only finds classes inside the app directory. If you create a new directory outside of "app", you have to add the directory to your psr-4 namespace mapping in the composer.json file.

In your example, you define a new root namespace in the existing app directory, so your issue was that the root namespace was unknown and you solved it by adding the line in your composer.json. This is possible, because psr-4 provides a huge flexibility. But personally, i would not recommend to use different root namespaces in the same project.

I hope i could help and maybe this is also interesting for you: composer.json PSR-4.

about 4 years ago · Santiago Trujillo Report

0

maybe you forgot to add this line of code

require_once('vendor/autoload.php');
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!