安装
安装命令:
1 | # 如果只想在开发环境安装请加上 --dev |
在 「config/app.php」的 「providers」数组中加入
1 | Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class, |
如果你的 Laravel 版本小于 5.5 的话,请注册服务提供者,否则请忽略
如果你只在开发环境中安装「larave-ide-helper」,那么可以在「app/Providers/AppServiceProvider.php」的「register」方法中写入下面代码:
1 | public function register() |
导出配置文件,即:config/ide-helper.php(如果默认配置就满足需求了,也可以忽略这一步)
1 | php artisan vendor:publish --provider="Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider" --tag=config |
好了,接下去可以愉快的使用了
自动为 Laravel 的 Facades 生成注释
在命令行下运行
1 | php artisan ide-helper:generate |
注: 如果存在文件 「bootstrap/compiled.php」 需要先删除, 可以在生成文当前运行 php artisan clear-compiled。
自动为模型生成注释
多个模型:直接
php artisan ide-helper:models
,当然也可以php artisan ide-helper:models -W
单个模型:这个不能按照 github 上的 readme 来,坑爹。必须指明具体的类全名:
php artisan ide-helper:models "App\Http\Models\User"
。
为所有模型生成注释 php artisan ide-helper:models
, 这时会出现询问:
1 | 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 的时候就生成注释,这样是得不到字段信息的。
自动为链式操作注释
这是什么意思呢?举个例子,在 migration 文件中经常可以看见这样的代码:
1 | $table->string('email')->unique(); |
这时候就算调用过了 php artisan ide-helper:generate
,在调用像 ->unique() 这样的链式操作的时候也无法实现代码提示,这时候需要将配置文件「如果导出的话」'include_fluent' => false
修改为 'include_fluent' => true
,重新运行 php artisan ide-helper:generate
。试试效果吧!
生成 .phpStorm.meta.php
可以生成一个PhpStorm meta 文件去支持工厂模式. 对于 Laravel, 这意味着我们可以让 PhpStorm 理解我们从 IoC 容器中解决了什么类型的对象。例如:事件将返回一个「Illuminate\Events\Dispatcher」对象,利用 meta 文件您可以调用 app(‘events’) 并且它将自动完成 Dispatcher 的方法。
1 | app('events')->fire(); |
提示:您可能需要重启 Phpstorm 使 .phpStorm.meta.php 文件生效。
自动运行 generate
想在依赖包更新是自动更新注释,可以在 composer.json 文件中做如下配置:
1 | "scripts":{ |
提示:如果只在 dev 环境下部署 ide helper 还是不要这么做了,防止在生产环境中报错导致不必要的麻烦。