php 为什么我必须运行“composer dump-autoload”命令才能使迁移在 Laravel 中工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33973967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:40:56  来源:igfitidea点击:

Why do I have to run "composer dump-autoload" command to make migrations work in laravel?

phplaravellaravel-5

提问by Hasan Al-Natour

I have built some migration classes in my application to create the tables I need, but I keep getting errors. I need to run this command:

我在我的应用程序中构建了一些迁移类来创建我需要的表,但我不断收到错误消息。我需要运行这个命令:

composer dump-autoload

composer dump-autoload

Only then it works again as expected. Am I doing something wrong that generates this error or this is a normal behaviour with migrations?

只有这样它才能再次按预期工作。我是不是做错了什么导致了这个错误,或者这是迁移的正常行为?

Below is the error that I get when running the migration process:

以下是我在运行迁移过程时遇到的错误:

  [Symfony\Component\Debug\Exception\FatalErrorException]  
  Class 'CreateVideoStatusTable' not found  

回答by Duenna

OK so I think i know the issue you're having.

好的,我想我知道您遇到的问题。

Basically, because Composer can't see the migration files you are creating, you are having 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), and this is why your migration is working after you run that command.

基本上,因为 Composer 看不到您正在创建的迁移文件,您必须运行 dump-autoload 命令,该命令不会下载任何新内容,但会再次查找它需要包含的所有类。它只是重新生成需要包含在项目中的所有类的列表 (autoload_classmap.php),这就是运行该命令后迁移工作的原因。

How to fix it (possibly) You need to add some extra information to your composer.json file.

如何修复它(可能) 您需要在 composer.json 文件中添加一些额外信息。

"autoload": {
    "classmap": [
        "PATH TO YOUR MIGRATIONS FOLDER"
    ],
}

You need to add the path to your migrations folder to the classmap array. Then run the following three commands...

您需要将迁移文件夹的路径添加到类映射数组中。然后运行以下三个命令...

php artisan clear-compiled 
composer dump-autoload
php artisan optimize

This will clear the current compiled files, update the classes it needs and then write them back out so you don't have to do it again.

这将清除当前编译的文件,更新它需要的类,然后将它们写回,这样您就不必再次执行此操作。

Ideally, you execute composer dump-autoload -o, for a faster load of your webpages. The only reason it is not default, is because it takes a bit longer to generate (but is only slightly noticable).

理想情况下,您执行composer dump-autoload -o,以便更快地加载您的网页。它不是默认值的唯一原因是它需要更长的时间来生成(但只是稍微值得注意)。

Hope you can manage to get this sorted, as its very annoying indeed :(

希望你能设法解决这个问题,因为它确实很烦人:(

回答by afshindadashnezhad

You should run:

你应该运行:

composer dump-autoload

and if does not work you should:

如果不起作用,您应该:

re-install composer

回答by Daniel W.

Short answer: classmaps are static while PSR autoloading is dynamic.

简短回答:类映射是静态的,而 PSR 自动加载是动态的。

If you don't want to use classmaps, use PSR autoloading instead.

如果您不想使用类映射,请改用 PSR 自动加载。