Home>Article>PHP Framework> How to increase the speed of your Laravel application
How to increase the speed of Laravel application? The following article will introduce to you how to optimize Laravel's operating efficiency. I hope it will be helpful to you!
#Software development is accelerating as the pace of technology-driven companies continues to increase. Many new technology stacks are helping us streamline the development process, and many of these new companies are using PHP as a backend framework for their applications. PHP and its various version updates have become popular among developers. Most PHP developers have at least heard of using Laravel. Laravel is best known for developing business-focused applications and providing security to the applications. The most important thing that attracts most developers is that it allows them to make small changes to improve website optimization.
In this article, we will discuss how to increase the speed of your Laravel application. So let's get started.
#Some of the suggestions below may not improve the speed of your specific application, but you can Try all the suggestions and through trial and error you'll see what works best for you. Let’s discuss them one by one.
Laravel performance monitoring tool helps to improve application performance using metrics and error reporting. You can use these statistics to predict user behavior. There are many tools available in the market for monitoring Laravel applications.
Scout APM helps you get these metrics easily in a single dashboard, helping you observe them quickly. You can check metrics such as where users leave the app, where users spend the most time, bad API calls, and more. Business teams can review the data and suggest relevant development paths for the application.
Laravel Mix is used to mix different style sheets into a package and make it into a file. Larvel Mix is present by default in all Laravel applications. Laravel Mix is generally used to compile different CSS files into a single file so that the application does not need to call two different HTTPS APIs. Therefore, the speed of the application can be improved a bit. Given below is a sample code for mixing two CSS files using Laravel Mix.
mix.styles([ 'public/css/vendor/normalize.css', 'public/css/styles.css' ], 'public/css/all.css');
Blending two CSS files will generally increase the size of the resulting file, thus reducing the benefits of blending two CSS files. To address this issue, we optimized the hybrid package and reduced the size of the production-grade application. It helps in fast loading of applications and faster response time.
In computer science, caching means using information that has already been produced in previous iterations/execution cycles. In any type of web application, caching plays an important role in improving website speed.
Laravel provides a very useful caching command that helps improve performance. Given below:
php artisan config:cache
You can use this command to cache the configuration file. Likewise, you can also cache routes in Laravel using the following command:
php artisan route:cache
队列是可用于提高应用程序性能的关键数据结构。大多数时候,队列用于向最终用户或架构中的另一个服务发送消息。
例如,如果你想在他们登录到应用程序后发送消息,你可以将消息推送到队列中,它们将被一一发送。此外,使用第三方解决方案可能会导致发送通知延迟,但队列会立即发送通知,从而为你提供更好的用户响应。
最新版本的 PHP 是优化 Laravel 应用程序速度所必需的。最新版本对当前版本的 Laravel 进行了一些重要的更改。所以你应该总是尝试安装最新版本的 PHP 和 Laravel。
最小化就是将应用程序的不同组件尽可能地最小化。您可以优化各种内容,如代码、服务器端配置和网站上资源的使用。在这一部分中,我们将讨论 Laravel 应用程序中可以缩小的东西。
缩小是将代码简化并将其拆分,以便更快地加载网站,最大限度地减少前端的 API 调用。它有助于更快地编译代码和执行脚本。这种方法可以大大减少网站的加载时间和平稳性。此外,它还有助于调试错误,因为代码越少,阅读起来就越清晰。
CSS 在网站的响应时间中起着主要作用。更多 CSS 意味着更多样式;因此,网站加载需要更多时间。可以使用不同类型的 CSS 框架来设计您的网站。缩小 CSS 还包括删除不相关的样式、错误的代码样式、删除空格等。但是在缩小 CSS 时,您应该非常小心,因为所有浏览器的行为与相同的 CSS 不同。不同的浏览器对 CSS 的行为不同;因此,您应该在缩小 CSS 的同时正确测试您的应用程序。
缩小 Javascript 主要是为了比非缩小版本更快地运行脚本。在缩小过程中,您会删除未使用的代码,例如 API 调用、变量定义、不相关的导入等。如果您仔细进行 javascript 缩小,它可以显著提高性能。它还有助于更快地将数据从网站发送到服务器。
Scout APM 是一个基于现代的应用程序监控系统,用于监控几乎所有类型的应用程序。它支持许多框架,如 PHP、Ruby、Python、Elixir 等。这里我们将讨论如何使用 Scout APM 提高 Laravel 应用程序的性能。 Scout APM 支持 5.5 以上的所有 Laravel 版本。 Scout 还处理 N+1 查询的问题,这是 Laravel Eloquent 中的主要问题之一。
在你的 Laravel 应用程序中安装 Scout APM 非常简单;您只需在控制台中运行以下命令,
composer require scoutapp/scout-apm-laravel
当您运行此命令时,会安装scout-php
。
然后,下一步是在 .env 文件中配置环境变量。要将 Scout 集成到您的应用程序中,您必须添加以下键:
# Scout 设置 SCOUT_MONITOR=true SCOUT_KEY="[在 SCOUT 用户界面中]" SCOUT_NAME="为你的应用起一个友好的名称"
在通过 Heroku Addon 安装的情况下,您不需要显式设置SCOUT_MONITOR
和SCOUT_KEY
;它是自动设置的。
第三步是使用以下命令添加config/scout_apm.php
。
php artisan vendor:publish --provider=\"Scoutapm\\Laravel\\Providers\\ScoutApmServiceProvider\"
完成此步骤后,您将需要清除并重建缓存,否则会产生意想不到的结果。最后一步是在进行这些更改后,您必须部署您的应用程序。大约需要五分钟后,您网站的结果就会出现在 Scout 的仪表板中。
此外,如果您想添加 scoutapm PHP 扩展,您也可以使用以下命令轻松完成,
sudo pecl install scoutapm
If you want some tools like timing of libcurl and file_get_contents, you will have to install this extension.
In this way,Scout APMcan help you view all the metrics of your
Laravel application. You can use these statistics to improve your Laravel application and create more value for your customers. Even if you don't have a credit card, you can start using the Scout APM 14-Day
Card for free. If you want your Laravel application to perform better, you should start analyzing its metrics now. You can sign up to start your first application onScout APM's website.
Original address: https://laravel-news.com/how-to-optimize-laravel-application-performance
Translation address: https://learnku.com/ laravel/t/70358
[Related recommendations:laravel video tutorial]
The above is the detailed content of How to increase the speed of your Laravel application. For more information, please follow other related articles on the PHP Chinese website!