Let's talk about the optimization skills of the Laravel 5 Framework of the PHP program.

Source: Internet
Author: User
Tags hhvm

Let's talk about the optimization skills of the Laravel 5 Framework of the PHP program.

Laravel is a powerful framework with many components and huge Code. Its ease of use sacrifices performance. Even so, Laravel is still an excellent framework, however, in the formal environment, we must optimize and speed up website opening.

Next, we will share some development best practices and optimization skills. You are welcome to leave a message to discuss other suggestions.

Here is a simple list:

Configuration Information cache artisan config: cache

Route cache artisan route: cache

Class ing loading optimization artisan optimize

Automatic Loading optimization composer dumpautoload

Use Memcached to store session config/session. php

Use a professional cache drive config/cache. php

Database Request Optimization

Write cache logic for datasets

Use real-time compilers (JIT), such as HHVM and OpCache.

Merge frontend resources with Elixir

1. Configure information Cache

Use the following command to merge all configuration information in the config folder into one file to reduce the number of files loaded during running:

php artisan config:cache

The above command will generate the file bootstrap/cache/config. php. You can use the following command to cancel the configuration information cache:

php artisan config:clear

This command deletes the bootstrap/cache/config. php file.

Note:The configuration information cache will not be automatically reloaded with updates. Therefore, we recommend that you disable the configuration information Cache during development. It is generally used in the production environment and can be used with the Envoy task runner.

2. Route Cache

The routing cache can effectively improve the efficiency of vro registration. In large applications, the effect becomes more obvious. You can use the following command:

php artisan route:cache

The preceding commands generate the bootstrap/cache/routes. php file. Note that the routing cache does not support the logic of writing anonymous functions.

Run the following command to clear the route cache:

php artisan route:clear

This command deletes the bootstrap/cache/routes. php file.

Note: The routing cache will not be automatically reloaded with updates. Therefore, we recommend that you disable the routing Cache during development. It is generally used in the production environment and can be used with the Envoy task runner.

3. Class ing loading Optimization

The optimize command combines frequently-used loaded classes into a file to improve the running efficiency by reducing file loading:

php artisan optimize --force

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

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 automatically generated.

To clear the class ing loading optimization, run the following command:

php artisan clear-compiled

This command will delete the two files generated by optimize.

Note:This command runs after php artisan config: cache, because the optimize command generates files based on the configuration information (such as the providers array of the config/app. php file.

4. Automatic Loading Optimization

This command is not only for Laravel programs, but also for all programs built using composer. This command converts the PSR-0 and PSR-4 to a class ing table to increase the loading speed of the class.

composer dumpautoload -o

Note: this operation has been performed in the php artisan optimize -- force command.

5. Use Memcached to store sessions

Each Laravel request generates a session. Modifying the session storage method can effectively improve the program efficiency. The session configuration information is config/session. php, it is recommended to change to professional cache software such as Memcached or Redis:

'driver' => 'memcached',

6. Use a professional cache drive

"Cache" is a magic weapon to improve the efficiency of application programs. The default cache driver is file Cache. We recommend that you switch to a professional cache system, such as Redis or Memcached. We do not recommend you use database cache.

'default' => 'redis',

7. database Request Optimization

Delayed pre-loading and pre-loading are used for data association model reading;
UseLaravel DebugbarOrClockworkPay attention to the total number of database requests on each page;
The length here is only related to Laravel. For more information about data optimization, see other documents.

8. Write cache logic for the dataset

Use the cache layer operations provided by Laravel reasonably to cache the data sets taken out from the database to reduce the pressure on the database, the professional cache software running on the memory reads data far faster than the database.

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

Remember and even the data association model are also cached. How convenient is it.

9. Use the instant Compiler

Both HHVM and OpCache can easily improve the performance of your applications by 50% or higher without any modifications. To learn more about this experiment, see: use OpCache to Improve the Performance of PHP 5.5 + programs.

10. frontend resource Merging

As a standard for optimization, only one CSS and one JS file should be loaded on a page, and the file must be easily accessible through CDN, and the file name must change with modification.

The above are ten optimization suggestions for the Laravel 5 Framework of the PHP program, for your reference.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.