Home PHP Framework Laravel Learn 10 tips to optimize PHP program Laravel 5 framework in one minute

Learn 10 tips to optimize PHP program Laravel 5 framework in one minute

Jun 18, 2021 am 09:23 AM
php

This article introduces you to learn 10 techniques in one minute to optimize the Laravel 5 framework of PHP programs. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Learn 10 tips to optimize PHP program Laravel 5 framework in one minutePerformance has always been a point of criticism for the Laravel framework, so tuning Laravel programs is a must-learn skill.
Here is a simple list:

Configuration information cache artisan config:cache

               路由缓存 artisan route:cache
               类映射加载优化 artisan optimize
               自动加载优化 composer dumpautoload
               使用 Memcached 来存储会话 config/session.PHP
               使用专业缓存驱动器 config/cache.php
               数据库请求优化
               数据集书写缓存逻辑
               使用即时编译器(JIT),如:HHVM、OpCache
               前端资源合并 Elixir
1. 配置信息缓存
Use the following Artisan built-in command to merge all the configuration information in the config folder into one file to reduce the number of files loaded during runtime:
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
What this command does is delete the bootstrap/cache/config.php file.
Note: The configuration information cache will not be automatically reloaded with updates. Therefore, it is recommended to turn off the configuration information cache during development. It is generally used in production environments and can be used with the Envoy task runner.
2. Route cache
Route cache can effectively improve the registration efficiency of the router, and the effect is more obvious in large applications. Use the following command:
##

php artisan route:cache
Copy after login

The above command will generate the bootstrap/cache/routes.php file , it should be noted that the route cache does not support routing anonymous function writing logic.
You can use the following command to clear the routing cache:

php artisan route:clear
Copy after login

此命令做的事情就是把 bootstrap/cache/routes.php 文件删除。
注意:路由缓存不会随着更新而自动重载,所以,开发时候建议关闭路由缓存,一般在生产环境中使用,可以配合 Envoy 任务运行器 一起使用。
3. 类映射加载优化
optimize 命令把常用加载的类合并到一个文件里,通过减少文件的加载,来提高运行效率:

php artisan optimize --force
Copy after login

会生成 bootstrap/cache/compiled.php 和 bootstrap/cache/services.json 两个文件。
你可以可以通过修改 config/compile.php 文件来添加要合并的类。
在 production 环境中,参数 --force 不需要指定,文件就会自动生成。
要清除类映射加载优化,请运行以下命令:

php artisan clear-compiled
Copy after login

此命令会删除上面 optimize 生成的两个文件。
注意:此命令要运行在 php artisan config:cache 后,因为 optimize 命令是根据配置信息(如:config/app.php 文件的 providers 数组)来生成文件的。
4. 自动加载优化
此命令不止针对于 Laravel 程序,适用于所有使用 composer 来构建的程序。此命令会把 PSR-0 和 PSR-4 转换为一个类映射表,来提高类的加载速度。

composer dumpautoload -o
Copy after login

注意:php artisan optimize --force 命令里已经做了这个操作。
5. 使用 Memcached 来存储会话
每一个 Laravel 的请求,都会产生会话,修改会话的存储方式能有效提高程序效率,会话的配置信息是 config/session.php,建议修改为 Memcached 或者 Redis 等专业的缓存软件:

'driver' => 'memcached',
Copy after login

6. 使用专业缓存驱动器
「缓存」是提高应用程序运行效率的法宝之一,默认缓存驱动是 file 文件缓存,建议切换到专业的缓存系统,如 redis 或者 Memcached,不建议使用数据库缓存。

'default' => 'redis',
Copy after login

7. 数据库请求优化
数据关联模型读取时使用 延迟预加载 和 预加载 ;
使用 Laravel Debugbar 或者 Clockwork 留意每一个页面的总数据库请求数量;
这里的篇幅只写到与 Laravel 相关的,其他关于数据优化的内容,请自行查阅其他资料。
8. 为数据集书写缓存逻辑
合理的使用 Laravel 提供的缓存层操作,把从数据库里面拿出来的数据集合进行缓存,减少数据库的压力,运行在内存上的专业缓存软件对数据的读取也远远快于数据库。

$posts = Cache::remember('index.posts', $minutes = 30, function()
{
    return Post::with('comments', 'tags', 'author', 'seo')->whereHidden(0)->get();
});
remember 甚至连数据关联模型也都一并缓存了,多么方便呀。
Copy after login

9. 使用即时编译器
HHVM 和 OpCache 都能轻轻松松的让你的应用程序在不用做任何修改的情况下,直接提高 50% 或者更高的性能,PHPhub 之前做个一个实验,具体请见:使用 OpCache 提升 PHP 5.5+ 程序性能。
10. 前端资源合并
作为优化的标准,一个页面只应该加载一个 CSS 和 一个 js 文件,并且文件要能方便走 CDN,需要文件名随着修改而变化

相关推荐:最新的五个Laravel视频教程


The above is the detailed content of Learn 10 tips to optimize PHP program Laravel 5 framework in one minute. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles