前言
今天在写项目的时候,感觉没有代码提示特别不爽,而且也容易出错,耗时耗力。遂找了一波关于laravel代码提示及补全工具,发现网上的文章都是通过barryvdh/laravel-ide-helper这个包生成的,我就自己配置了一下 ,感觉舒服了不少。但是还是有一个问题,就是每个项目里都得引用一下这个包,然后通过php artisan
去生成提示文件,虽然可以通过.gitignore
去让提交的时候忽略掉提示文件,但是每个项目都要这么走一遭,感觉比较鸡肋,所以就研究了一下Phpstorm
的ide配置方法,发现可行,遂分享给大家。
生成ide_helper文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| ➜ test_demo composer require barryvdh/laravel-ide-helper
Using version ^2.6 for barryvdh/laravel-ide-helper ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 14 installs, 0 updates, 0 removals - Installing doctrine/event-manager (v1.0.0): Downloading (100%) - Installing doctrine/cache (v1.8.0): Downloading (100%) - Installing doctrine/dbal (v2.9.2): Downloading (100%) - Installing composer/xdebug-handler (1.3.3): Downloading (100%) - Installing composer/spdx-licenses (1.5.2): Downloading (100%) - Installing symfony/filesystem (v4.3.5): Downloading (100%) - Installing justinrainbow/json-schema (5.2.9): Downloading (100%) - Installing seld/phar-utils (1.0.1): Downloading (100%) - Installing seld/jsonlint (1.7.1): Downloading (100%) - Installing composer/semver (1.5.0): Downloading (100%) - Installing composer/ca-bundle (1.2.4): Downloading (100%) - Installing composer/composer (1.9.0): Downloading (100%) - Installing barryvdh/reflection-docblock (v2.0.6): Downloading (100%) - Installing barryvdh/laravel-ide-helper (v2.6.5): Downloading (100%) doctrine/cache suggests installing alcaeus/mongo-php-adapter (Required to use legacy MongoDB driver) barryvdh/reflection-docblock suggests installing dflydev/markdown (~1.0) Writing lock file Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: barryvdh/laravel-ide-helper Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully.
|
修改config/app.php
中的providers
增加Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| 'providers' => [
Illuminate\Auth\AuthServiceProvider::class, Illuminate\Broadcasting\BroadcastServiceProvider::class, Illuminate\Bus\BusServiceProvider::class, Illuminate\Cache\CacheServiceProvider::class, Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class, Illuminate\Cookie\CookieServiceProvider::class, Illuminate\Database\DatabaseServiceProvider::class, Illuminate\Encryption\EncryptionServiceProvider::class, Illuminate\Filesystem\FilesystemServiceProvider::class, Illuminate\Foundation\Providers\FoundationServiceProvider::class, Illuminate\Hashing\HashServiceProvider::class, Illuminate\Mail\MailServiceProvider::class, Illuminate\Notifications\NotificationServiceProvider::class, Illuminate\Pagination\PaginationServiceProvider::class, Illuminate\Pipeline\PipelineServiceProvider::class, Illuminate\Queue\QueueServiceProvider::class, Illuminate\Redis\RedisServiceProvider::class, Illuminate\Auth\Passwords\PasswordResetServiceProvider::class, Illuminate\Session\SessionServiceProvider::class, Illuminate\Translation\TranslationServiceProvider::class, Illuminate\Validation\ValidationServiceProvider::class, Illuminate\View\ViewServiceProvider::class,
App\Providers\AppServiceProvider::class, ... Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class ],
|
php artisan vendor:publish
1 2 3
| ➜ test_demo php artisan vendor:publish --provider="Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider" --tag=config Copied File [/vendor/barryvdh/laravel-ide-helper/config/ide-helper.php] To [/config/ide-helper.php] Publishing complete.
|
php artisan ide-helper:generate
1 2
| ➜ test_demo php artisan ide-helper:generate A new helper file was written to _ide_helper.php
|
此时生成的_ide_helper.php
在项目根目录下面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| ➜ test_demo tree -L 1 . ├── _ide_helper.php ├── app ├── artisan ├── bootstrap ├── composer.json ├── composer.lock ├── config ├── database ├── package.json ├── phpunit.xml ├── public ├── resources ├── routes ├── server.php ├── storage ├── tests ├── vendor ├── webpack.mix.js └── yarn.lock
|
至此,_ide_helper.php
生成完成
PhpStorm集成_ide_helper.php
_ide_helper.php
找一个地方放着
1 2
| ➜ ~ mkdir PhpstormHelper ➜ ~ mv test/test_demo/_ide_helper.php PhpstormHelper/laravel_ide_helper.php
|
修改后
重点
model提示需要在class上方加注释,如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable;
class User extends Authenticatable { }
|
注册的别名函数不用加,可以直接使用