Performance has always been one of the biggest topics in Laravel, but it has certainly provided a lot of different knowledge for many PHP developers, and here’s another acceleration for the framework.

The basics (a cliche)

  • Caches, sessions, queues are all usedRedisdrive
  • Built-in caching for the framework

    • The routing cachephp artisan route:cache
    • Configure the cachephp artisan config:cache
  • Use the cache based on the idempotency of the interface

upgrade

  • OPcache

    • No particular reason to go straight to this
    • And theopcache.validate_timestampsSet to0, which allows you to produce environmentsPHPCode is never automatically updated, and like other compiled languages, every time code is deployed, it needs to be restartedphp-fpmTo load the new code
    • More this article https://gywbd.github.io/posts/2016/1/best-config-for-zend-opcache.html for reference

    Reduce unnecessary middleware

  • Such asLaravelCross-domain middleware is now built in, if only forAPPBy providing interfaces, there is no cross-domain problem at all, and the middleware can be annotated directly
  • Like the built-inAPIRate-limiting interfaces may not be appropriate for many project scenarios

Decrease service providers

  • For example, interfaces don’t need view services,SessionServices, password reset services, etc

Projects that mix API and Admin use a full-stack framework. There are also projects that use laravel-admin or Dcat Admin

I also wrote about how to reduce your number of service providers, since since Laravel5.5, any third party packages that need to be registered can be registered on their own. Laravel then automatically discovers these service providers and runs this command to find out which service providers you have registered:

php artisan package:discover

Discovered Package: dcat/laravel-admin
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
  • You can see it clearly heredcat/laravel-admin, we just need to go to the project root directorycomposer.jsonWrite the following configuration
 "extra": {
     "laravel": {
         "dont-discover": [
             "dcat/laravel-admin"
         ]
     }
 }
  • When you have done so, executephp artisan package:discoverYou will finddcat/laravel-admindisappeared
  • But that also means we can’t use itAdminSo we also need to add a condition to manually register the service provider
  • We can do thatAppServiceProvider.phpAdd the following code
<? php namespace App\Providers; use Dcat\Admin\Admin; use Dcat\Admin\AdminServiceProvider; use Illuminate\Foundation\AliasLoader; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { // } /** * Bootstrap any application services. * * @return void */ public function boot() { // If (config('admin.enable')) {aliasLoader ::getInstance()->alias(' admin ', admin ::class); $this->app->register(AdminServiceProvider::class); }}}
  • After the modification, you find that the performance is good40%Because ofAdminRegistered a lot of routes, started a lot of things)

    Machine:

    CentOS Linux Release 8.3.2011 for 2u4G machines

    Environment: (UseLaradockFamily bucket, openOPcache)
7.4 PHP_FPM_INSTALL_OPCACHE PHP_VERSION = = true
  • Finally, I asked for one of themAPIDiagram of server



    As you can see, it looks pretty good.

Links

https://www.shiguopeng.cn/archives/374

https://www.shiguopeng.cn/archives/507