Phpstorm 開啟 Laravel 程式碼提示

北行發表於2019-10-21

今天在寫專案的時候,感覺沒有程式碼提示特別不爽,而且也容易出錯,耗時耗力。遂找了一波關於laravel程式碼提示及補全工具,發現網上的文章都是通過barryvdh/laravel-ide-helper這個包生成的,我就自己配置了一下 ,感覺舒服了不少。但是還是有一個問題,就是每個專案裡都得引用一下這個包,然後通過php artisan去生成提示檔案,雖然可以通過.gitignore去讓提交的時候忽略掉提示檔案,但是每個專案都要這麼走一遭,感覺比較雞肋,所以就研究了一下Phpstorm的ide配置方法,發現可行,遂分享給大家。

➜  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

'providers' => [
        /*
         * Laravel Framework Service 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,
        /*
         * Package Service Providers...
         */
        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        ...
        Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class
    ],

php artisan vendor:publish 

➜  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

➜  test_demo php artisan ide-helper:generate
A new helper file was written to _ide_helper.php

此時生成的_ide_helper.php在專案根目錄下面

➜  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生成完成

_ide_helper.php找一個地方放著

➜  ~ mkdir PhpstormHelper
➜  ~ mv test/test_demo/_ide_helper.php PhpstormHelper/laravel_ide_helper.php



修改後

重點

model提示需要在class上方加註釋,如下

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

/**
 * App\User
 *
 * @mixin \Eloquent
 */
class User extends Authenticatable
{
}

註冊的別名函式不用加,可以直接使用

Phpstorm開啟Laravel程式碼提示

原文出處 https://shiwenyuan.github.io/post/ck20cx3v...

原創不易,轉載請註明出處

相關文章