PHP code example of schaefersoft / laravel-llms-txt

1. Go to this page and download the library: Download schaefersoft/laravel-llms-txt 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/ */

    

schaefersoft / laravel-llms-txt example snippets


// app/Providers/AppServiceProvider.php

use SchaeferSoft\LaravelLlmsTxt\LlmsTxt;

public function boot(): void
{
    LlmsTxt::configure(fn ($llms) => $llms
        ->title('My App')
        ->description('A short description of what this site offers.')
        ->section('Services', fn ($s) => $s
            ->entry('Web Development', 'https://example.com/web', 'Laravel & Vue.js')
            ->entry('Hosting', 'https://example.com/hosting', 'Managed hosting')
        )
    );
}

LlmsTxt::make()
    ->title('My App')
    ->description('Short tagline for the site.');

LlmsTxt::make()
    ->title(fn () => __('llms.title'))
    ->description(fn () => __('llms.description'));

use SchaeferSoft\LaravelLlmsTxt\Entry;
use SchaeferSoft\LaravelLlmsTxt\Section;

$section = Section::create('Services')
    ->addEntry(Entry::create('Web Development', 'https://example.com/web', 'Laravel & Vue.js'))
    ->addEntry(Entry::create('Hosting', 'https://example.com/hosting'));

LlmsTxt::make()
    ->title('My App')
    ->addSection($section);

LlmsTxt::make()
    ->title('My App')
    ->section('Services', fn ($s) => $s
        ->entry('Web Development', 'https://example.com/web', 'Laravel & Vue.js')
        ->entry('Hosting', 'https://example.com/hosting', 'Managed hosting')
    )
    ->section('References', fn ($s) => $s
        ->entry('Projects', 'https://example.com/references', 'All client projects')
    );

LlmsTxt::configure(fn ($llms) => $llms
    ->title('My App')
    ->section('Services', fn ($s) => $s
        ->entries(Service::published()->get(), fn ($service) => Entry::create(
            $service->name,
            route('services.show', $service),
            $service->tagline,
        ))
    )
);

->section('Blog', fn ($s) => $s
    ->entry('All Posts', route('blog.index'))
    ->entries(Post::featured()->get(), fn ($post) => Entry::create(
        $post->title,
        route('blog.show', $post),
    ))
)

LlmsTxt::make()
    ->title('My App')
    ->section('Services', fn ($s) => $s
        ->entry('Web Development', 'https://example.com/web')
        ->when((bool) config('features.shop'), fn ($s) => $s
            ->entry('Shop', 'https://example.com/shop')
        )
    )
    ->when((bool) config('features.api'), fn ($llms) => $llms
        ->section('API', fn ($s) => $s
            ->entry('API Docs', 'https://example.com/api')
        )
    );

->when(fn () => Feature::active('shop'), fn ($s) => $s
    ->entry('Shop', 'https://example.com/shop')
)

// config/llms-txt.php
'exclude_routes' => [
    'admin/*',        // URI wildcard: excludes /admin and everything below
    'legal/imprint',  // exact URI
    'internal.*',     // route-name wildcard
],

// config/llms-txt.php
'register_routes' => false,

// routes/web.php
use SchaeferSoft\LaravelLlmsTxt\LlmsTxt;

Route::middleware(['web', 'cache.headers:public;max_age=3600'])
    ->group(function () {
        LlmsTxt::routes();
    });

LlmsTxt::configure(fn ($llms) => $llms
    ->title(__('llms.title'))
    ->description(__('llms.description'))
    ->section(__('llms.sections.services'), fn ($s) => $s
        ->entry(
            __('llms.entries.web_dev'),
            route('services.web'),
            __('llms.entries.web_dev_desc'),
        )
    )
);

// lang/en/llms.php
return [
    'title'       => 'My App',
    'description' => 'Software agency',
    'sections'    => ['services' => 'Services'],
    'entries'     => [
        'web_dev'      => 'Web Development',
        'web_dev_desc' => 'Modern web applications with Laravel & Vue.js',
    ],
];

// lang/de/llms.php
return [
    'title'       => 'Meine App',
    'description' => 'Software-Agentur',
    'sections'    => ['services' => 'Leistungen'],
    'entries'     => [
        'web_dev'      => 'Webentwicklung',
        'web_dev_desc' => 'Moderne Webanwendungen mit Laravel & Vue.js',
    ],
];

// config/llms-txt.php
'locales'          => ['en', 'de'],
'localize_routes'  => true,

// config/llms-txt.php
'register_routes' => false,

// routes/web.php
Route::group(
    ['prefix' => LaravelLocalization::setLocale(), 'middleware' => ['localize']],
    function () {
        LlmsTxt::routes();
    }
);

// Write to the default location
LlmsTxt::make()->title('My App')->writeToDisk();

// Write to a custom path
LlmsTxt::make()->title('My App')->writeToDisk('custom/path/llms.txt');

// Write with a locale prefix (writes to de/llms.txt)
LlmsTxt::make()->title('My App')->locale('de')->writeToDisk();

// Write the full version
LlmsTxt::make()->title('My App')->writeFullToDisk();

// Render as a string
$content = LlmsTxt::make()
    ->title('My App')
    ->section('Docs', fn ($s) => $s
        ->entry('API', 'https://example.com/api')
    )
    ->renderFull();

// Write directly to disk
LlmsTxt::make()->title('My App')->writeFullToDisk();

// config/llms-txt.php
'full_route_enabled' => true,

// Use the default cache key ('llms-txt')
$content = LlmsTxt::make()->title('My App')->getCached();

// Use a custom cache key
$content = LlmsTxt::make()->title('My App')->getCached('my-custom-key');

// Flush ALL package cache keys (base keys + locale variants used by the routes)
LlmsTxt::make()->flushCache();

// Flush a single custom key
LlmsTxt::make()->flushCache('my-custom-key');
bash
php artisan vendor:publish --tag=llms-txt-config
bash
php artisan llms:generate --full
bash
php artisan llms:clear