Laravel5.1优化方案

Published on 2017 - 04 - 02

Before deploying your application you’ll logically want to ensure the code has been properly optimized for a production environment. Frankly, entire books have been written about tuning web applications, and therefore this discussion could go in many directions and really never even begin scratch the surface. Therefore I think it makes the most sense to focus on a few key Laravel-specific optimization features, for the moment leaving third-party optimization solutions out of the discussion. In future iterations I’ll selectively expand this section to cover other topics.

Creating a Faster Class Loader

A Laravel application’s request and response life cycle obviously involves quite a few different classes. As you might imagine, loading and invoking dozens of different classes with each request can be quite a detriment to performance. Laravel offers a solution for creating an optimized class loader by way of Composer. You can improve performance by using Artisan’s optimize command to significantly improve the efficiency in which your project classes are loaded. You’ll invoke optimize like so:

$ php artisan optimize

This command will by default run Composer’s dump-autoload command with the --optimize option. This command will in turn create vendor/composer/autoload_classmap.php which contains an array consisting of all class names and the paths to their corresponding files. This file is then subsequently used to quickly load third-party classes because the array can be used for referential purposes rather than requiring the autoloader to separately find and open each class file.

Additionally, Laravel will further optimize matters by concatenating all of its own native classes into a single file found at storage/framework/compiled.php, and also create a file named services.json in the same directory. This file is used to optimize the loading of your project’s service providers. Further, it will cache all of your project’s views within storage/framework/views.

However, if your project’s APP_DEBUG configuration setting is set to true (the default when in your development environment), this command will not work as intended because Laravel presumes you’ll always want to be working with the very latest versions of your files rather than rely on a cache. You can override this behavior in the local environment by including the --force option:

$ php artisan optimize --force
Generating optimized class loader
Compiling common classes
Compiling views

Now you’ll be able to peruse storage/framework/compiled.php and storage/framework/services.json even when working in the local environment.

You can replace compiled.php and services.json by running optimize anew, keeping in mind you’ll need the --force option if you’d like to experiment with it locally. If you’d like to remove these files altogether, run the following command:

$ php artisan clear-compiled

This command (also confusingly) does not however automatically delete the compiled views which were generated when the --force option was provided. At this time there is not any documented solution for deleting these files via Artisan, so you could optionally (and rather easily) write your own Artisan command for doing so, or just navigate to storage/framework/views and manually delete all of the files.

Caching Route Definitions

The route definitions found in app/Http/routes.php are by default read into the framework as part of the bootstrapping process. You can cache these routes by encoding and serializing them using Artisan’s route:cache command:

$ php artisan route:cache
Route cache cleared!
Routes cached successfully!

This cache file is stored in vendor/routes.php. If this file exists, Laravel will refer to it rather than parsing the source route definitions file. You can delete the route cache file using route:clear:

$ php artisan route:clear

Optimizing Your CSS and JavaScript

You’ll always want to minimize the number and size of requests required to render your site within the user’s browser. One of the easiest things you can do in this regards is to optimize your CSS and JavaScript, as well as take advantage of content delivery networks.

Combining your CSS using Elixir and a CSS preprocessor such as Less is pretty easy; take a look at resources/assets/less/app.less and you’ll see how to use Less’ @import directive to combine multiple CSS files:

@import "bootstrap/bootstrap";

@btn-font-weight: 300;
@font-family-sans-serif: "Roboto", Helvetica, Arial, sans-serif;

body, label, .checkbox label {
        font-weight: 300;
}

Laravel’s default gulpfile.js uses mix.less to compile the app.less file, saving the combined CSS output to public/css/app.css. This is great because it combines the more than 40 Bootstrap CSS source files found in resources/assets/less/bootstrap and the custom Less CSS found in app.less into a single file. However you’ll additionally want to minify the CSS (remove all whitespace and comments). You can do so by passing --production to gulp:

$ gulp --production

Just taking the default app.less and Bootstrap files into account, using --production results in a 20% reduction in the compiled app.css file size!

You can additionally combine your JavaScript files together. For instance if you are managing two separate CoffeeScript files in resources/assets/coffee named test.coffee and test2.coffee, and wanted to combine the compiled output into a single file within public/js you can update gulpfile.js like so:

mix.coffee().scriptsIn('public/js', 'public/js');

When the CoffeeScript files found in resources/assets/coffee are compiled and saved to public/js, the scriptsIn method will subsequently concatenate these files together and save them to a file named all.js. Passing --production to the gulp command will additionally minify the concatenated JavaScript file.

When relying on third-party libraries such as jQuery you’ll almost certainly want to use a CDN (Content Delivery Network) rather than locally host your own copy. This may seem counterintuitive, however the reasoning behind this best practice is explained here.

Reference

  • Easy Laravel 5 Chapter 8 Deploying, Optimizing and Maintaining Your Application