PHPackages                             jrhenderson1988/miniphy - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. jrhenderson1988/miniphy

ActiveLibrary[Templating &amp; Views](/categories/templating)

jrhenderson1988/miniphy
=======================

A HTML/PHP template minifier. Includes support for Laravel's Blade compiler.

v1.0(9y ago)02.1k↓50%MITPHP

Since Mar 23Pushed 9y ago2 watchersCompare

[ Source](https://github.com/jrhenderson1988/miniphy)[ Packagist](https://packagist.org/packages/jrhenderson1988/miniphy)[ RSS](/packages/jrhenderson1988-miniphy/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

Miniphy
=======

[](#miniphy)

A PHP based HTML minifier that's designed to be simple. It offers built in support for Laravel 5, providing both a service provider and facade. Additionally, it is configurable to allow for compile-time minification of blade template files.

Installation
============

[](#installation)

You can install Miniphy using composer. Just run the following command to get the latest version:

```
$ composer require jrhenderson1988/miniphy

```

Or you can add it manually to your `composer.json` file and run `composer update`.

```
{
    "require": {
        "jrhenderson1988/miniphy": "^1.0"
    }
}

```

If you're using Laravel, don't forget to add the `MiniphyServiceProvider` to your `config/app.php`.

```
// ...

Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,

/**
 * Package Service Providers...
 */
Miniphy\MiniphyServiceProvider::class,

// ...

```

If you want to use the Facade, make sure you add it to your `aliases` in `config/app.php`.

```
    // ...

    'Validator' => Illuminate\Support\Facades\Validator::class,
    'View' => Illuminate\Support\Facades\View::class,

    'Miniphy' => Miniphy\Facades\Miniphy::class,
],

```

Usage
=====

[](#usage)

It's pretty easy to use Miniphy, in fact it's just a case of creating a `Miniphy` instance and calling the `html` method to create a HTML driver to be used for minifying HTML. You can then call the `minify` method on the resulting driver with some content and it's minified content will be returned:

```
use Miniphy\Miniphy;

$content = '           Your HTML content            ';
$miniphy = new Miniphy();
$minifiedContent = $miniphy->html()->minify($content);

```

For convenience, you may also pass your HTML content to the `html` method and the minified content will be returned. Under the hood this will create a HTML driver and call it's `minify` method.

```
use Miniphy\Miniphy;
$content = '           Your HTML content            ';
$minifiedContent = (new Miniphy())->html($content);

```

### Modes

[](#modes)

Miniphy's HTML minification allows for 3 different modes: soft, medium and hard.

- *Soft (default):* This mode will leave a single space between HTML elements when removing whitespace. This is the safest mode to use and will be the least likely to cause problems.
- *Medium:* This mode will remove all whitespace around block level and undisplayed elements, but preserve a single space around inline elements such as `span`, `a` etc. Bear in mind that it's possible to make typically inline elements behave like block elements using CSS, so this mode may have unwanted side-effects if doing so.
- *Hard*: This is the most aggressive mode and will remove whitespace around all elements.

You can easily set the mode by calling the chainable `setHtmlMode` method on the `Miniphy` instance. There is also a `htmlMode` method that can be used to get or set the HTML mode, when provided with a parameter, this method will return the `Miniphy` instance for chaining. Without a parameter, this method will return an integer value representing the mode.

```
use Miniphy\Miniphy;

$miniphy = new Miniphy();

// Soft mode
$miniphy->setHtmlMode(Miniphy::HTML_MODE_SOFT);

// Set medium mode and minify the provided HTML content
$miniphy->setHtmlMode(Miniphy::HTML_MODE_MEDIUM)->html(' HTML CONTENT ');

// Set hard mode and minify the content using the htmlMode method
$miniphy->htmlMode(Miniphy::HTML_MODE_HARD)->html(' HTML CONTENT ');

```

### Laravel

[](#laravel)

When you've set up the package correctly in Laravel you can use Miniphy very easily, either through using the Facade, dependency injection or you can get an instance of `Miniphy` from the IoC container. Alternatively, you may enable compile-time Blade optimisation through the settings to automatically minify your blade templates when they're generated:

#### Facade

[](#facade)

```
$minified = Miniphy::html(' Your HTML content ');

```

#### Dependency Injection

[](#dependency-injection)

```
