123movies-seo/app/Services/SitemapGenerator.php

28 lines
976 B
PHP

<?php
namespace App\Services;
use samdark\sitemap\Sitemap;
use App\Services\ApiClient;
use App\Supports\Traits\CleanItems;
class SitemapGenerator {
use CleanItems;
public function generate(ApiClient $api)
{
$sitemap = new Sitemap(public_path('sitemap.xml'));
$movies = $this->formatApiResponse($api->getTrendingMovies(1, 500), false)['data'];
$shows = $this->formatApiResponse($api->getTrendingShows(1, 500), false)['data'];
foreach ($movies as $key => $movie) {
if ($movie['slug'] === '') continue;
$sitemap->addItem(route('movie', ['id' => $movie['id'], 'slug' => $movie['slug']]), time(), Sitemap::DAILY, 0.9);
}
foreach ($shows as $key => $show) {
if ($show['slug'] === '') continue;
$sitemap->addItem(route('show', ['id' => $show['id'], 'slug' => $show['slug']]), time(), Sitemap::DAILY, 0.9);
}
$sitemap->write();
// dd($movies);
}
}