Laravel向けllms.txt
Laravel の public/ ディレクトリは Web ルートなので、設定は不要です。動的生成する場合は routes/web.php にルートを追加するか、Laravel の Cache facade を使う専用コントローラーを追加します。
最終更新:
オプション 1:public/ 内の静的ファイル
Laravelの public/ ディレクトリはnginxまたはApacheが配信するドキュメントルートです。そこに置いたファイルは、ルーティング設定なしで
対応するURLパスから利用できます。これは最も簡単な方法で、コード変更は一切必要ありません。
# Laravel static file approach
#
# Laravel's public/ directory is the web root (served by nginx/Apache).
# Drop your file here and it is immediately available at /llms.txt.
#
# Project structure:
# your-laravel-app/
# ├── public/
# │ ├── index.php
# │ └── llms.txt ← add this
# ├── routes/
# └── app/
#
# No code change needed. Works on Forge, Vapor, Heroku, and bare VPS.
この方法は、Laravelのすべてのデプロイ先で利用できます: Laravel Forge, Laravel Vapor, Heroku, Railwayおよびベア VPS サーバーです。Web サーバー(nginx/Apache)が PHP を介さずにファイルを直接配信するため、より高速でもあります。
オプション2:routes/web.php のルート
コンテンツをプログラムで生成したり、ファイルではなくコードから管理したりしたい場合は、
に名前付きルートを追加してください。 routes/web.php:
<?php
// routes/web.php, serve llms.txt via a named route
use Illuminate\Support\Facades\Route;
Route::get('/llms.txt', function () {
$content = <<<'LLMS'
# Your Site
> One-sentence description of what your site or product does.
## Documentation
- [Getting Started](https://yoursite.com/docs/start): Install and configure in minutes.
- [API Reference](https://yoursite.com/docs/api): Full endpoint catalog with examples.
## Product
- [Overview](https://yoursite.com/product): Core features and capabilities.
- [Pricing](https://yoursite.com/pricing): Plans and billing details.
## Optional
- [Changelog](https://yoursite.com/changelog): Release history.
LLMS;
return response($content, 200)
->header('Content-Type', 'text/plain; charset=utf-8')
->header('Cache-Control', 'public, max-age=3600, stale-while-revalidate=86400');
})->name('llms-txt');
PHPのheredoc構文(<<<'LLMS')を使えば、エスケープを気にせず内容をインラインで記述できます。単一引用符付きのヒアドキュメントマーカーでは変数展開が行われず、内容はリテラル文字列として扱われます。
選択肢3:キャッシュ付きコントローラー
データベースから動的に生成されるコンテンツ(例:公開済みの
ドキュメントページの取得)については、専用の呼び出し可能なコントローラーを使用し、 Cache::remember() リクエストごとにデータベースクエリを実行することを避けるために:
<?php
// app/Http/Controllers/LlmsTxtController.php
namespace App\Http\Controllers;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;
class LlmsTxtController extends Controller
{
public function __invoke(): Response
{
// Cache for 1 hour, regenerates automatically when expired
$content = Cache::remember('llms_txt', 3600, function () {
return $this->buildContent();
});
return response($content, 200)
->header('Content-Type', 'text/plain; charset=utf-8')
->header('Cache-Control', 'public, max-age=3600, stale-while-revalidate=86400');
}
private function buildContent(): string
{
// You can query your database here:
// $docs = \App\Models\Doc::published()->get();
// $links = $docs->map(fn($d) => "- [{$d->title}](https://yoursite.com/docs/{$d->slug}): {$d->summary}")->join("\n");
return <<<'LLMS'
# Your Site
> One-sentence description of your product.
## Documentation
- [Getting Started](https://yoursite.com/docs/start): Install and configure in minutes.
- [API Reference](https://yoursite.com/docs/api): Full endpoint catalog with examples.
## Optional
- [Changelog](https://yoursite.com/changelog): Release history.
LLMS;
}
}
<?php
// routes/web.php, register the controller route
use App\Http\Controllers\LlmsTxtController;
use Illuminate\Support\Facades\Route;
Route::get('/llms.txt', LlmsTxtController::class)->name('llms-txt');
Cache::remember() 設定されたキャッシュドライバー(Redis、Memcached、 データベース、またはファイル)に結果を保存します。キャッシュは3600秒後に自動的に再生成されます。コンテンツの更新直後に
キャッシュを無効化するには、 Cache::forget('llms_txt') モデルのオブザーバー内、またはデプロイメントフックの後に実行してください。
再利用可能にするレスポンスマクロ
複数のプレーンテキストファイルを提供する場合、またはアプリケーション全体で一貫したパターンを確保したい場合は、
以下を登録してください。 plaintext() response マクロ内 AppServiceProvider:
<?php
// app/Providers/AppServiceProvider.php, response macro for reusability
namespace App\Providers;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
// Register a plaintext() macro for serving text/plain responses
Response::macro('plaintext', function (string $content, int $maxAge = 3600) {
return Response::make($content, 200, [
'Content-Type' => 'text/plain; charset=utf-8',
'Cache-Control' => "public, max-age={$maxAge}, stale-while-revalidate=86400",
]);
});
}
}
// Usage in a route or controller:
// return response()->plaintext($content);
このマクロは、アプリケーション内のどこからでも次を介して利用できます: response()->plaintext($content)、 Content-Type および Cache-Control ヘッダーの一貫性を保ちます。
検証
デプロイ後、ファイルが正しく配信されていることを確認します:
curl -I https://yoursite.com/llms.txt
# Expected:
# HTTP/2 200
# content-type: text/plain; charset=utf-8
# cache-control: public, max-age=3600
curl https://yoursite.com/llms.txt | head -5
# Should print: # Your Site 次に URL を llms.txt バリデーター で仕様への完全な準拠を確認します。
リリース前のチェックリスト
- ファイルの配信先:
/llms.txtで200 OK. Content-Type: text/plain; charset=utf-8が設定されていること。Cache-Controlヘッダーが存在します。- レスポンスはプレーンテキストで、HTML も Blade の出力もありません。
- 先頭に H1 見出しがちょうど1つあること。
- H1の直後にブロッククォートの要約を配置する。
- すべてのURLは絶対パスである(
https://). - この
/llms.txtルートに配置します。 - バリデーターでエラーが返されないこと: llmtxt.info/validator/
関連ガイド
- llms.txtの作成方法、テンプレート、チェックリスト。
- llms.txt フォーマットリファレンス、仕様の詳細。
- ヘッドレスCMS向けllms.txt、Contentful、Sanity、Strapi。
- ベストプラクティス、含めるものと省くもの。
- バリデータ · ジェネレーター.