Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nhussain's avatar

external classes and PHP files in Laravel - Class not found

I have two php files and I already created a folder called "ExternalClasses" and added the file to that folder. In my php file, I add this line namespace App\ExternalClasses;

and in my controller I add this line use App\ExternalClasses\CCheckMail;

and this is how I use it: $pricesClass = new CCheckMail(); $email2 = ['[email protected]']; $prices = $pricesClass->execute ($email2); return view('pages.home', compact('prices'));

but it gave me an error: Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Class 'App\ExternalClasses\CCheckMail' not found

Here is my php file (CCheckMail.php):-

0 likes
8 replies
Digitalized's avatar

Does the folder structure where CCheckMail resides also match the namespace? eg. App > ExternalClasses > CCheckMail?

nhussain's avatar

Yes, the CCheckMail.php is in app->ExternalClasses->CCheckMail.php

Digitalized's avatar

If you are autoloading using psr-4, check to see whether the App folder is being autoloaded in your composer.json file in its entirety. For example, in my own projects I have:

"autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\": "app/"
        }
    },

In this example, App is referenced, so any sub directories of App should be autoloaded, including your ExternalClasses folder

romulo27's avatar

Rename the folder to: Classes, inside the App.

Digitalized's avatar

If that doesn't highlight the issue, try running composer dump-autoload from the command line in your project

romulo27's avatar

I had this problem yesterday. I create an folder.

App\Classes, inside, NameClass.php.

nhussain's avatar

Thank you.. I think it is type issue..

1 like
Cronix's avatar

And the custom class has namespace App\ExternalClasses; at the top?

Please or to participate in this conversation.