HTMLMin Is A Simple HTML Minifier For Laravel 5
Laravel HTMLMin was created by, and is maintained by Graham Campbell, and is a simple HTML minifier for Laravel 5. It utilises Mr Clay's Minify package to minify entire responses, but can also minify blade at compile time. Feel free to check out the change log, releases, license, and contribution guidelines.
To get the latest version of Laravel HTMLMin, simply add the following line to the require block of your
You'll then need to run
composer install or
composer update to download it and have the autoloader updated.
Once Laravel HTMLMin is installed, you need to register the service provider. Open up
config/app.php and add the following to the
You can register the HTMLMin facade in the
aliases key of your
config/app.php file if you like.
'HTMLMin' => 'GrahamCampbell\HTMLMin\Facades\HTMLMin'
Laravel HTMLMin supports optional configuration.
To get started, you'll need to publish all vendor assets:
$ php artisan vendor:publish
This will create a
config/htmlmin.php file in your app that you can modify to set your configuration. Also, make sure you check for changes to the original config file in this package between releases.
There are two config options:Automatic Blade Optimizations
This option (
'blade') enables minification of the blade views as they are compiled. These optimizations have little impact on php processing time as the optimizations are only applied once and are cached. This package will do nothing by default to allow it to be used without minifying pages automatically. The default value for this setting is
This option (
'force') forces blade minification on views where there such minification may be dangerous. This should only be used if you are fully aware of the potential issues this may cause. Obviously, this setting is dependent on blade minification actually being enabled. The default value for this setting is
This is the class of most interest. It is bound to the ioc container as
'htmlmin' and can be accessed using the
Facades\HTMLMin facade. There are four public methods of interest.
'blade' method will parse a string as blade and minify it as quickly as possible. This is method the compiler class uses when blade minification is enabled.
'js' methods will parse a string as css/js and will minify it using Mr Clay's Minify package.
'html' method will parse a string as html and will minify it as best as possible using Mr Clay's Minify package. It will also be able to minify inline css and js. This is the method that is used by the minification middleware.
This facade will dynamically pass static method calls to the
'htmlmin' object in the ioc container which by default is the
This interface defines the public method a minifier class must implement. Such a class must only provide a
'render' method which takes one parameter as a string, and should return a string. This package ships with 4 implementations of this interface, but these classes are not intended for public use, so have no been documented here. You can see the source here.
You may put the
GrahamCampbell\HTMLMin\Http\Middleware\MinifyMiddleware middleware in front of your routes to live minify them. Note that this middleware allows you to achieve maximal results, though at a performance cost because of it running on each request instead of once like the built in blade minification. It may be useful for you to take a look at the source for this, read the tests, or check out Laravel's documentation if you need to.
This class contains no public methods of interest. This class should be added to the providers array in
config/app.php. This class will setup ioc bindings and register automatic blade minification based on the config.
There are other classes in this package that are not documented here (such as the compiler class). This is because they are not intended for public use and are used internally by this package.
Laravel HTMLMin is licensed under The MIT License (MIT).