ReflectionException: Class ClassName does not exist - Laravel

久未见 提交于 2019-11-27 12:18:21

Perform a composer update, then composer dump-autoload.

If the above doesn't solve the problem, change the classmap in your composer.json file such that it contains the project-relative path to your php files:

"autoload-dev": {
    "classmap": [
        "tests/TestCase.php",
        "database/seeds/UserTableSeeder.php" //include the file with its path here
    ]
}, /** ... */

and soon after, perform a composer dump-autoload, and it should work now like a breeze!

Edit by @JMSamudio

If composer dump-autoload is not found, just enable this option composer config -g -- disable-tls true.

From my experience, this will show up most of the time when the class you are trying to call has some bugs and cannot be compiled. Please check if the class that is not being reflected can be executed at its own.

A composer dump-autoload should fix it.

I had this problem and I could solve it by doing php artisan config:cache. The problem was that I had already run that command previously and later included some new seeder classes. The cached configurations didn't recognize the new classes. So running that command again worked.

If you see yourself making frequent changes to include new seeder classes then consider running php artisan config:clear. This will enable you to make as many changes as you'd like and then after testing you can run config:cache again to make things run optimally again.

For some reason I had to run composer dumpautoload -o. Notice flag -o which mean with optimization. I don't have any idea why it works only with it but give it a try. Perhaps it helps someone.

Luciano Braga

I have the same problem with a class. I tried composer dump-autoload and php artisan config:clear but it did not solve my problem.

Then I decided to read my code to find the problem and I found the problem. The problem in my case was a missing comma in my class. See my Model code:

{
    protected
    $fillable = ['agente_id', 'matter_id', 'amendment_id', 'tipo_id'];

    public
    $rules = [
        'agente_id' => 'required', // <= See the comma
        'tipo_id' => 'required'
    ];

    public
    $niceNames = [
        'agente_id' => 'Membro', // <= This comma is missing on my code
        'tipo_id' => 'Membro'
    ];
}

try to use following command

php artisan db:seed --class=DatabaseSeeder

You need to assign it to a name space for it to be found.

namespace App\Http\Controllers;

I am not writing as the answer for this question. But I want to help others if they faced the same bug but the answers mentioned here not works. I also tried all the solutions mentioned here. But my problem was with the namespace I used. The path was wrong.

The namespace I used is:

namespace App\Http\Controllers;

But actually the controller reside inside a folder named 'FrontEnd'

so the solution is change the namespace to:

namespace App\Http\Controllers\Frontend;
composer dump-autoload
php artisan config:clear
php artisan cache:clear

This will surely work. If not then, you need to do

composer update

and then, the aforementioned commands.

Fazil Raza

Check your capitalization!

Your host system (Windows or Mac) is case insensitive by default, and Homestead inherits this behavior. Your production server on the other hand is case sensitive.

Whenever you get a ClassNotFound Exception check the following:

  1. Spelling
  2. Namespaces
  3. Capitalization

When it is looking for the seeder class file, you can run composer dump-autoload. When you run it again and it's looking for the Model, you can reference it on the seeder file itself. Like so,

use App\{Model};

Not strictly related to the question but received the error ReflectionException: Class config does not exist

I had added a new .env variable with spaces in it. Running php artisan config:clear told me that any .env variable with spaces in it should be surrounded by "s.

Did this and my application stated working again, no need for config clear as still in development on Laravel Homestead (5.4)

I've recreated class, and it worked. Copy all file content, delete file, and run php artisan make:seeder ModelTableSeeder. Because composer dump-autoload didn't worked for me.

You may try to write app in use uppercase, so App. Worked for me.

Richard kortby
composer dump-autoload 

this will fix it

Usually error message ReflectionException: Class app does not exist appears in larval test when you forget to close connection.

if you're using setUp or tearDown function in your test do not forget to close connection by calling parent::setUp and parent::tearDown

    public function setUp()
    {
        parent::setUp();
    }

    public function tearDown()
    {
        parent::tearDown();
    }

In case of using psr-0 , check the class name does not contain underscore .

KNK
composer update 

Above command worked for me.

Whenever you make new migration in la-ravel you need to refresh classmap in composer.json file .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!