tomvdpeet/markdown-negotiation-bundle
Composer 安装命令:
composer require tomvdpeet/markdown-negotiation-bundle
包简介
Symfony bundle to negotiate Markdown responses from HTML routes.
README 文档
README
Symfony bundle that negotiates Markdown responses for opted-in routes.
This bundle was created for the article Getting agent-ready with Symfony.
Installation
composer require tomvdpeet/markdown-negotiation-bundle
Symfony Flex enables the bundle automatically. If you do not use Flex, register it manually in config/bundles.php:
TomvdPeet\MarkdownNegotiationBundle\MarkdownNegotiationBundle::class => ['all' => true],
Usage
Enable Markdown negotiation per route with the markdown route option:
use Symfony\Component\Routing\Attribute\Route; #[Route('/docs', name: 'docs', options: ['markdown' => true])] public function docs(): Response { return $this->render('docs/index.html.twig'); }
Requests that prefer text/markdown over text/html receive a converted Markdown response:
Accept: text/markdown, text/html;q=0.5
HTML remains the response format when it has a higher or equal quality:
Accept: text/html, text/markdown;q=0.5
Wildcard-only requests also keep HTML:
Accept: */*
Debug Query Parameter
When kernel.debug is enabled, you can force Markdown negotiation from a browser by adding _markdown to the URL:
/docs?_markdown
This behaves like the request preferred text/markdown, but only in debug kernels. Production kernels keep the normal Accept header behavior and do not check the query string.
The query parameter name is configurable:
markdown_negotiation: debug_query_parameter: markdown_preview
Set it to null or false to disable the debug shortcut.
Behavior
The bundle only converts responses when all of these conditions match:
- the current route has
options: ['markdown' => true] - the request is the main request
text/markdownhas a strictly higher effectiveAcceptquality thantext/html- the response is successful
- the response content type is
text/html - the response is not streamed or binary
Routes without the option return immediately through an optimized route map. Responses that already use text/markdown are left untouched.
Before conversion, the HTML is cleaned to remove common layout/decorative nodes such as head, nav, script, style, footer, and aside. Markdown-relevant tags such as headings, paragraphs, links, images, code, lists, tables, and blockquotes are preserved for conversion.
Links and images keep their href and src values. Absolute same-host URLs are normalized to relative URLs before conversion.
Manual Markdown Responses
Use MarkdownResponse when a controller already has Markdown and should return it directly:
use TomvdPeet\MarkdownNegotiationBundle\Http\MarkdownResponse; return new MarkdownResponse("# Hello\n\nManual Markdown.");
This sets Content-Type: text/markdown; charset=UTF-8 and does not add Vary: Accept.
If the response was selected through content negotiation, use the named constructor:
return MarkdownResponse::negotiated("# Hello");
That also adds Vary: Accept.
For custom controller logic, inject MarkdownNegotiator:
use Symfony\Component\HttpFoundation\Request; use TomvdPeet\MarkdownNegotiationBundle\Http\MarkdownNegotiator; use TomvdPeet\MarkdownNegotiationBundle\Http\MarkdownResponse; public function docs(Request $request, MarkdownNegotiator $negotiator): Response { if ($negotiator->prefersMarkdown($request)) { return MarkdownResponse::negotiated("# Docs"); } return $this->render('docs/show.html.twig'); }
tomvdpeet/markdown-negotiation-bundle 适用场景与选型建议
tomvdpeet/markdown-negotiation-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 160 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 05 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「markdown」 「bundle」 「content-negotiation」 「accept-header」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tomvdpeet/markdown-negotiation-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tomvdpeet/markdown-negotiation-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tomvdpeet/markdown-negotiation-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
2lenet/EasyAdminPlusBundle
The bundle for easy using json-rpc api on your project
Provide a way to secure accesses to all routes of an symfony application.
DMS Meetup API Bundle, enables Meetup API clients in services
Adds more BBCode
统计信息
- 总下载量: 160
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 43
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-05-11