Cài đặt Laravel5 IDE Helper Generator

Cài đặt Laravel5 IDE Helper Generator

Package : https://github.com/barryvdh/laravel-ide-helper

Cài đặt

1. Require package

composer require barryvdh/laravel-ide-helper

2. Add service provider vào providers array trong config/app.php

Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,

3. phpDoc generate cho Laravel Facades

php artisan ide-helper:generate

Bạn có thể gặp lỗi dưới:

Error generating IDE Helper: first delete your compiled file (php artisan clear-compiled)

Lý do là bạn cần clear file bootstrap/complied.php trước, vì vậy cần chạy lệnh

php artisan clear-compiled

trước khi chạy lệnh php artisan ide-helper:generate

Sau khi chạy lệnh generate ide-helper xong bạn sẽ thấy file _ide_helper.php được sinh ra, và ide của bạn có thể autocomplete được như hình dưới:
autocomplete

4.Tự động cập nhật IDE helper code

Trong file composer.json, bạn sửa đoạn script post-update-cmd như bên dưới. Mục đích là để sau khi cài đặt thêm các package, ide-helper sẽ được generate ứng với thay đổi của project.

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

5. Tự động sinh Phpdoc cho Model

Bạn cần cài thêm package composer require doctrine/dbal vào project để get thông tin từ database

composer require doctrine/dbal

sau đó có thể generate bằng lệnh:

php artisan ide-helper:models

Mặc định package sẽ hỏi bạn có ghi đè vào file Model hay ghi ra 1 file riêng có tên là _ide_helper_models.php trong project ?

Do you want to overwrite the existing model files? Choose no to write to _ide_helper_models.php instead? (Yes/No): (yes/no) [no]:

Trong trường hợp bạn chọn yes, các file Model sẽ được ghi thêm Phpdoc comment:

Phpdoc comment in model

và khi code IDE của bạn có thể gợi ý các properties của Model như hình dưới:

autocomplete model properties