llms.txt لـ Laravel
دليل public/ في Laravel هو جذر الويب ولا يحتاج إلى إعداد. وللإنشاء الديناميكي، أضف مسارًا في routes/web.php أو وحدة تحكم مخصصة تستخدم واجهة Cache في Laravel.
آخر تحديث:
الخيار 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 العادية. يقدّم خادم الويب (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');
استخدم صيغة heredoc في PHP (<<<'LLMS') لكتابة المحتوى مضَمّناً من دون
القلق بشأن الهروب. وتعني علامة heredoc ذات الاقتباس المفرد عدم استبدال المتغيرات، إذ يُعامل
المحتوى كسلسلة حرفية.
الخيار 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() ماكرو الاستجابة في 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 واحد بالضبط في الأعلى.
- ملخص الاقتباس مباشرةً بعد H1.
- كل عناوين URL مطلقة (
https://). - لم تُطبَّق برمجية وسيطة للمصادقة على
/llms.txtالمسار. - يعيد المدقق بلا أخطاء: llmtxt.info/validator/
أدلة ذات صلة
- كيفية إنشاء llms.txt, والقوالب وقائمة الفحص.
- مرجع تنسيق llms.txt, تفاصيل المواصفة.
- llms.txt لنظام إدارة محتوى بلا واجهةوContentful وSanity وStrapi.
- أفضل الممارسات، وما ينبغي تضمينه وما ينبغي تخطيه.
- المدقّق · المولّد.