
إضافة خريطة موقع (Sitemap) إلى تطبيق Laravel خطوة مهمة لتُعرّف محرّكات البحث بمحتواك وتعزّز تحسين محرّكات البحث.
في هذا المقال سنستخدم إحدى أكثر الحِزم شيوعاً لإنشاء خرائط المواقع في Laravel: spatie/laravel-sitemap.
سنتعلّم ثلاثة أساليب مختلفة لإنشاء الخرائط:
- خريطة موقع واحدة (Single Sitemap)
- إنشاء تلقائي لخريطة الموقع (Auto-Generating Sitemap)
- فهرس خرائط الموقع (Sitemap Index)
لننشئ المتحكّم بالأمر التالي: php artisan make:controller SitemapController
. ثم نثبّت الحزمة بتشغيل: composer require spatie/laravel-sitemap
.
والآن لنبدأ!
خريطة موقع واحدة
تولّد هذه الدالة خريطة موقع لنموذج واحد باسم Content
. نذهب إلى SitemapController
الذي أنشأناه ونضيف السطر التالي قبل تعريف الصنف:
use Spatie\Sitemap\Sitemap;
ثم نكتب داخل الصنف الدالة التالية، مع تعليقات تشرح كل سطر:
public function firstSitemap(Request $request) {
// Create Sitemap instance
$contentSitemap = Sitemap::create();
// Get All the published content
$contents= Content::where('published',1)->orderBy('created_at', 'desc')->get();
// Loop
foreach($contents as $content) {
// Write the public url of this content which is in our case localhost/content/{id}
$url = url('content/'.$content->id);
// Add the url to the sitemap
$contentSitemap->add($url);
}
// generate the sitemap and save it on the desk
$contentSitemap->writeToDisk('public', 'sitemap_content.xml');
// generating a success message
$request->session()->flash('alert-success', 'sitemap generated successfully');
// return to the first page
return redirect()->route('home');
}
للتنفيذ نضيف المسار التالي إلى web.php
:
Route::get('/first_sitemap', [SitemapController::class, 'firstSitemap'])->name('sitemap.first');
عند زيارة هذا المسار ستُولَّد في مجلد public
ملف باسم sitemap_content.xml
يحوي كل روابط المحتوى.
إنشاء خريطة الموقع تلقائياً
لا توفّر الحزمة طريقة مضمّنة للتوليد التلقائي (أو ربما لم أعثر عليها). لكن يمكننا تحقيق ذلك بإرجاع الملف الذي تم إنشاؤه للتو. لا أوصي بهذه الطريقة للخرائط الكبيرة. إليك الدالة:
public function firstSitemap() {
// Create Sitemap instance
$contentSitemap = Sitemap::create();
// Get All the published content
$contents= Content::where('published',1)->orderBy('created_at', 'desc')->get();
// Loop
foreach($contents as $content) {
// Write the public url of this content which is in our case localhost/content/{id}
$url = url('content/'.$content->id);
// Add the url to the sitemap
$contentSitemap->add($url);
}
// generate the sitemap and save it on the desk
$contentSitemap->writeToDisk('public', 'sitemap_content.xml');
// return the above generated sitemap file
return \File::get(public_path() . '/sitemap_content.xml');
}
فهرس خرائط الموقع (Sitemap index)
يكون مفيداً إذا كان لديك عدة نماذج وتريد إنشاء خريطة موقع لكل منها، ثم توليد فهرس يضمّها جميعاً.
ننشىء أولاً عدّة خرائط كما في القسم الأول (مثلاً sitemap_content.xml
وsitemap_poems.xml
). ثم نولّد فهرس الخرائط كالتالي:
قبل تعريف الصنف نستدعي الصنف التالي: use Spatie\Sitemap\SitemapIndex;
ثم نضيف الدالة:
public function sitemap() {
// Create a sitemap index instance
$sitemapIndex = SitemapIndex::create()
// Add the sitemaps by writing there path in the public folder
->add('/sitemap_content.xml')
->add('/sitemap_poems.xml');
// generate the sitemap index and save it on the desk
$sitemapIndex->writeToDisk('public', 'sitemap_index.xml');
// generating a success message
$request->session()->flash('alert-success', 'sitemap index generated successfully');
// return to the first page
return redirect()->route('home');
}
وهذا كل شيء. آمل أن يكون المقال مفيداً!