Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Yaml Front Matter

Yaml Front Matter Laravel Package

spatie/yaml-front-matter

Parse YAML front matter from Markdown and similar files. Reads metadata wrapped in --- at the top and returns an object with easy access to fields (via matter() or properties) plus the remaining body content. Ideal for static pages, docs, and blogs.

View on GitHub
Deep Wiki
Context7

A to the point yaml front matter parser

Frequently asked questions about Yaml Front Matter
How do I parse YAML front-matter from a Markdown file in Laravel?
Use `YamlFrontMatter::parse(file_get_contents('path/to/file.md'))` to extract metadata. The parsed object supports both `matter('key')` and direct property access (e.g., `$object->title`). For Laravel, pair this with `Storage::disk()` to read files from `storage` or `public` directories.
Does this package work with Laravel Eloquent models?
Yes. Parse front-matter in model accessors (e.g., `getTitleAttribute()`) or use model observers to hydrate fields from YAML. Example: `return YamlFrontMatter::parse($this->content)->title;` in an accessor. Cache results with Redis for performance.
What Laravel versions and PHP requirements does spatie/yaml-front-matter support?
The package requires PHP 8.1+ and is officially tested with Laravel 10/11. It avoids deprecated features, ensuring compatibility with Laravel’s LTS releases. Check the [GitHub](https://github.com/spatie/yaml-front-matter) for minor version updates.
How do I handle malformed YAML or security risks like YAML injection?
The package throws exceptions on invalid YAML. Mitigate security risks by validating file paths (e.g., `realpath()`) and sanitizing YAML content. For strict environments, pre-validate files with `symfony/yaml` or whitelist allowed keys. Avoid arbitrary YAML tags (e.g., `!!php/object`).
Can I cache parsed YAML front-matter for better performance?
Absolutely. Cache the parsed `YamlFrontMatterObject` using Laravel’s cache (e.g., `Cache::remember()`) or Redis with tags (e.g., `post:{id}`). Invalidate caches on file changes via filesystem events or model observers. For dynamic sites, cache for 5–15 minutes.
How do I integrate this with Laravel’s Filesystem (e.g., Storage facade)?
Use `Storage::disk('local')->get('file.md')` to read files, then pass the content to `YamlFrontMatter::parse()`. For large-scale apps, stream files with `SplFileObject` to avoid memory issues. Example: `$content = Storage::disk('public')->read('posts/post.md');`
Are there alternatives to spatie/yaml-front-matter for Laravel?
Yes. For basic needs, use `symfony/yaml` directly. For Laravel-specific solutions, consider `laravel-frontmatter` (older, less maintained) or `spatie/laravel-markdown` (if you need combined Markdown/YAML parsing). This package is preferred for its simplicity and Laravel integration.
How do I validate required YAML fields in front-matter?
Extend the parsed object or use Laravel validation rules. Example: `Validator::make($object->matter(), ['title' => 'required|string'])` before saving to a model. For reusable validation, create a custom validator or use `spatie/laravel-validation-rules`.
Will this package work with nested YAML structures (e.g., arrays, objects)?
Yes, it supports standard YAML 1.2 structures like arrays (`tags: [tech, laravel]`) and nested objects. Access nested keys with `matter('nested.key')` or `$object->nested->key`. For complex schemas, validate with `symfony/yaml` or JSON Schema.
How do I migrate from hardcoded metadata to YAML front-matter in a Laravel app?
Start by adding YAML front-matter to a subset of files (e.g., blog posts). Use feature flags to toggle parsing logic. Update models to read from YAML instead of hardcoded fields. Test incrementally, then replace legacy metadata paths. Use CI/CD to lint YAML files with `symfony/yaml`.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport