PHP code example of backstage / laravel-minify-html-middleware

1. Go to this page and download the library: Download backstage/laravel-minify-html-middleware library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

backstage / laravel-minify-html-middleware example snippets




return [
    'transformers' => [
        Backstage\MinifyHtml\Transformers\RemoveComments::class,
        Backstage\MinifyHtml\Transformers\RemoveWhitespace::class,
        Backstage\MinifyHtml\Transformers\TrimScripts::class,
    ],
];



use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Backstage\MinifyHtml\Middleware\MinifyHtml;

return Application::configure(basePath: dirname(__DIR__))
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->append(MinifyHtml::class);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

protected $middleware = [
    // ...
    \Backstage\MinifyHtml\Middleware\MinifyHtml::class,
];

->withMiddleware(function (Middleware $middleware) {
    $middleware->alias([
        'minify' => \Backstage\MinifyHtml\Middleware\MinifyHtml::class,
    ]);
})

protected $middlewareAliases = [
    // ...
    'minify' => \Backstage\MinifyHtml\Middleware\MinifyHtml::class,
];

Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware('minify');

// Or to a group
Route::middleware(['minify'])->group(function () {
    Route::get('/about', [AboutController::class, 'index']);
    Route::get('/contact', [ContactController::class, 'index']);
});



return [
    'transformers' => [
        // Use only specific transformers
        Backstage\MinifyHtml\Transformers\RemoveComments::class,
        Backstage\MinifyHtml\Transformers\RemoveWhitespace::class,
        // Backstage\MinifyHtml\Transformers\TrimScripts::class, // Disabled

        // Add custom transformers
        App\HtmlTransformers\CustomTransformer::class,
    ],
];



return [
    'transformers' => [
        // Only remove whitespace, keep comments
        Backstage\MinifyHtml\Transformers\RemoveWhitespace::class,
    ],
];



namespace App\HtmlTransformers;

class RemoveMetaTags
{
    public function transform(string $html): string
    {
        // Remove all meta tags
        return preg_replace('/<meta[^>]*>/i', '', $html);
    }
}



return [
    'transformers' => [
        Backstage\MinifyHtml\Transformers\RemoveComments::class,
        Backstage\MinifyHtml\Transformers\RemoveWhitespace::class,
        Backstage\MinifyHtml\Transformers\TrimScripts::class,
        App\HtmlTransformers\RemoveMetaTags::class,
    ],
];



namespace App\HtmlTransformers;

class UppercaseTitle
{
    public function transform(string $html): string
    {
        return preg_replace_callback(
            '/<title>(.*?)<\/title>/i',
            function ($matches) {
                return '<title>' . strtoupper($matches[1]) . '</title>';
            },
            $html
        );
    }
}

// Removes: <!-- This will be removed -->
// Keeps: <!--Livewire--> and <!-- ko --> (Knockout.js)



return [
    'transformers' => [
        // Backstage\MinifyHtml\Transformers\RemoveComments::class,
        Backstage\MinifyHtml\Transformers\RemoveWhitespace::class,
        // Backstage\MinifyHtml\Transformers\TrimScripts::class,
    ],
];
bash
php artisan vendor:publish --tag="laravel-minify-html-middleware-config"
bash
composer analyse