innmind/media-type
A PHP library for working with media types (MIME types): parse, validate, and compare content types, handle parameters, and map file extensions. Lightweight and framework-agnostic, it helps Laravel and PHP applications negotiate HTTP responses, validate uploads, and manage content-type headers relia...
Accept header — including wildcards (text/*, */*), suffixes (application/problem+json, application/hal+json), and quality-ordered variants — versus returning 406 Not Acceptable. This is the single highest-value use case: it is user-visible behavior that is easy to get subtly wrong.image/jpg → image/jpeg), and safely separating a type from its parameters (charset, boundary) before storage or downstream processing.explode(';') check. That is 20 lines we would own forever, and it is almost always wrong on parameters, quoting, or case-insensitivity. A small MIT value object is cheaper than the review cycles a hand-rolled validator attracts.MediaType enters our domain as a typed value object versus a string passed through five layers. Adopting it makes invalid media types unrepresentable at the boundary, which reduces defensive validation further down.Vary: Accept-driven cache key derivation, and validating OpenAPI content maps in contract tests all become trivial once a parsed representation exists.Adopt it when:
Content-Type / Accept values and currently validate them with regex, str_contains, or naive explode(';') splitting.bool you can forget to check.Look elsewhere when:
Request::accepts(), getAcceptableContentTypes(), and getMimeType() already cover routine negotiation. Buying a package to duplicate framework behavior is not a win.symfony/mime or fileinfo instead.Net: a good fit for a narrowly scoped, well-tested boundary component. A poor fit as a mandated org-wide standard.
For executives: This is a small, MIT-licensed PHP component that turns "is this a valid media type?" into one tested call instead of our own regex. It removes a recurring class of bugs in two places customers actually notice — API content negotiation and upload validation — at effectively zero cost and with no vendor lock-in. We can replace it in an afternoon if we ever want to. Recommendation: pilot in one service, measure, then decide.
For engineering: A typed value object for RFC-style media type strings — parsing, parameter extraction, wildcard and suffix handling, and normalization. It replaces bespoke string handling and gives us a construct that cannot hold an invalid value. Two caveats to price in: adoption signal is essentially nil (0 stars, 0 dependents, single-maintainer ecosystem), so read the source, pin the version, and check its PHP and transitive dependency requirements against our matrix before committing. Wrap it behind a thin internal interface so the swap cost stays trivial. Proposed next step: adopt behind an adapter in the API gateway service, cover the negotiation paths with our own tests, and revisit org-wide use after one quarter of production signal.
How can I help you explore Laravel packages today?