Laravel 门面自动补全工具 laravel-ide-helper

2019-08-27 05:43:13

参考地址 Laravel 门面自动补全工具 laravel-ide-helper

简介

当我们在 PhpStorm 编辑器中,开发 Laravel 框架的项目时,很多类方法都不能自动补全和定位,比如 Facade 门面的方法,DB::table()、Route::get() 等。


laravel-ide-helper 工具可以解决这个问题。


安装和配置

首先,通过 composer 安装 laravel-ide-helper


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

然后,注册服务提供者。有两种注册方式:


在 config/app.php 的 providers 数组中注册服务提供者


Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,

在 app/Providers/AppServiceProvider.php 的 register() 方法中注册。


public function register()

{

    if ($this->app->environment() !== 'production') {

        $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);

    }

    // ...

}

推荐使用第二种方式,因为它只会在非生产环境中加载 Laravel IDE Helper。


自动生成 Laravel 门面对应的 phpDoc

注意:在生成 phpDoc 之前,我们需要先清除 bootstrap/compiled.php 文件。


清除命令是:


php artisan clear-compiled

然后,执行下面的命令生成 phpDoc 文件。


php artisan ide-helper:generate

生成的 phpDoc 就是项目根目录下的 _ide_helper.php 文件,里面包含了 Laravel 门面的相关信息。


为了方便,你可以在 composer.json 文件中作如下配置:


"scripts":{

    ...

    "post-update-cmd": [

        "Illuminate\\Foundation\\ComposerScripts::postUpdate",

        "php artisan ide-helper:generate",

        "php artisan ide-helper:meta"

    ]

},

你还可以发布配置文件,以便修改默认的实现。


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

生成器会尝试去识别真正的类,如果找不到,你可以在配置文件中定义。


有些类需要数据库连接,如果没有相应的数据库连接,某些门面可能无法包含进来。


你可以选择要包含的辅助函数文件,默认该选项并未开启,但是你可以通过 --helpers (-H) 选项进行覆盖。


默认已经引入了 Illuminate/Support/helpers.php ,当然你还可以在配置文件中添加自己的辅助函数文件。


关于 laravel-ide-helper 的更多功能,请参考 https://github.com/barryvdh/laravel-ide-helper



  • 2017-10-16 16:45:45

    Android开发技巧:Application和Instance

    在开发过程中,我们经常会需要用到一些全局的变量或者全局的“管理者”,例如QQ,需要有一个“全局的管理者“保存好友信息,各个activity即可直接通过该”管理者“来获取和修改某个好友信息,显然,这样的一个好友信息,保存到某一个具体的activity里面,然后依靠activity的intent来传递参数是不合适。我们有两种方法来实现这样一个全局的管理者,一种是使用C++/Java中常用的单例模式,另一种是利用Android的Application类,下面一一阐述。

  • 2017-11-01 01:30:45

    解决第三方包内jar包冲突

    这个问题就是因为引入jar包的冲突,这时我们可以在build.gradle中添加如下代码,下方单独的是添加的代码

  • 2017-11-06 01:00:17

    撤销git add

    如何撤销git add,不小心执行了git add . 操作,但是又不能提交所有的文件,因为对应不同的分支,现在怎么样可以将git add 撤销回来

  • 2017-11-10 00:06:15

    CORS: credentials mode is 'include'

    XMLHttpRequest cannot load http://localhost/Foo.API/token. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:5000' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.