LaravalIDE auto-completion assistant: LaravelIDEHelperGenerator

Source: Internet
Author: User
LaravalIDE auto-completion assistant: LaravelIDEHelperGenerator 1. Introduction

This extension package can generate a file that the IDE can understand to provide precise automatic complementing functions. The generated file is based on the files in the project, so it is always real-time. Of course, if you do not want to manually generate this file, you can also use pre-generated files and put them in the project root directory (these files are not so real-time ):

  • Larval 5: https://gist.github.com/barryvdh/5227822
  • Lumen: https://gist.github.com/barryvdh/be17164b0ad51f832f20
  • PHPStorm Meta: https://gist.github.com/barryvdh/bb6ffc5d11e0a75dba67

Note: for details about Laravel 4.x, refer to here.

2. Installation

Run the following Composer command to install the dependencies related to the extension package:

composer require barryvdh/laravel-ide-helper

After the installation is complete, register the service provider in the providers option of app/config. php:

Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,
3. automatically generate the phpDoc corresponding to the Laravel facade

Before using this extension package, many people may have encountered the problem that PHPStorm cannot automatically complete the Facade (Facade). now Laravel IDE Helper Generator brings us the gospel. Run the following command to generate a file containing the facade completion information:

php artisan ide-helper:generate

Note: bootstrap/compiled. php needs to be cleared before Generation. Therefore, run php artisan clear-compiled and then run php artisan optimize before generation.

For later convenience, you can also make the following configuration in the composer. json file:

"scripts":{    "post-update-cmd": [        "php artisan clear-compiled",        "php artisan ide-helper:generate",        "php artisan optimize"    ]},

You can also release a configuration file to modify the default implementation:

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

The generator will try to locate the real class. if it cannot be found, it can be defined in the configuration file.

Some classes require database connection. if there is no corresponding database connection, some facades may not be included.

You can select the auxiliary function file to be included. by default, this option is not enabled, but you can overwrite the default configuration through the -- helpers option. Illuminate/Support/helpers. php will be introduced by default. of course, you can also add custom helper function files in the configuration file.

4. automatically generate the phpDoc corresponding to the model

Before using this feature, install doctrine/dbal :~ 2.3:

composer require doctrine/dbal

If you do not want to write model attributes by yourself, you can use the php artisan ide-helper: models command to generate the corresponding phpDoc based on data table fields, associations, and getters/setters. You can use the-write (-W) option to write model file comments. by default, you need to overwrite or create a separate file _ ide_helper_models.php, you can also use-nowrite (-N) without changing it. Note that you need to back up the model file before performing the operation, because we want to keep the existing, only append new attributes and methods, instead of overwriting and rewriting. If the phpdoc already exists, it will be replaced. Otherwise, the existing phpdoc will be ignored through the-reset (-R) option, and the new field/link will be saved.

php artisan ide-helper:models Post

This command will generate the file _ ide_helper_models.php in the root directory.

/** * An Eloquent Model: 'Post' * * @property integer $id * @property integer $author_id * @property string $title * @property string $text * @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $updated_at * @property-read \User $author * @property-read \Illuminate\Database\Eloquent\Collection|\Comment[] $comments */

By default, models in app/models are traversed. you can use the following method to tell which models are used:

php artisan ide-helper:models Post User

You can also use the -- dir option to browse another directory (based on the root path ):

php artisan ide-helper:models --dir="path/to/models" --dir="app/src/Model"

You can also publish the configuration file (php artisan vendor: publish) and set the default directory.

You can use the-ignore (-I) option to ignore the model:

php artisan ide-helper:models --ignore="Post,User"

You can use the namespace package model name: php artisan ide-helper: models "API \ User"

5. metadata of the container instance in PHPStorm

You can generate a PHPStorm meta file to add factory design mode support. for Laravel, this means that PHPStorm can understand the object types retrieved from the IoC container. For example, events will return the Illuminate \ Events \ Dispatcher object. Therefore, you can use the meta file to call the app ('events') and then it will automatically complete the corresponding scheduling method.

php artisan ide-helper:meta

The. phpstorm. meta. php file is generated in the root directory.

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 nameapp('App\SomeClass');

Pre-generated example: https://gist.github.com/barryvdh/bb6ffc5d11e0a75dba67

Note: You may need to restart PHPStorm to ensure that. phpstorm. meta. php is indexed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.