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

bulgaria_mitko's avatar

Why i have to dump-autoload every time i create a new class?

Hello there i am new to laravel and i am following the this video https://laracasts.com/series/whip-monstrous-code-into-shape/episodes/2?autoplay=true and i have created a new usecase inside App\UseCases\PurchasePodcast.php the code of the file is this one:

namespace App\UseCases;

Class PurchasePodcast extends UseCases
{
    public function handle()
    {
        $this->preparePurchase()
            ->sendEmail();
    }

    private function preparePurchase()
    {
        echo "<pre>";
        print_r('prepering the purchase');
        echo "</pre>";

        return $this;
    }

    private function sendEmail()
    {
        echo "<pre>";
        print_r('send an email with their invoice');
        echo "</pre>";

        return $this;
    }
}

then i have created the UseCase class and the code is this one:

namespace App\UseCases;

abstract class UseCases
{
    public static function perform()
    {
        return (new static)->handle();
    }

    abstract public function handle();
}

and why i tried to run the application/website i got an error that it cannot find the UseCase class. Then i have to run composer dump-autoload and restart the server and then the website is working fine again and was able to find the UseCase class, but my question is why i have to run composer dump-autoload everytime i create manually a new class?

0 likes
19 replies
rin4ik's avatar

you have to run the dump-autoload command which won't download anything new, but looks for all of the classes it needs to include again. It just regenerates the list of all classes that need to be included in the project (autoload_classmap.php)

bulgaria_mitko's avatar

and why the laravel project it is not build so that when i run php artisan serve to automatically run composer dump-autoload in the background? That makes more sense to me

rin4ik's avatar

maybe, but you don't need to run it every time

bulgaria_mitko's avatar

yes, that's true, but also how often do you run the server. once per day maybe twice?

rin4ik's avatar
rin4ik
Best Answer
Level 50

I'm using laravel valet. I don't need to run serve at all) it is always up) and recommend you to switch to valet if you are on mac or linux.

1 like
bulgaria_mitko's avatar

yes i am using valet as well, but only for some projects. Will valet recognise my new class of UseCase without running composer dump-autoload?

click's avatar

I use homestead (vagrant + virtualbox) and experience no problems at all. So if Valet solves your problem that is ok. But there must something else going on that is causing your problem.

It can be multiple things like wrong directory names (not correct use of capital letters) or just a wrong composer.json setup.

Do you see this in your composer.json file?

"autoload": {
        // ...
        "psr-4": {
            "App\": "app/"
        },
        // ....
    },

This SO thread is explaining what could be wrong and what the solution is. https://stackoverflow.com/questions/41784405/need-to-dump-autoload-to-everytime-i-add-a-new-class

bulgaria_mitko's avatar

@m-rk thanks for the answer. my current composer.json file is

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

maybe if i add under the classmap "app/UseCases", it will work?

btw, valet is working so far fine for me, but i wanted to know what is causing this issue

rin4ik's avatar

I didn't notice u have wrong namespace in your UseCases class

namespace App\UseCases;

should be

namespace App;

click's avatar

@bulgaria_mitko@yahoo.com could you make a screenshot of your directory structure. Especially within the app directory Or just post it here?

What @rin4ik says is is true only if the your abstract class UseCases is directly in the /app directory and not within the app/UseCases directory.

click's avatar

The filename UseCase.php is wrong. It should be equal to your class name that is UseCases.

bulgaria_mitko's avatar

thanks for that i was my mistake... but right now it happend again even with valet, i have create a new file in app/Http/Controllers/TeamMembersController.php and this is the code:

namespace App\Http\Controllers;

use App\Team;
use Illuminate\Http\Request;
use App\Policy\AddTeamMemberPolicy;

class TeamMembersController extends Controller
{
    public function store(Team $team)
    {
        // 1 way - make checks
        (new AddTeamMemberPolicy($team))->allows();

        return 'Add the user to the team.';
    }
}

and other file in app/Policy/AddTeamMemberPolicy.php file:

<?php

namespace App\Policy;

use App\Team;

Class AddTeamMemberPolicy
{
    protected $team;

    public function __construct(Team $team)
    {
        $this->team = $team;
    }


    public function allows()
    {
        // if you are not sighed in, no way
        if (auth()->guest()) {
            abort(403, 'You are not signed in.');
        }

        // if you are not the owner of the team, no way
        if ($this->team->owner_id != auth()->user()->id) {
            abort(403, 'You are not the owner of this team.');
        }

        // if your team is maxed out, no way
        if ($this->team->isMaxedOut()) {
            abort(403, 'Your team is mixed out.');
        }
    }
}

and even that i am using valet i get an error: `Class not found AddTeamMemberPolicy in file TeamMembersController.php on line 11

so i had to again composer dump-autoload in order for my code to start working again and find my new classes

bulgaria_mitko's avatar

i have notice that when i am creating classes from php artisan i don have any problems, but when i create them manually, i am suing sublime btw thats when it says that the class is not found

click's avatar

Valet or not has nothing todo with your problem. Are you 100% sure your namespaces are equal to your directory and file names?

It should not matter what IDE you use. Autoloading should be taken care of by the server not your IDE and it is not required to run composer dump-autoload everytime you create a new class. Something else is going on in your application but I can't see from here what exactly.

On a side note: the word class should be all lowercase you use Class YourClassName I doubt that this will solve your issue but using standards wherever you can will certainly help.

rin4ik's avatar

yeah it's not related to Valet at all. show us your folder structure like you did before

bulgaria_mitko's avatar

link to the folder structure https://imgur.com/a/KAxpx

@rin4ik and @m-rk , thanks for your time dealing with this situation i will keep on researching the subject i have fixed my snipped with with a capital C for class, but now i have fixed that so from now on it will be with lower case

rin4ik's avatar

you are welcome. from now on be more attentive)

Please or to participate in this conversation.