Cara membuat sitemap dengan Laravel Spatie
Install Package Spatie-sitemap
1. Buka terminal pada project Laravel anda lalu jalankan perintah berikut
composer require spatie/laravel-sitemap
Disitu nanti akan terinstall paket laravel sitemapnya
2. Anda dapat mengganti opsi default untuk crawler. Pertama publikasikan konfigurasinya:
php artisan vendor:publish --provider="Spatie\Sitemap\SitemapServiceProvider" --tag=sitemap-config
Perintah tersebut akan mengkopi konfigurasi default ke file config/sitemap.php dimana kamu akan bisa mengedit sesuai keinginan.
use GuzzleHttp\RequestOptions; use Spatie\Sitemap\Crawler\Profile; return [ /* * These options will be passed to GuzzleHttp\Client when it is created. * For in-depth information on all options see the Guzzle docs: * * http://docs.guzzlephp.org/en/stable/request-options.html */ 'guzzle_options' => [ /* * Whether or not cookies are used in a request. */ RequestOptions::COOKIES => true, /* * The number of seconds to wait while trying to connect to a server. * Use 0 to wait indefinitely. */ RequestOptions::CONNECT_TIMEOUT => 10, /* * The timeout of the request in seconds. Use 0 to wait indefinitely. */ RequestOptions::TIMEOUT => 10, /* * Describes the redirect behavior of a request. */ RequestOptions::ALLOW_REDIRECTS => false, ], /* * The sitemap generator can execute JavaScript on each page so it will * discover links that are generated by your JS scripts. This feature * is powered by headless Chrome. */ 'execute_javascript' => false, /* * The package will make an educated guess as to where Google Chrome is installed. * You can also manually pass it's location here. */ 'chrome_binary_path' => '', /* * The sitemap generator uses a CrawlProfile implementation to determine * which urls should be crawled for the sitemap. */ 'crawl_profile' => Profile::class, ];
3. Buat konfigurasi sitemap
Konfigurasi sitemap di file route/web.php agar lebih mudah diaksesnya. Buat konfigurasi route seperti berikut
// sitemap for SEO Route::get('/sitemap', function(){ // buat halaman index utama, disini saya contohkan ada 4 halaman, home, course, event dan artikel $sitemap = Sitemap::create() ->add(Url::create('/')) ->add(Url::create('/course')) ->add(Url::create('/event')) ->add(Url::create('/artikel')); // Buat konten yang terpublish untuk ditampilkan pada sitemap $post = Course::wherePublishment('published')->get(); foreach ($post as $post) { $sitemap->add(Url::create("/course/{$post->slug}")); } $event = Event::wherePublishment('published')->get(); foreach($event as $item){ $sitemap->add(Url::create("/event/{$item->slug}")); } $article = Article::wherePublishment('published')->get(); foreach($article as $item){ $sitemap->add(Url::create("/article/{$item->slug}")); } $sitemap->writeToFile(public_path('sitemap.xml')); return "Sitemap berhasil diperbarui, <a href='https://namasitus.com/sitemap.xml'>Lihat sitemap</a>"; });
Akhirnya pembuatan sitemap telah selesai, silahkan buka file /sitemap untuk melihat hasilnya. Kode diatas akan menggenerate file public/sitemap.xml yang bisa kita masukkan pada google search console kita.
Cukup mudah bukan? Cara membuat sitemap dengan laravel spatie?
Sumber :
https://github.com/spatie/laravel-sitemap
