silarhi / llms-txt-bundle
Build, dump and serve an llms.txt file from your Symfony application, the PrestaSitemapBundle way.
Package info
github.com/silarhi/llms-txt-bundle
Type:symfony-bundle
pkg:composer/silarhi/llms-txt-bundle
Requires
- php: >=8.2
- psr/link: ^1.1 || ^2.0
- symfony/config: ^6.4 || ^7.0 || ^8.0
- symfony/event-dispatcher: ^6.4 || ^7.0 || ^8.0
- symfony/event-dispatcher-contracts: ^2.5 || ^3.0
- symfony/filesystem: ^6.4 || ^7.0 || ^8.0
- symfony/http-foundation: ^6.4 || ^7.0 || ^8.0
- symfony/http-kernel: ^6.4 || ^7.0 || ^8.0
- symfony/routing: ^6.4 || ^7.0 || ^8.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.58.0
- friendsofphp/php-cs-fixer: ^3.95.32
- nikic/php-parser: ^5.9.0
- phpstan/extension-installer: ^1.4.3
- phpstan/phpstan: ^2.3.7
- phpstan/phpstan-strict-rules: ^2.1.2
- phpstan/phpstan-symfony: ^2.1.1
- phpunit/phpunit: ^11.5.57 || ^12.5.38 || ^13.4.1
- rector/rector: ^2.9.2
- shipmonk/dead-code-detector: ^1.4.2
- symfony/browser-kit: ^6.4 || ^7.0 || ^8.0
- symfony/console: ^6.4 || ^7.0 || ^8.0
- symfony/css-selector: ^6.4 || ^7.0 || ^8.0
- symfony/dependency-injection: ^6.4 || ^7.0 || ^8.0
- symfony/error-handler: ^6.4 || ^7.0 || ^8.0
- symfony/framework-bundle: ^6.4 || ^7.0 || ^8.0
- symfony/stopwatch: ^6.4 || ^7.0 || ^8.0
- symfony/twig-bundle: ^6.4 || ^7.0 || ^8.0
- symfony/web-link: ^6.4 || ^7.0 || ^8.0
- twig/twig: ^2.13 || ^3.0.4
Suggests
- shipmonk/dead-code-detector: Keep the #[AsLlmsTxtLinkProvider] methods from being reported as dead: include vendor/silarhi/llms-txt-bundle/shipmonk.neon
- silarhi/cursor-pagination: Reads the large Doctrine result sets of LlmsTxtPopulateEvent listeners by keyset pages, with a flat memory
- symfony/console: Required for the llms-txt:dump command
- symfony/framework-bundle: Register Silarhi\LlmsTxtBundle\LlmsTxtBundle for the configuration, the controllers, the listeners and the commands wired (optional: without it, build the generator, the renderers and the dumpers by hand, without symfony/dependency-injection)
- symfony/twig-bundle: Required for the llms_txt_url() and llms_txt_link() Twig functions
- symfony/web-link: Required for the Link header of the discovery.link_header option
Provides
None
Conflicts
Replaces
None
README
LLMs.txt Bundle
Build, dump and serve an llms.txt file from your Symfony application.
The PrestaSitemapBundle way: listeners fill it, a controller serves it, a command dumps it.
📖 Documentation
Features
- Link providers: a
#[AsLlmsTxtLinkProvider(section: 'Products')]method returns the links of a section, from your repositories - Event driven: listeners of
LlmsTxtPopulateEventchange the whole document, likeSitemapPopulateEvent - Route options: static pages join the file with an
llms_txtroute option, like thesitemapone - On the fly or dumped: the controller serves the dumped file when there is one, and builds it otherwise
- Flat memory: links added as generators are read one at a time, streamed to the response or the dumped file
- llms-full.txt: the Markdown of the pages, next to
llms.txt, from thecontentof their links - Markdown versions: a route accepting the
mdformat serves its page atpage.md, and to the agents asking fortext/markdown - Checks:
debug:llms-txtshows where the links come from, and finds duplicates, relative URLs, a file too large - Spec compliant: H1 title, blockquote summary, H2 sections of links, the
Optionalsection always last, escaped links - Discovery: a
<link>tag for the<head>of your pages and an opt-inLinkheader, through WebLink
Requirements
- PHP 8.2+
- Symfony 6.4, 7.x or 8.x
symfony/framework-bundleto register the bundle: the package does not install it, and the generator, the renderers and the dumpers work without it, see Without the bundle
Installation
composer require silarhi/llms-txt-bundle
Import the route serving /llms.txt:
# config/routes/llms_txt.yaml llms_txt: resource: '@LlmsTxtBundle/config/routes.php'
Without the bundle
An application without the framework or symfony/dependency-injection builds the services by hand, from an event
dispatcher and the router (the llms_txt route options):
use Silarhi\LlmsTxtBundle\Controller\LlmsTxtController; use Silarhi\LlmsTxtBundle\Event\LlmsTxtPopulateEvent; use Silarhi\LlmsTxtBundle\EventListener\RouteOptionsListener; use Silarhi\LlmsTxtBundle\LlmsTxt; use Silarhi\LlmsTxtBundle\Service\Dumper; use Silarhi\LlmsTxtBundle\Service\FullMarkdownRenderer; use Silarhi\LlmsTxtBundle\Service\Generator; use Silarhi\LlmsTxtBundle\Service\MarkdownRenderer; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Filesystem\Filesystem; $dispatcher = new EventDispatcher(); $dispatcher->addSubscriber(new RouteOptionsListener($router, 'Pages')); // the routes carrying an "llms_txt" option $dispatcher->addListener(LlmsTxtPopulateEvent::class, static function (LlmsTxtPopulateEvent $event) use ($products): void { $event->getDocument()->addLinks('Products', $products->links()); // a generator of Link, read while writing }); $generator = new Generator($dispatcher, title: 'SILARHI', summary: 'Web and mobile applications.'); // public/llms.txt, and public/llms-full.txt from the same listeners (new Dumper($generator, new MarkdownRenderer(), new Filesystem(), __DIR__ . '/public', LlmsTxt::FILENAME))->dump(); (new Dumper($generator, new FullMarkdownRenderer(), new Filesystem(), __DIR__ . '/public', LlmsTxt::FULL_FILENAME))->dump(); // or serve it: the dumped file when there is one, built on the fly otherwise $controller = new LlmsTxtController($generator, new MarkdownRenderer(), __DIR__ . '/public', 'text/plain; charset=UTF-8', 3600, LlmsTxt::FILENAME, true); $response = $controller($request);
DumpCommand (with symfony/console), LlmsTxtExtension (with twig/twig) and MarkdownVersionListener are built
the same way. What needs a container stays with the bundle class: the configuration, the #[AsLlmsTxtLinkProvider]
attribute (add a LinkProviderListener to the dispatcher instead), the routes of routes.php, the cache warmers and
Twig's escaping of the *.md.twig templates.
Quick start
# config/packages/llms_txt.yaml llms_txt: title: 'SILARHI' summary: 'Web and mobile applications, from design to maintenance.'
use Silarhi\LlmsTxtBundle\Routing\LlmsTxtEntry; #[Route('/contact', name: 'contact', options: ['llms_txt' => new LlmsTxtEntry(title: 'Contact')])] public function contact(): Response
GET /llms.txt now lists the contact page. Add the pages of your database from
a link provider, or dump the file with
php bin/console llms-txt:dump.
Documentation
Everything else is on llms-txt-bundle.silarhi.dev:
- Installation and configuration reference
- Adding links: link providers, event listeners, generators for large sections, route options
- llms-full.txt and Markdown versions of the pages
- Serving the file, on the fly or dumped, and checking it
- Discovery:
<link>tag andLinkheader
Contributing
See CONTRIBUTING.md. Release notes are in CHANGELOG.md.
License
Released under the MIT License.