Laraval IDE Auto complement Assistant: Laravel IDE Helper Generator

Source: Internet
Author: User

1. Introduction

The expansion pack can generate a file that the IDE can understand to provide accurate auto-completion functionality. This generated file is based on the files that exist in the project, so it is always real-time. Of course, if you don't want to generate this file manually, you can also use the pre-generated files and put them in the project root directory (these files are less real-time):

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

Note: Laravel 4.x please refer here.

2. Installation

We use the following composer command to install the extension package dependency:

Composer require Barryvdh/laravel-ide-helper

After the installation is complete we need to register the service provider in the app/config.php providers option:

Barryvdh\laravelidehelper\idehelperserviceprovider::class,

3, automatically generate laravel façade corresponding to the Phpdoc

Before using this expansion pack, perhaps a lot of people have encountered the use of Phpstorm can not automatically fill the façade (facade) problem, now Laravel IDE Helper Generator bring us the gospel. We use the following command to generate a file containing the façade completion information:

PHP Artisan Ide-helper:generate

Note: Before you need to clear bootstrap/compiled.php, you need to run PHP artisan clear-compiled and then run PHP artisan optimize before generating.

For subsequent convenience, you can also configure the following in the Composer.json file:

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

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

PHP artisan vendor:publish--provider= "Barryvdh\laravelidehelper\idehelperserviceprovider"--tag=config

The generator attempts to locate the real class, which can be defined in the configuration file if it is not found.

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

You can select the auxiliary function file to include, which is not turned on by default, but you can override the default configuration with the--helpers option. Illuminate/support/helpers.php is introduced by default, and you can also add custom auxiliary function files to the configuration file.

4, automatic generation model corresponding to the Phpdoc

Before using this feature, you need to first install Doctrine/dbal: ~2.3:

Composer require Doctrine/dbal

If you don't want to write the model properties yourself, you can use the PHP artisan ide-helper:models command to generate the corresponding Phpdoc based on the data table fields, association relationships, and Getters/setters. You can write a comment for the model file by using the-write (-w) option, which by default you need to overwrite or create a separate file _ide_helper_models.php, or you can change it by-nowrite (-N). It is important to note that you need to back up the model files before you proceed, because we want to keep the previous existing, just append the new properties and methods instead of overwriting and overriding. Phpdoc existing will be replaced, otherwise new, through the-reset (-r) option, the existing phpdoc will be ignored, the new field/relationship will be saved.

PHP Artisan ide-helper:models Post

This command generates the file _ide_helper_models.php at the root directory.

/** * An eloquent Model: ' Post ' * * @property integer $id * @property integer $author _id * @property string $title * @prop Erty string $text * @property \carbon\carbon $created _at * @property \carbon\carbon $updated _at * @property-read \user $au Thor * @property-read \illuminate\database\eloquent\collection|\comment[] $comments */

By default, the models in App/models are traversed, and you can tell which models are used in the following ways:

PHP Artisan ide-helper:models Post User

You can also browse through a different directory (based on the root path) with the--dir option:

PHP artisan ide-helper:models--dir= "path/to/models"--dir= "App/src/model"

You can also publish a 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"

Model name can be wrapped by namespace: Php artisan ide-helper:models "Api\user"

5, Phpstorm in the container instance corresponding meta

You can generate a phpstorm meta file to add factory design pattern support, which means, for laravel, that we can let phpstorm understand the type of objects taken out of the IOC container. For example, events returns a Illuminate\events\dispatcher object, so you can invoke the app (' Events ') via the meta file and it will automatically complement the corresponding scheduling method.

PHP Artisan Ide-helper:meta

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

->fire (' events '); \app::make (' Events ')->fire ()/** @var \illuminate\foundation\application $app */$app- >make (' Events ')->fire ()//When the key was not found, it uses the argument as Class Nameapp (' App\someclass ');

Pre-build Example: https://gist.github.com/barryvdh/bb6ffc5d11e0a75dba67

Note: You may need to restart Phpstorm to ensure that the. 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.