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 被索引。

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
