Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the optimization skills of PHP program Laravel 5 framework?

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "what are the PHP program Laravel 5 framework optimization skills", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what are the PHP program Laravel 5 framework optimization skills"!

Here is a simple list:

Configuration information cache artisan config:cache

Routing cache artisan route:cache

Class mapping loading optimization artisan optimize

Automatically load optimized composer dumpautoload

Use Memcached to store session config/session.php

Use professional cache drive config/cache.php

Database request optimization

Write cache logic for a dataset

Use just-in-time compilers (JIT), such as HHVM, OpCache

Front-end resources merge Elixir

1. Configure information cache

Use the following Artisan native command to merge all the configuration information in the config folder into one file to reduce the number of files loaded at run time:

Php artisan config:cache

The above command generates the file bootstrap/cache/config.php, and you can use the following command to unconfigure the information cache:

Php artisan config:clear

What this command does is delete the bootstrap/cache/config.php file.

Note: the configuration information cache is not automatically overloaded with updates, so it is recommended to turn off the configuration information cache during development. It is generally used in a production environment and can be used with the Envoy task runner.

two。 Routing cach

Route cache can effectively improve the registration efficiency of the router, and the effect is more obvious in large applications, you can use the following command:

Php artisan route:cache

The above command generates a bootstrap/cache/routes.php file, but it should be noted that the route cache does not support routing anonymous function writing logic.

You can clear the route cache using the following command:

Php artisan route:clear

What this command does is delete the bootstrap/cache/routes.php file.

Note: the route cache is not automatically reloaded with updates, so it is recommended to turn off the route cache during development. It is generally used in a production environment and can be used with the Envoy task runner.

3. Class mapping loading optimization

The optimize command merges commonly loaded classes into one file to improve efficiency by reducing the loading of files:

Php artisan optimize-force

Two files bootstrap/cache/compiled.php and bootstrap/cache/services.json are generated.

You can add classes to merge 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 mapping load optimization, run the following command:

Php artisan clear-compiled

This command deletes the two files generated by optimize above.

Note: this command should be run after php artisan config:cache, because the optimize command generates files based on configuration information, such as the providers array of config/app.php files.

4. Automatic loading optimization

This command applies not only to Laravel programs, but to all programs built using composer. This command converts PSR-0 and PSR-4 into a class mapping table to improve class loading speed.

Composer dumpautoload-o

Note: this is already done in the php artisan optimize-- force command.

5. Use Memcached to store the session

Every Laravel request will generate a session. Modifying the storage method of the session can effectively improve the efficiency of the program. The configuration information of the session is config/session.php. It is recommended to modify it to professional caching software such as Memcached or Redis:

'driver' = >' memcached'

6. Use a professional cache drive

"caching" is one of the magic weapons to improve the efficiency of the application. The default cache driver is file file cache. It is recommended to switch to a professional cache system, such as Redis or Memcached. Database cache is not recommended.

'default' = >' redis'

7. Database request optimization

Use delayed preloading and preloading when reading data association model

Use Laravel Debugbar or Clockwork to note the total number of database requests per page

The space here is only related to Laravel, other content about data optimization, please consult other materials by yourself.

8. Write cache logic for a dataset

Reasonably use the cache layer operation provided by Laravel to cache the data set taken from the database to reduce the pressure on the database, and the professional cache software running on memory can read the data much faster than the database.

$posts = Cache::remember ('index.posts', $minutes = 30, function () {return Post::with (' comments', 'tags',' author', 'seo')-> whereHidden (0)-> get ();})

Remember even caches the data association model, how convenient it is.

9. Use just-in-time compiler

Both HHVM and OpCache can easily improve the performance of your application by 50% or more without any modification. PHPhub did an experiment before, see: use OpCache to improve the performance of PHP 5.5 + programs.

10. Front-end resource consolidation

As a standard of optimization, a page should load only one CSS and one JS file, and for the file to be convenient for CDN, the file name needs to change as it is modified.

At this point, I believe you have a deeper understanding of "what are the optimization skills of PHP program Laravel 5 framework". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report