承接 caujasutom/laravel-optimizer 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

caujasutom/laravel-optimizer

Composer 安装命令:

composer require caujasutom/laravel-optimizer

包简介

A lightweight Laravel package to optimize performance by minifying HTML, caching, and more.

README 文档

README

The Laravel Optimizer package is a collection of middlewares and helpers designed to optimize and improve the performance of your Laravel applications. It includes HTML minification, static HTML caching, and Gzip compression.

Table of Contents

  1. Installation
  2. Configuration
  3. Middlewares
    1. HTML Minify Middleware
    2. Static HTML Cache Middleware
    3. Gzip Compression Middleware
  4. Helpers
  5. Examples

Installation

To install the Laravel Optimizer package, run the following command in your project's root directory:

composer require caujasutom/laravel-optimizer

After installing the package, publish the configuration file by running the following command:

php artisan vendor:publish --provider="Caujasutom\LaravelOptimizer\LaravelOptimizerServiceProvider"

Configuration

The configuration file config/laravel_optimizer.php contains the default settings for the package. You can modify these settings to suit your needs.


 env('LARAVEL_OPTIMIZER_STATIC_CACHE_TTL', 60),
// Path for storing cache files.
'cache_path' => env('LARAVEL_OPTIMIZER_CACHE_PATH', 'static_cache/'),

// Compression level for gzip (0-9).
'compression_level' => env('LARAVEL_OPTIMIZER_COMPRESSION_LEVEL', 9),

// Gzip compression settings
'gzip_compression' => [
    'level' => env('LARAVEL_OPTIMIZER_GZIP_COMPRESSION_LEVEL', 9), // Default compression level for Gzip.
],

// Minification settings
'minification' => [
    'enabled' => env('LARAVEL_OPTIMIZER_MINIFICATION_ENABLED', true),
    'patterns' => [
        '/>\s+/s',
        '/\s+</s',
        '/(\s)+/s',
        '/>\s+</',
        '/[\r\n]+/'
    ],
    'replacements' => [
        '>',
        '<',
        '\\1',
        '><',
        ''
    ],
],

];

Middlewares

1. HTML Minify Middleware

The HtmlMinifyMiddleware minifies HTML content by removing unnecessary whitespace and line breaks. To use this middleware, add it to your app/Http/Kernel.php file.

protected $middlewareGroups = [
    'web' => [
        // ...
        \Caujasutom\LaravelOptimizer\Middleware\HtmlMinifyMiddleware::class,
    ],
];

2. Static HTML Cache Middleware

The StaticHtmlCacheMiddleware caches the generated HTML content for a specified duration. To use this middleware, add it to a specific route or route group in your routes/web.php file.

Route::middleware(['static.html.cache:60'])
    ->group(function () {
        Route::get('/', 'HomeController@index');
});

The number (60 in the example above) represents the cache duration in minutes. Adjust this value as needed.

3. Gzip Compression Middleware

The GzipCompressionMiddleware compresses the response content using Gzip compression. To use this middleware, add it to your app/Http/Kernel.php file.

protected $middlewareGroups = [
    'web' => [
        // ...
        \Caujasutom\LaravelOptimizer\Middleware\GzipCompressionMiddleware::class,
    ],
    // ...
];

Helpers

The package provides helper functions to simplify the use of the StaticHtmlCache class. The following functions are available:

LaravelOptimizer::cache()->store()

Stores the generated HTML content for the given URL.

LaravelOptimizer::cache()->store($url, $content, $minutes = null);

Parameters:

Parameter Type Description
$url string The URL for which the HTML content is generated.
$content string The HTML content to be cached.
$minutes int|null The duration in minutes for which the content should be cached. If not provided, the default environment value will be used.

LaravelOptimizer::cache()->retrieve($url)

Retrieves the cached HTML content for the given URL.

LaravelOptimizer::cache()->retrieve($url);

Parameters:

Parameter Type Description
$url string The URL for which to retrieve the cached HTML content.

Returns:

Type Description
string|null The cached HTML content for the given URL, or null if it doesn't exist.

LaravelOptimizer::cache()->delete($url)

Deletes the cached HTML content for the given URL.

LaravelOptimizer::cache()->delete($url);

Parameters:

Parameter Type Description
$url string The URL for which to delete the cached HTML content.

Examples

namespace App\Http\Controllers;

use Caujasutom\LaravelOptimizer\Facades\LaravelOptimizer; use Illuminate\Http\Request; use Illuminate\Support\Facades\View;

class ArticlesController extends Controller { /** * Display a listing of articles. * * @return \Illuminate\Http\Response */ public function index() { // Check if cached HTML content exists for this URL $cachedContent = LaravelOptimizer::cache()->retrieve(request()->url()); if ($cachedContent) { // If cached content exists, return it return response($cachedContent); } else { // If cached content doesn't exist, generate and cache new content $articles = Post::all(); // Fetch articles from the database or any other source

        // Render the articles view
        $htmlContent = View::make('articles.index', ['articles' => $articles])->render();
        
        // Cache the generated HTML content for this URL
        LaravelOptimizer::cache()->store(request()->url(), $htmlContent);
        
        return response($htmlContent);
    }
}

}

In this example, we have an ArticlesController with an index function. This function is responsible for displaying a listing of articles.

Here's the breakdown of the function:

  1. First, it checks if there is cached HTML content available for the current URL using the LaravelOptimizer::cache()->retrieve(request()->url()) method.
  2. If cached content exists, it returns the cached HTML content as the response.
  3. If cached content doesn't exist, it fetches the articles from the database or any other source.
  4. It then renders the articles.index view with the fetched articles using View::make() and render() methods.
  5. The generated HTML content is stored in the variable $htmlContent.
  6. The generated HTML content is cached using LaravelOptimizer::cache()->store(request()->url(), $htmlContent) method.
  7. Finally, it returns the generated HTML content as the response.

统计信息

  • 总下载量: 245
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-28