{"id":1820,"date":"2023-10-28T11:03:31","date_gmt":"2023-10-28T08:03:31","guid":{"rendered":"https:\/\/www.shneler.com\/oman\/?p=1820"},"modified":"2023-10-28T11:04:32","modified_gmt":"2023-10-28T08:04:32","slug":"how-to-optimize-laravel-performance","status":"publish","type":"post","link":"https:\/\/shneler.com\/oman\/?p=1820","title":{"rendered":"How to Optimize Laravel Performance"},"content":{"rendered":"<h3 id=\"1-route-caching\" class=\"has-anchor-hash\">1. Route Caching<\/h3>\n<p>Route caching is an excellent feature, especially for apps with a large number of configurations and routes spread across the code. It\u2019s a group of routes packed in a single command to help reduce the tedious task of charting your routes manually. As a result, your website\u2019s pages will load a lot faster.<\/p>\n<p>Route caching allows Laravel to retrieve routes periodically from the pre-compiled cache rather than having to start from the ground up for each new user.<\/p>\n<p>Use this command to cache the routing data required:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"language-bash\" tabindex=\"0\"><code class=\"language-bash\">php artisan route:cache<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>Keep in mind that the cache expires when the user leaves your site. It\u2019s also important to run that route cache command every time after making structural changes (e.g. routes files and config) to your website, as any modifications made afterward won\u2019t take effect.<\/p>\n<p>If you want to clear the route cache, run the following command:<\/p>\n<pre class=\"language-bash\" tabindex=\"0\"><code class=\"language-bash\">php artisan route:clear<\/code><\/pre>\n<h3 id=\"2-optimize-composer\" class=\"has-anchor-hash\">2. Optimize Composer<\/h3>\n<p>Laravel uses a separate tool called Composer to manage different dependencies. When you initially\u00a0install Composer, it loads dev dependencies into your system by default.<\/p>\n<p>These dependencies are useful for developing a website. But once your site is fully operational, they\u2019re no longer required, and in fact, they\u2019ll only slow it down.<\/p>\n<p>When utilizing Composer to install packages, use the\u00a0<code>--no-dev<\/code>\u00a0and\u00a0<code>-o<\/code>\u00a0parameters as follows to remove dev dependencies:<\/p>\n<pre class=\"language-bash\" tabindex=\"0\"><code class=\"language-bash\"><span class=\"token function\">composer<\/span> <span class=\"token function\">install<\/span> --prefer-dist --no-dev <span class=\"token parameter variable\">-o<\/span><\/code><\/pre>\n<p>This command allows Composer to create a directory for optimizing the autoloader and boosting performance. It simply requests the official distribution to be retrieved and packaged, with no dev dependencies.<\/p>\n<p>Be careful not to eliminate any runtime dependencies. This could jeopardize your website\u2019s performance or even cause it to crash.<\/p>\n<h3 id=\"3-reduce-autoloaded-services\" class=\"has-anchor-hash\">3. Reduce Autoloaded Services<\/h3>\n<p>The goal of Laravel is to make the development process as breezy for devs as possible. When you launch Laravel, for example, it auto-loads a large volume of service providers listed in the\u00a0<strong>config\/app.php<\/strong>\u00a0file to help you get started with your project quickly.<\/p>\n<p>While this is a beneficial step by Laravel, you won\u2019t need to use all of these services for building an application.<\/p>\n<p>Take the REST API for instance. You don\u2019t require services such as View Service Provider or Session Service Provider. In addition, many developers don\u2019t follow the default framework settings. You can simply disable services that are superfluous to your needs (e.g. Pagination Service Provider, Translation Service Provider, Auth Service Provider, etc).<\/p>\n<p>You\u2019ll be able to improve the speed of your Laravel applications by applying the same principle to other apps. Just make sure you don\u2019t remove any important services, and double-check everything before you drop the hammer.<\/p>\n<h3 id=\"4-use-artisan-commands-and-cache-effectively\" class=\"has-anchor-hash\">4. Use Artisan Commands and Cache Effectively<\/h3>\n<p>Artisan is a popular command-line tool that comes with Laravel. It makes it easy for developers to carry out recurring and complex tasks automatically. Website creators can also use it to conduct tests and generate commands.<\/p>\n<p>Using Artisan commands cleverly can amp up your app performance. Below, we\u2019ve listed several of the best caching commands you can utilize.<\/p>\n<h4 id=\"configuration-caching\" class=\"has-anchor-hash\">Configuration Caching<\/h4>\n<p>Cache config is an excellent command to get a speed boost. It compiles all of your application\u2019s configuration values into one file so that the framework can load faster. All you need is to run:<\/p>\n<pre class=\"language-bash\" tabindex=\"0\"><code class=\"language-bash\">php artisan config:cache<\/code><\/pre>\n<p>Note that you shouldn\u2019t execute the\u00a0<strong>config cache<\/strong>\u00a0command during local development. This is because configuration settings may need to be changed often throughout the development of your app.<\/p>\n<p>To clear the config cache, run this command:<\/p>\n<pre class=\"language-bash\" tabindex=\"0\"><code class=\"language-bash\">php artisan config:clear<\/code><\/pre>\n<h4 id=\"views-caching\" class=\"has-anchor-hash\">Views Caching<\/h4>\n<p>The view cache is another aspect of the application that contains a cache. The view cache stores generated Blade templates to increase the speed of your project. You can use the artisan command below to compile all views manually and optimize performance:<\/p>\n<pre class=\"language-bash\" tabindex=\"0\"><code class=\"language-bash\">php artisan view:cache<\/code><\/pre>\n<p>Remember to clear the cache when you upload a new code; otherwise, Laravel will use your old views and you will spend lots of time trying to troubleshoot this. Run this command to clear the view cache:<\/p>\n<pre class=\"language-bash\" tabindex=\"0\"><code class=\"language-bash\">php artisan view:clear<\/code><\/pre>\n<h4 id=\"application-caching\" class=\"has-anchor-hash\">Application Caching<\/h4>\n<p>This is the main cache in Laravel. It saves all the data that you cache manually in your app. Using Laravel\u2019s cache is a smart approach to speed up commonly accessed data and optimize Laravel performance. If you use tags or multiple cache storage, you can flush only certain elements of the cache.<\/p>\n<p>Here\u2019s the artisan command to clear the Laravel cache:<\/p>\n<pre class=\"language-bash\" tabindex=\"0\"><code class=\"language-bash\">php artisan cache:clear<\/code><\/pre>\n<p>Keep in mind that this command won\u2019t delete any route, config, or view cache located in the \/bootstrap\/cache\/ folder.<\/p>\n<aside id=\"MTIzODEx\" class=\"WzIxOTk1LDIxOTkzXQ==\" data-bce=\"dGhPLTdSQ2pYU2RJWkVtOE16\" data-bm=\"NUhWWE9kczBRQ2Y5b3libnB0\" data-cu0x=\"OV94bzdFcXVDMWNoa0Z0TTN2WVou\" data-qb5l0=\"YVlIWnJCNHBzN3o4bEVRMGg=\" data-weqpc1=\"YjlIdU55SlhWLW8=\" data-96zxp=\"U2hLNEd5eFBIQzdqLlJ1VnpGTFRiWQ==\" data-r3koi=\"Im1pZGRsZSI=|ImVuX1VTIg==\" data-p=\"WERHNm83MnJKRmZFcGgx\" data-c=\"V2otNE95ZS5LQUpvZ3VGYXRZ\" data-gro96k=\"TDVwYnhFclVldDJPVEk=\">\n<div>\n<div>\n<div class=\"bg-dark article-normal rounded background-image\">\n<div class=\"flex\">\n<div class=\"container row block-0\">\n<div class=\"col-6\">\n<h2 class=\"title font-size-3\">Deploy Your Laravel<\/h2>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/aside>\n","protected":false},"excerpt":{"rendered":"<p>1. Route Caching Route caching is an excellent feature, especially for apps with a large number of configurations and routes spread across the code. It\u2019s a group of routes packed in a single command to help reduce the tedious task of charting your routes manually. As a result, your website\u2019s pages will load a lot [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1821,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1820","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-1"],"_links":{"self":[{"href":"https:\/\/shneler.com\/oman\/index.php?rest_route=\/wp\/v2\/posts\/1820","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shneler.com\/oman\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shneler.com\/oman\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shneler.com\/oman\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shneler.com\/oman\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1820"}],"version-history":[{"count":2,"href":"https:\/\/shneler.com\/oman\/index.php?rest_route=\/wp\/v2\/posts\/1820\/revisions"}],"predecessor-version":[{"id":1824,"href":"https:\/\/shneler.com\/oman\/index.php?rest_route=\/wp\/v2\/posts\/1820\/revisions\/1824"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/shneler.com\/oman\/index.php?rest_route=\/wp\/v2\/media\/1821"}],"wp:attachment":[{"href":"https:\/\/shneler.com\/oman\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shneler.com\/oman\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shneler.com\/oman\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}