PHP code example of silarhi / llms-txt-bundle

1. Go to this page and download the library: Download silarhi/llms-txt-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

silarhi / llms-txt-bundle example snippets


// config/bundles.php
return [
    // ...
    Silarhi\LlmsTxtBundle\LlmsTxtBundle::class => ['all' => true],
];

use Silarhi\LlmsTxtBundle\Event\LlmsTxtPopulateEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final readonly class LlmsTxtListener
{
    public function __construct(
        private UrlGeneratorInterface $urlGenerator,
        private ProjectRepository $projectRepository,
    ) {
    }

    #[AsEventListener]
    public function __invoke(LlmsTxtPopulateEvent $event): void
    {
        $document = $event->getDocument();

        foreach ($this->projectRepository->findBy(['published' => true]) as $project) {
            $document->addLink(
                'Projets',
                $this->urlGenerator->generate('project_show', ['slug' => $project->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL),
                $project->getName(),
                $project->getSummary(), // optional description
            );
        }
    }
}

use Silarhi\CursorPagination\Configuration\OrderConfiguration;
use Silarhi\CursorPagination\Configuration\OrderConfigurations;
use Silarhi\CursorPagination\Pagination\CursorPagination;

#[AsEventListener]
public function __invoke(LlmsTxtPopulateEvent $event): void
{
    $event->getDocument()->addLinks('Projets', $this->projectLinks());
}

/**
 * @return iterable<Link>
 */
private function projectLinks(): iterable
{
    $queryBuilder = $this->projectRepository->createQueryBuilder('p')
        ->select('p.id, p.slug, p.name, p.summary')
        ->where('p.published = true');

    /** @var CursorPagination<array{id: int, slug: string, name: string, summary: string|null}> $pagination */
    $pagination = new CursorPagination(
        $queryBuilder,
        // the order of the pages; it must be unique: close a non-unique one (a date, a name…) with the id
        new OrderConfigurations(new OrderConfiguration('p.id', static fn (array $project): int => $project['id'], isUnique: true)),
        500,
        // scalar rows: no collection to fetch-join
        fetchJoinCollection: false,
    );

    foreach ($pagination->getResults() as $project) {
        yield new Link(
            $this->urlGenerator->generate('project_show', ['slug' => $project['slug']], UrlGeneratorInterface::ABSOLUTE_URL),
            $project['name'],
            $project['summary'],
        );
    }
}

use Silarhi\LlmsTxtBundle\Model\Section;
use Silarhi\LlmsTxtBundle\Routing\LlmsTxtEntry;

#[Route('/contact', name: 'contact', options: ['llms_txt' => new LlmsTxtEntry(
    title: 'Contact',                      // blic function cgu(): Response
yaml
# config/routes/llms_txt.yaml
llms_txt:
    resource: '@LlmsTxtBundle/config/routes.php'
bash
php bin/console llms-txt:dump
php bin/console llms-txt:dump --base-url=https://example.com   # without "framework.router.default_uri"
php bin/console llms-txt:dump var/llms_txt                     # another directory
yaml
# the Link header of every HTML page
llms_txt:
    discovery:
        link_header: true # Link: <https://example.com/llms.txt>; rel="llms-txt"