Laravel 5:如何将播种机类添加到自动加载?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27426139/
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
Laravel 5: How can I add seeder class to autoload?
提问by Dmitry
I follow the docs: http://laravel.com/docs/master/migrations#database-seeding
我按照文档:http: //laravel.com/docs/master/migrations#database-seeding
I placed UserTableSeeder
file near DatabaseSeeder
. In resources/database/seeds/
folder.
These files are without namespaces (only classes in app/
are namespaced).
我将UserTableSeeder
文件放在DatabaseSeeder
. 在resources/database/seeds/
文件夹中。
这些文件没有命名空间(只有其中的类app/
是命名空间的)。
Of course there is an exception: exception 'ReflectionException' with message 'Class UserTableSeeder does not exist'
当然也有例外: exception 'ReflectionException' with message 'Class UserTableSeeder does not exist'
What is the best way to solve this problem?
解决这个问题的最佳方法是什么?
回答by Eduardo Trujillo
The default Laravel 5 project has a classmap defined in its composer.json
:
默认的 Laravel 5 项目在其中定义了一个类映射composer.json
:
{
// ...
"autoload": {
"classmap": [
"database"
],
// ...
}
}
Run composer dump
every time you add or remove a class on your database
directory to update the Composer autoloader
composer dump
每次在database
目录中添加或删除类时运行以更新 Composer 自动加载器
Reference: https://github.com/laravel/laravel/blob/develop/composer.json
参考:https: //github.com/laravel/laravel/blob/develop/composer.json
回答by Andrés Damesón
You should use composer dump-autoload
command. From the docs:
你应该使用composer dump-autoload
命令。从文档:
Once you have written your seeder, you may need to regenerate Composer's autoloader using the dump-autoload command:
composer dump-autoload
编写播种机后,您可能需要使用 dump-autoload 命令重新生成 Composer 的自动加载器:
composer dump-autoload
Reference here.
参考这里。