search
HomeBackend DevelopmentPHP TutorialAutomatic Asset Optimization with Munee

Munee: A powerful PHP asset management tool, say goodbye to NodeJS

Munee is a PHP-based asset management tool that can compile LESS, SCSS or CoffeeScript, process images, compress CSS and JS, and dynamically cache assets on the server and client side. It supports PHP 5.3 and later. This tutorial will explain how to use Munee to simplify how assets are included in templates, as well as how they are installed, working, and using them. Munee provides an alternative to NodeJS for asset management of PHP applications.

Automatic Asset Optimization with Munee

Core advantages:

  • Omni-purpose asset management: Munee is able to compile LESS, SCSS and CoffeeScript, process images, compress CSS and JS, and implement server-side and client-side caching. It automates many tasks that would have to be done manually, saving time.
  • Dynamic processing: Munee processes or compresses these assets according to query string parameters by intercepting client requests for CSS, LESS, SCSS, JS, CoffeeScript and various image format files. It also enables server-side caching by storing compiled, compressed, and processed assets in separate directories.
  • Image Processing: In addition to compiling and compressing CSS and JS files, Munee can dynamically adjust image size, crop, color, and convert images to grayscale or negative effects. It can also replace missing images with default images and combine assets to reduce the total number of HTTP requests required to get all assets.
  • Easy to install and customize: Munee can be easily installed through Composer and provides API provisioning to manually optimize, process and compress assets with programs. This is a highly customizable tool that allows developers to specify the optimization techniques to use and adjust the compression level.

Why choose Munee?

Munee is designed to simplify asset management. It dynamically performs many tasks that we had to do manually before (i.e., when the client requests assets), thus saving time. Here are some reasons why you might want to use Munee:

  1. We often make small changes to CSS, LESS, SCSS, JavaScript, and CoffeeScript files. Each change requires compilation and compression. Munee will do these operations automatically.
  2. Many shared web hosting servers do not have gzip enabled. If you use Munee, it will use PHP to compress the file, ignoring this limitation.
  3. You don't need to worry about adding cache directives to .htaccess files. Munee will be responsible for server-side and client-side asset caching.
  4. Munee can process images dynamically and is very convenient for responsive web design. You do not need to maintain different image files for different sizes.

How Munee works:

After Munee is installed, it will automatically start cache assets on the server, send the correct client cache header, and start sending gzip compressed responses, as well as compilation output to requested LESS, SCSS, and CoffeeScript files.

To provide instructions for processing images or compressing CSS and JS files, you need to add query string parameters to the asset path.

To be able to process or compress assets, Munee needs to intercept client requests for CSS, LESS, SCSS, JS, CoffeeScript and various image format files. In order for Munee to intercept client requests for these assets, we need to add internal rewrite rules to the .htaccess file (Nginx description is also below).

Munee uses other third-party libraries (such as imagine, leaf, meenie, tedivm, etc.) to resize, process, compile and compress assets.

The query string parameters used to provide instructions to Munee are called filters.

How does Munee cache assets?

To implement client caching, it sets the Cache-Control: must-revalidate header when sending a response to the requested asset. It also reads the cache header in the HTTP request and sends a response or 304 Not Modified state based on whether there are latest assets in the client cache.

To implement server-side caching, it stores compiled, compressed, and processed assets in a separate directory.

It detects changes to the original asset at runtime. When it detects changes to the original asset, it updates the server cache and forces the client to use the latest file.

Installation of Munee:

Install with the following command:

composer require meenie/munee

If you try to install Munee on a shared hosting server, use composer require Munee manually on your local computer and upload the vendor directory to the hosting server.

Now we need to create a PHP file that is responsible for optimizing, processing, compiling and compressing assets using Munee. To do this, create a PHP file called munee.php:

<?php
require "vendor/autoload.php";

echo \Munee\Dispatcher::run(new \Munee\Request());

Now we need to redirect asset requests for CSS, LESS, SCSS, JS, CoffeeScript and various image format files to munee.php. We can do this using the server's internal URL rewrite rules.

If you are using Apache, place this code in the .htaccess file in the directory where the munee.php file:

RewriteEngine On
RewriteRule ^(.*\.(?:css|less|scss|js|coffee|jpg|png|gif|jpeg))$ munee.php?files=/ [L,QSA,NC]

If you are using Nginx, you must modify the actual virtual host settings for URL rewrite rules based on this issue and this gist.

If .htaccess is disabled on your server, or you don't want to use .htaccess for rewriting, then you can manually pass the file path to munee.php instead of using the asset path in HTML.

For the rest of this tutorial, we will assume that you are using .htaccess.

All assets in the directory tree where Munee is installed will be optimized.

(The following content is a brief summary of the rest of the original text to avoid duplication and redundancy)

Compile SCSS, LESS and CoffeeScript: Simply point to these files in HTML and Munee will automatically handle the server-side compilation.

Compress CSS and JS files: Add the minify=true parameter to the asset path in HTML.

Processing images: Munee allows dynamic resizing, cropping, and shading of images, and supports grayscale, negative film conversion, and placeholders for missing images. Image size and cropping can be controlled using the resize filter, and an alternative to missing images can be configured in the placeholders array. Munee has built-in security mechanisms to prevent malicious image processing requests. munee.php

Combination Assets: Use comma-separated file paths in HTML to combine multiple CSS or JS files.

Munee API: Provides API provisioning applications to manually optimize assets.

Summary: Munee is ideal for dynamically managing assets.

FAQ (FAQ): (The original FAQ part has been summarized and core information is retained)

Munee is a standalone PHP library for automating a variety of web performance optimization tasks. Compared with other tools, it requires no additional software, is highly flexible and customizable, and supports a variety of file types and optimization technologies. It can handle image optimization, CSS and JavaScript optimizations, and supports custom optimization settings. The system requirements are PHP 5.3 or higher, as well as the GD library and LESS/SCSS compilation library.

The above is the detailed content of Automatic Asset Optimization with Munee. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

What are the advantages of using a database to store sessions?What are the advantages of using a database to store sessions?Apr 24, 2025 am 12:16 AM

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

How do you implement custom session handling in PHP?How do you implement custom session handling in PHP?Apr 24, 2025 am 12:16 AM

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

What is a session ID?What is a session ID?Apr 24, 2025 am 12:13 AM

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

How do you handle sessions in a stateless environment (e.g., API)?How do you handle sessions in a stateless environment (e.g., API)?Apr 24, 2025 am 12:12 AM

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)