Laraval IDE 自动补全助手:Laravel IDE Helper Generator
1、简介
该扩展包可以生成一个IDE可以理解的文件,以便提供精准的自动补全功能。这个生成的文件基于项目中存在的文件,所以永远是实时的。当然如果你不想手动生成这个文件,也可以使用预生成的文件并把它们放到项目根目录下(这些文件就没有那么实时了):
- Larval 5: https://gist.github.com/barryvdh/5227822
- Lumen: https://gist.github.com/barryvdh/be17164b0ad51f832f20
- PHPStorm Meta 文件: https://gist.github.com/barryvdh/bb6ffc5d11e0a75dba67
注:Laravel 4.x请参考 这里 。
2、安装
我们使用如下Composer命令安装扩展包相关依赖:
composer require barryvdh/laravel-ide-helper
安装完成后我们需要到app/config.php的providers选项中注册服务提供者:
Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,
3、自动生成Laravel门面对应的phpDoc
在使用这个扩展包之前,或许不少人已经碰到了使用PHPStorm不能自动补全门面(Facade)的问题,现在Laravel IDE Helper Generator 为我们带来了福音。我们使用如下命令生成包含门面补全信息的文件:
php artisan ide-helper:generate
注意:在此之前需要清除 bootstrap/compiled.php ,所以生成之前需要先运行 php artisan clear-compiled 然后运行 php artisan optimize 。
为了后续方便,你也可以在 composer.json 文件中作如下配置:
"scripts":{ "post-update-cmd": [ "php artisan clear-compiled", "php artisan ide-helper:generate", "php artisan optimize" ]},
还可以发布配置文件以修改默认实现:
php artisan vendor:publish --provider="Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider" --tag=config
生成器会尝试定位真正的类,如果找不到,可以在配置文件中定义。
有些类需要数据库连接,如果没有相应的数据库连接,某些门面可能无法包含进来。
你可以选择要包含的辅助函数文件,默认该选项并未开启,但是你可以通过 --helpers 选项覆盖默认配置。默认会引入 Illuminate/Support/helpers.php ,当然你还可以在配置文件中添加自定义的辅助函数文件。
4、自动生成模型对应的phpDoc
在使用本特性之前,需要先安装 doctrine/dbal: ~2.3 :
composer require doctrine/dbal
如果你不想自己编写模型属性,可以使用 php artisan ide-helper:models 命令来基于数据表字段、关联关系以及getters/setters生成对应的phpDoc。你可以通过 —write(-W) 选项来编写模型文件的注释,默认情况下,你需要覆盖或新建一个单独的文件 _ide_helper_models.php ,你也可以通过 —nowrite(-N) 不做更改。需要注意的是在进行操作之前需要备份模型文件,因为我们要保留之前已存在的,只是追加新属性和方法,而不是覆盖和重写。phpdoc已存在会被替换,否则新增,通过 —reset(-R) 选项,已存在的phpdoc会被忽略,新增的字段/关系才会被保存。
php artisan ide-helper:models Post
该命令会根目录下生成文件 _ide_helper_models.php 。
/** * 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 */
默认情况下,app/models中的模型会被遍历,可以使用如下方式告诉哪些模型被使用:
php artisan ide-helper:models Post User
还可以通过 --dir 选项浏览一个其他目录(基于根路径):
php artisan ide-helper:models --dir="path/to/models" --dir="app/src/Model"
也可以发布配置文件( php artisan vendor:publish )并设置默认目录。
可以使用 —ignore(-I) 选项来忽略模型:
php artisan ide-helper:models --ignore="Post,User"
可以通过命名空间包裹模型名称: php artisan ide-helper:models "API\User"
5、PHPStorm中容器实例对应的Meta
可以生成一个PHPStorm meta文件来添加工厂设计模式支持,对Laravel而言,这意味着我们可以让PHPStorm理解从IoC容器中取出的对象类型。例如, events 会返回 Illuminate\Events\Dispatcher 对象,因此通过meta文件你可以调用 app('events') 然后它会自动补全对应的调度方法。
php artisan ide-helper:meta
会在根目录下生成 .phpstorm.meta.php 文件。
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');
预生成示例: https://gist.github.com/barryvdh/bb6ffc5d11e0a75dba67
注意:你可能需要重启PHPStorm以确保 .phpstorm.meta.php 被索引。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
