first change

This commit is contained in:
2025-07-27 17:40:56 +05:45
commit f8b9a6725b
3152 changed files with 229528 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
<?php
namespace Modules\Sitemap\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Spatie\Crawler\Crawler;
use Spatie\Sitemap\SitemapGenerator;
use Psr\Http\Message\UriInterface;
use Modules\Sitemap\Models\SitemapConfig;
class SitemapController extends Controller
{
public function index()
{
$path = public_path('sitemap.xml');
if (!file_exists($path)) {
return response('Sitemap not found.', 404);
}
return response()->file($path, [
'Content-Type' => 'application/xml',
]);
}
public function generate(Request $request)
{
$path = public_path('sitemap.xml');
$sitemapConfig = SitemapConfig::active()->latest()->first();
$sitemapExcludes = $sitemapConfig->sitemap_excludes;
$ignoreRobots = $sitemapConfig->ignore_robot;
if (is_string($sitemapExcludes)) {
$sitemapExcludes = array_map('trim', explode(',', $sitemapExcludes));
}
$generator = SitemapGenerator::create(config('app.url'))
->configureCrawler(function (Crawler $crawler) use ($ignoreRobots, $sitemapConfig) {
if ($ignoreRobots) {
$crawler->ignoreRobots();
}
if ($sitemapConfig->max_depth) {
$crawler->setMaximumDepth($sitemapConfig->max_depth);
}
})
->shouldCrawl(function (UriInterface $url) use ($sitemapExcludes) {
$path = $url->getPath();
foreach ($sitemapExcludes as $exclude) {
if (str_starts_with($path, $exclude)) {
return false;
}
}
return true;
});
if ($sitemapConfig->max_crawl_count) {
$generator->setMaximumCrawlCount($sitemapConfig->max_crawl_count);
}
$generator->writeToFile($path);
if (!file_exists($path)) {
return response('Failed to generate sitemap.', 404);
}
return response()->file($path, [
'Content-Type' => 'application/xml',
]);
}
}