PHP Class not found in Composer Autoload After Moving Project to New Folder
You move your PHP or Laravel project to a new folder or hosting account, open it in the browser or CLI, and suddenly see errors like: Class “App\Services\PaymentService” not found or in Laravel: Target class [App\Http\Controllers\HomeController] does not exist.
The code did not change, only the folder. The usual culprit is Composer’s autoload files and namespaces that still point to the old structure.
Let’s go through what you should check and how to fix it cleanly.
1. How Composer autoload actually finds your classes
Composer reads composer.json and builds a file called vendor/autoload.php, plus some maps under vendor/composer/.
Most projects use PSR-4 autoloading like this:
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
This means:
- Classes under the
App\namespace live in theapp/directory. App\Services\PaymentServiceshould be inapp/Services/PaymentService.php.
If you move the project but keep this structure, everything should still work. Problems appear when:
- The path in
composer.jsonno longer matches the real folder. - You moved only part of the project.
- Autoload files are still cached for the old location.
So first step is always to refresh Composer’s autoload.
2. Run Composer dump-autoload after moving
As soon as you move the project to a new folder (or new server), log into that folder and run:
composer dump-autoload
or, for optimized production:
composer dump-autoload -o
This regenerates all autoload maps with the new absolute paths.
If composer is not available globally on shared hosting, use the one in your project:
php composer.phar dump-autoload -o
For many people, this alone fixes the “Class not found” errors.
3. Confirm your namespaces and file paths still match
If errors still appear, check the exact class mentioned in the error.
Example error:
Class "App\Services\PaymentService" not found
Questions to ask:
- Does the file exist?
app/Services/PaymentService.php
- Does the namespace inside that file match?
<?php namespace App\Services; class PaymentService { // ... } - Is the filename exactly the same as the class (case sensitive on Linux)?
PaymentService.phpcontainingclass PaymentService
If the file is in a different directory after the move, update composer.json or move the file back to the correct PSR-4 path.
Example: custom folder
If you created a src folder and moved classes there, you must tell Composer:
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
Then run composer dump-autoload again.
4. Check you moved the entire vendor folder (or reinstall)
If you copied the project manually and skipped the vendor folder, nothing autoloads until dependencies are installed.
Inside the new project folder, run:
composer install
This recreates vendor/ and all autoload files based on composer.lock.
On slow shared hosting, this can take a bit longer, but it is the cleanest way to ensure everything matches the new environment.
5. Fix Laravel specific “Target class does not exist” problems
In Laravel, moving a project plus changing PHP versions or paths can cause route and container caches to point to the wrong place.
From the project root, run:
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
Then regenerate autoload again:
composer dump-autoload -o
If the error is about a controller in routes:
use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'index']);
Verify:
app/Http/Controllers/HomeController.phpexists.- Namespace at the top is
namespace App\Http\Controllers;. - Class name is
class HomeController extends Controller.
Sometimes after moving, people rename folders (for example, Controllers to controllers), which breaks on Linux but not on Windows because of case sensitivity.
6. Paths hard coded in custom autoload or include statements
If you previously added manual includes like:
require '/old/path/to/app/Helpers/helpers.php';
they will break in the new environment.
Better approach is to let Composer autoload helper files:
In composer.json:
"autoload": {
"psr-4": {
"App\\": "app/"
},
"files": [
"app/Helpers/helpers.php"
]
}
After adding this, run:
composer dump-autoload
Now helpers will load automatically, no absolute paths required.
7. Double check project root when using CLI
On a new server or folder, sometimes you run artisan or PHP scripts from the wrong directory. Composer then looks for vendor/autoload.php relative to that wrong place.
Always cd into the project root before running commands:
cd /var/www/new-folder/my-project
php artisan migrate
php artisan tinker
If you see errors like:
Warning: require(vendor/autoload.php): failed to open stream
you are probably not in the correct directory, or vendor/ is missing.
8. Common real-world scenarios and quick fixes
Scenario A: moved from /var/www/html to /var/www/project
Symptoms:
- “Class not found” on every request.
Fix:
cd /var/www/projectcomposer dump-autoload -o- Clear Laravel caches if using Laravel.
Scenario B: copied only app/ and resources/ to a new Laravel install
Symptoms:
- Some custom classes not found, especially services, Jobs, or Events.
Fix:
- Make sure
composer.jsonin the new project has the sameautoloadsection as the old one. - Move any custom folders (like
app/Services,app/Repositories) as well. - Run
composer dump-autoload.
Scenario C: changed namespace but forgot to update routes or code
You renamed App to MyApp in composer.json:
"psr-4": {
"MyApp\\": "app/"
}
but controllers still use namespace App\Http\Controllers;.
Fix: update namespaces in your PHP files to match MyApp\..., or revert the autoload config.
9. Quick checklist
When you see “Class not found” after moving a PHP or Laravel project:
- Run
composer dump-autoload -oin the new folder. - Confirm the class file exists where PSR-4 says it should.
- Make sure namespaces and filenames (case) are correct.
- Install or copy the
vendorfolder and runcomposer installif needed. - For Laravel, clear config, route, view, and app caches.
- Remove any old absolute paths and let Composer autoload helpers and libraries.
- Always run artisan and PHP scripts from the actual project root.
Once Composer has fresh autoload files and your namespaces match the new directory layout, the “Class not found” errors stop and your moved project behaves like it did before.