Performance has always been a point of Laravel framework, so tuning the Laravel program is a must-learn skill.
Next share some of the best practices for development, as well as tuning tips, and you are welcome to comment on other suggestions.
Here is a simple list:
Configuration information Cache artisan Config:cache route cache Artisan Route:cache class-Map load optimization artisan optimize auto-load optimization composer Dumpautoload use Memcached To store the session config/session.php use the Pro cache drive config/cache.php database request optimization for the dataset writing cache logic using the instant compiler (JIT), such as: HHVM, Opcache front-end resource merging Elixir
1. Configure the information cache use the following Artisan to bring all the configuration information in the Config folder into one file, reducing the number of files loaded at runtime:
PHP artisan config:cache The above command generates a file bootstrap/cache/config.php, you can use the following command to cancel the configuration information cache:
PHP artisan config:clear This command to do is to delete the bootstrap/cache/config.php file.
Note: The configuration information cache is not automatically overloaded with updates, so it is recommended that you turn off the configuration information cache during development, which is typically used in a production environment and can be used with the Envoy task runner. 2. Route cache routing cache can effectively improve the efficiency of the router registration, in large applications, the more obvious effect, you can use the following command:
PHP artisan route:cache The above command generates bootstrap/cache/routes.php files, it should be noted that the routing cache does not support routing anonymous function authoring logic, see: Document-route cache.
You can use the following command to clear the route cache:
PHP artisan route:clear This command to do is to delete the bootstrap/cache/routes.php file.
Note: Routing caches are not automatically overloaded with updates, so it is advisable to turn off the route cache during development, which is typically used in production environments and can be used with the Envoy task runner.
3. Class-Map load optimization The optimize command merges commonly loaded classes into a single file, improving operational efficiency by reducing file loading:
PHP Artisan optimize--force generates bootstrap/cache/compiled.php and Bootstrap/cache/services.json two files.
You can add classes to be merged by modifying the config/compile.php file.
In the production environment, the parameter--force does not need to be specified, and the file is generated automatically.
To clear the class-map load optimization, run the following command:
PHP artisan clear-compiled This command deletes the two files generated above optimize.
Note: This command runs after PHP artisan config:cache because the optimize command generates files based on configuration information such as providers array of config/app.php files. 4. Automatic load Optimization This command is not only for Laravel programs, but for all programs built using composer. This command converts PSR-0 and PSR-4 to a class-map table to increase the load speed of the class.
Composer Dumpautoload-o Note: This is already done in PHP artisan optimize--force command. 5. Using Memcached to store each Laravel request, the session will be generated, modify the storage of the session can effectively improve the efficiency of the program, the session configuration information is config/session.php, it is recommended to change to Memcached or Redis and other professional caching software:
' Driver ' = ' memcached ', 6. Using Professional cache Drive "Cache" is one of the magic weapons to improve the efficiency of the application, the default cache driver is the file cache, it is recommended to switch to a professional caching system, such as Redis or Memcached, and it is not recommended to use the database cache.
' Default ' = ' Redis ', 7. Database request optimization Database request optimization
The Data association model reads with lazy preload and preload; use Laravel Debugbar or clockwork to pay attention to the total number of database requests per page; The space here is only related to Laravel, other information about data optimization, please consult other materials on your own.
8. For the data set writing cache logic reasonable use of Laravel provides cache layer operation, the data collection from the database to cache, reduce the pressure on the database, running in-memory professional cache software to read data is much faster than the database.
$posts = Cache::remember (' index.posts ', $minutes = +, function () {return Post::with (' comments ', ' tags ', ' author ', ' SEO ')->wherehidden (0)->get ();}); Remember even the data correlation model has been cached, how convenient it is.
9. Using the instant compiler HHVM and Opcache can easily let your application without any modification, directly improve the performance of 50% or higher, phphub before doing an experiment, see: Using Opcache to improve the performance of PHP 5.5+ program.
10. Front-end resource consolidation as the standard of optimization, a page should only load a CSS and a JS file, and the file to facilitate the CDN, need to change the file name with the changes.
Laravel Elixir offers a simple and practical solution, see documentation for details: Laravel Elixir documentation.
10 Tips for optimizing LARAVEL5 framework Programs