简体   繁体   English

ReflectionException: Class ClassName 不存在 - Laravel

[英]ReflectionException: Class ClassName does not exist - Laravel

As soon, I am typing php artisan db:seed command.很快,我正在输入php artisan db:seed命令。

I'm getting Error Like:我收到错误,例如

[ReflectionException] [反射异常]
Class UserTableSeeder does not exist类 UserTableSeeder 不存在

root@dd-desktop:/opt/lampp/htdocs/dd/laravel# php artisan db:seed

Here, Is my UserTableSeeder.php & DatabaseSeeder.php Page在这里,是我的UserTableSeeder.phpDatabaseSeeder.php页面

UserTableSeeder.php UserTableSeeder.php

<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class UserTableSeeder extends Seeder
{    
    public function run()
    {
        DB::table('users')->delete();
        User::create(array(
        'name'     => 'Chris Sevilleja',
        'username' => 'sevilayha',
        'email'    => 'chris@scotch.io',
        'password' => Hash::make('awesome'),
        ));
    }    
}

DatabaseSeeder.php数据库浏览器.php

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Eloquent::unguard();
        $this->call('UserTableSeeder');
    }
}

I'm Referring This Link To Design & Develop Login Page.我引用此链接设计和开发登录页面。 Please Help me to resolve this issue.请帮我解决这个问题。 Thanks.谢谢。

Perform a composer update , then composer dump-autoload .执行composer update ,然后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:如果上述方法不能解决问题,请更改您的composer.json文件中的类classmap ,使其包含您的 php 文件的项目相对路径:

"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 !不久之后,执行composer dump-autoload ,它现在应该可以像微风一样工作了!

Edit by @JMSamudio由@JMSamudio 编辑

If composer dump-autoload is not found, just enable this option composer config -g -- disable-tls true .如果找不到composer dump-autoload ,只需启用此选项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.根据我的经验,当您尝试调用的类有一些错误并且无法编译时,大多数情况下都会出现这种情况。 Check if the class that is not being reflected can be executed at its own.检查未被反射的类是否可以自行执行。

composer dump-autoload应该修复它。

I had this problem and I could solve it by doing php artisan config:cache .我遇到了这个问题,我可以通过执行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 .如果您发现自己经常更改以包含新的播种机类,请考虑运行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.这将使您能够进行任意数量的更改,然后在测试之后您可以再次运行config:cache以使事情再次以最佳方式运行。

I have the same problem with a class .我对class有同样的问题。 I tried composer dump-autoload and php artisan config:clear but it did not solve my problem.我试过composer dump-autoloadphp artisan config:clear但它没有解决我的问题。

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'
    ];
}

Check your capitalization!检查您的大小写!

Your host system (Windows or Mac) is case insensitive by default, and Homestead inherits this behavior.你的主机系统(Windows 或 Mac)默认不区分大小写,Homestead 继承了这个行为。 Your production server on the other hand is case sensitive.另一方面,您的生产服务器区分大小写。

Whenever you get a ClassNotFound Exception check the following:每当您收到ClassNotFound异常时,请检查以下内容:

  1. Spelling拼写
  2. Namespaces命名空间
  3. Capitalization大写

For some reason I had to run composer dumpautoload -o .出于某种原因,我不得不运行composer dumpautoload -o Notice flag -o which mean with optimization.注意标志-o表示优化。 I don't have any idea why it works only with it but give it a try.我不知道为什么它只适用于它,但试一试。 Perhaps it helps someone.也许它可以帮助某人。

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.然后,上述命令。

您需要将它分配给一个名称空间才能找到它。

namespace App\Http\Controllers;

If the issue is not resolved by composer dump-autoload or is not a newly created class.如果问题没有被composer dump-autoload解决或者不是新创建的类。

Check for syntax errors in your class and its traits as well.检查您的类及其特征中的语法错误。 In my case, there was a syntax error in a Trait, but ReflectionException was shown for the actual Class using that Trait instead.在我的情况下,Trait 中存在语法错误,但使用该 Trait 为实际类显示了ReflectionException

When it is looking for the seeder class file, you can run composer dump-autoload .当它在寻找播种机类文件时,你可以运行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};

尝试使用以下命令

php artisan db:seed --class=DatabaseSeeder

Not strictly related to the question but received the error ReflectionException: Class config does not exist与问题不严格相关,但收到错误ReflectionException: Class config does not exist

I had added a new .env variable with spaces in it.我添加了一个新的.env变量,其中包含空格。 Running php artisan config:clear told me that any .env variable with spaces in it should be surrounded by " s.运行php artisan config:clear告诉我任何带有空格的.env变量都应该用" s 包围。

Did this and my application stated working again, no need for config clear as still in development on Laravel Homestead (5.4)这是否和我的应用程序声明再次工作,不需要明确的配置,因为仍在 Laravel Homestead (5.4) 上开发

Don't take this as the answer for this question.不要把这个当作这个问题的答案。 If it helps others with the same bug but the answers mentioned here not works for them.如果它可以帮助其他有相同错误的人,但这里提到的答案对他们不起作用。 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 '但实际上控制器驻留在名为“ FrontEnd ”的文件夹中

so the solution is change the namespace to:所以解决方案是将命名空间更改为:

namespace App\Http\Controllers\Frontend;

I've recreated class, and it worked.我重新创建了课程,并且奏效了。 Copy all file content, delete file, and run php artisan make:seeder ModelTableSeeder .复制所有文件内容,删除文件,然后运行php artisan make:seeder ModelTableSeeder Because composer dump-autoload didn't worked for me.因为composer dump-autoload对我不起作用。

You may try to write app in use uppercase, so App .您可以尝试use大写来编写app ,因此App . Worked for me.为我工作。

Usually error message ReflectionException: Class app does not exist appears in larval test when you forget to close connection.通常,当您忘记关闭连接时,幼虫测试中会出现错误消息ReflectionException: Class app does not exist

if you're using setUp or tearDown function in your test do not forget to close connection by calling parent::setUp and parent::tearDown如果您在测试中使用 setUp 或 tearDown 函数,请不要忘记通过调用parent::setUpparent::tearDown来关闭连接

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

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

如果使用psr-0 ,请检查类名不包含下划线。

I had this error when trying to reach an endpoint in a custom route file, that had a namespace prepended in RouteServiceProvider我在尝试访问自定义路由文件中的端点时遇到此错误,该文件在RouteServiceProvider前置了一个命名空间

  • The namespace in the class was correct.类中的命名空间是正确的。
  • The file path of the class file was correct.类文件的文件路径是正确的。
  • The controller call from the routes file was correct (when not taking the prepended namespace from the RouteServiceProvider into account)来自路由文件的控制器调用是正确的(当不考虑来自RouteServiceProvider的前置命名空间时)
  • The default $router->group(['namespace' ...]) in the RouteServiceProvider was incorrect . RouteServiceProvider中的默认$router->group(['namespace' ...])不正确

Updating the namespace in the RouteServiceProvider solved the issue, as the relative controller path specified in the routes file was now resolving correctly.更新RouteServiceProvider的命名空间解决了这个问题,因为路由文件中指定的相对控制器路径现在可以正确解析。

Check file/folder permissions检查文件/文件夹权限

I struggled with this error today, and no amount of cache, config, autoload clears did anything to help.我今天遇到了这个错误,没有任何缓存、配置、自动加载清除做任何帮助。 To add to the confusion, the error was thrown if initiated by a web request, but accessing the class in tinker worked fine.更令人困惑的是,如果由 Web 请求启动,则会引发错误,但在 tinker 中访问该类工作正常。

After checking for typo's, syntax errors, and incorrect namespaces, I ended up discovering it was a file permission issue.在检查了拼写错误、语法错误和不正确的命名空间后,我最终发现这是一个文件权限问题。 The folder and file containing my class did not have appropriate permissions so it was throwing this error.包含我的类的文件夹和文件没有适当的权限,所以它抛出了这个错误。 The incorrect permission level I had was 771 (folder) and 660 (file), by changing it to 775 and 664 I was able to get it working.我的错误权限级别是 771(文件夹)和 660(文件),通过将其更改为 775 和 664,我能够使其正常工作。

My understanding of the different behaviors is that when running from the command line it was reading the file as my user (which had all the permissions it needed), but when initiated from the web it uses the "other" permission group which could do nothing.我对不同行为的理解是,当从命令行运行时,它以我的用户身份读取文件(它具有所需的所有权限),但是当从网络启动时,它使用“其他”权限组,它无能为力.

composer dump-autoload 

这将解决它

composer update 

Above command worked for me.以上命令对我有用。

Whenever you make new migration in la-ravel you need to refresh classmap in composer.json file .每当您在 la- classmap中进行新迁移时,您都需要刷新composer.json文件中的类classmap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM