為PhpStorm添加Laravel 代碼智能提示功能


php artisan clear-compiled    //清除bootstrap/compiled.php
php artisan ide-helper:generate    //為 Facades 生成注釋,需要先清除bootstrap/compiled.php
php artisan ide-helper:models    //為模型生成注釋
php artisan ide-helper:meta    //生成 .phpStorm.meta.php

一、為PHPSTORM安裝Laravel Plugin插件

二、應用 composer 安裝 barryvdh/laravel-ide-helperdoctrine/dbal

packagist官網地址https://packagist.org/

使用如下命令安裝barryvdh/laravel-ide-helper

composer require --dev barryvdh/laravel-ide-helper

使用如下命令安裝doctrine/dbal「請裝上它,在為模型注釋字段的時候必須用到它」

composer require "doctrine/dbal: ~2.5"

三、允許應用程序在非生產環境中加載Laravel IDE Helper

app/Providers/AppServiceProvider.php文件中的register()方法中添加下面的代碼:

//允許應用程序在非生產環境中加載Laravel IDE Helper
if ($this->app->environment() !== 'production') {
    $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
}

四、使用publish命令將軟件包配置復制到本地配置:

php artisan vendor:publish --provider="Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider" --tag=config

修改配置文件ide-helper.php自動為鏈式操作注釋):

'include_fluent' => true,

五、為 Facades 生成注釋

必須首先清除bootstrap/compiled.php,運行以下命令進行清除:

php artisan clear-compiled

為 Facades 生成注釋:

php artisan ide-helper:generate

六、為模型生成注釋

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 的時候就生成注釋,這樣是得不到字段信息的。

七、生成 .phpStorm.meta.php

php artisan ide-helper:meta

可以生成一個PhpStorm meta 文件去支持工廠模式. 對於 Laravel, 這意味着我們可以讓 PhpStorm 理解我們從 IoC 容器中解決了什么類型的對象。例如:事件將返回一個「Illuminate\Events\Dispatcher」對象,利用 meta 文件您可以調用 app('events') 並且它將自動完成 Dispatcher 的方法。

app('events')->fire();
\App::make('events')->fire();

/** @var \Illuminate\Foundation\Application $app */
$app->make('events')->fire();

// When the key is not found, it uses the argument as class name
app('App\SomeClass');

提示:如果 .phpStorm.meta.php 文件不生效的話,則可能需要重啟PHPSTORM。

八、自動運行 generate

想在依賴包更新時自動更新注釋,可以在 composer.json 文件中做如下配置:

scripts標簽中添加下面的代碼:

"post-update-cmd": [
    "Illuminate\\Foundation\\ComposerScripts::postUpdate",
    "php artisan ide-helper:generate",
    "php artisan ide-helper:meta"
]

 其他操作相關文檔:

https://packagist.org/packages/barryvdh/laravel-ide-helper

https://laravel-china.org/articles/10172/laravel-super-good-code-prompt-tool-laravel-ide-helper


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM