Laravel IDE Helper 是一个极其好用的代码提示及补全工具,可以给编写代码带来极大的便利。
安装 larave-ide-helper# 如果只想在开发环境安装请加上 --dev
composer require barryvdh/laravel-ide-helper
安装 doctrine/dbal 「请装上它,在为模型注释字段的时候必须用到它」
# 如果只想在开发环境安装请加上 --dev
composer require "doctrine/dbal: ~2.3"
在 「config/app.php」的 「providers」数组中加入
Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class
如果你的 Laravel 版本小于 5.5 「如果没记错」的话,请注册服务提供者,否则请忽略
如果你只在开发环境中安装「larave-ide-helper」,那么可以在「app/Providers/AppServiceProvider.php」的「register」方法中写入下面代码:
public function register()
{
if ($this->app->environment() !== 'production') {
$this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
}
// ...
}
导出配置文件(如果默认配置就满足需求了,也可以忽略这一步)
php artisan vendor:publish --provider="Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider" --tag=config
好了,接下去可以愉快的使用了
使用php artisan ide-helper:generate - 为 Facades 生成注释
php artisan ide-helper:models - 为数据模型生成注释
php artisan ide-helper:meta - 生成 PhpStorm Meta file
自动为 Laravel 的 Facades 生成注释
在命令行下运行 php artisan ide-helper:generate
运行上面命令会自动创建ide-helper.php
注:如果存在文件 「bootstrap/compiled.php」 需要先删除, 可以在生成文当前运行 php artisan clear-compiled。
自动为模型生成注释
为所有模型生成注释 php artisan ide-helper:models, 这时会出现询问:
Do you want to overwrite the existing model files? Choose no to write to _ide_helper_models.php instead? (Yes/No): (yes/no) [no]:
输入 yes 则会直接在模型文件中写入注释,否则会生成「_ide_helper_models.php」文件。建议选择 yes,这样在跟踪文件的时候不会跳转到「_ide_helper_models.php」文件,不过这么做最好对模型文件做个备份,至少在生成注释之前用 git 控制一下版本,以防万一。
提示: 为模型生成字段信息必须在数据库中存在相应的数据表,不要生成 migration 还没运行 migrate 的时候就生成注释,这样是得不到字段信息的。