Skip to content

Instantly share code, notes, and snippets.

@ManiruzzamanAkash
Created January 9, 2024 17:16
Show Gist options
  • Save ManiruzzamanAkash/638d1d0ffe100e58aa56bc530227b990 to your computer and use it in GitHub Desktop.
Save ManiruzzamanAkash/638d1d0ffe100e58aa56bc530227b990 to your computer and use it in GitHub Desktop.
SiteMap Generator PHP - Laravel
<?php
namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\CodeExample;
use App\Models\Course;
use App\Models\CssCode;
use App\Models\Developer;
use App\Models\Page;
use App\Models\Post;
use App\Models\Project;
use App\Models\Service;
use App\Models\Tag;
use Carbon\Carbon;
use Illuminate\Support\Facades\View;
class SitemapController extends Controller
{
public $lastUpdatedAt = null;
public function index()
{
$categories = $this->getCategories();
$tags = $this->getTags();
$articles = array_merge(
$this->getAllPages(),
$this->getAllPosts($categories, $tags),
$this->getAllCodeExaples($categories, $tags),
$this->getAllUsers(),
$this->getAllServices(),
$this->getAllDesigns(),
$this->getAllProjects(),
$this->getAllCourses(),
$this->getAllRootGenericPages()
);
// Unique the array by loc.
$articles = collect($articles)->unique('loc')->toArray();
// Filter out also if lastmod or loc is empty
$articles = array_filter($articles, function ($article) {
return !empty($article['loc']) && !empty($article['lastmod']);
});
$content = '';
$content .= '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$content .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
foreach ($articles as $article) {
$url = strip_tags($article['loc']); // Strip HTML tags from the URL
// Filter out non-UTF-8 characters, see https://stackoverflow.com/a/1401710/3958494
$url = str_replace('&', '&amp;', $url);
$url = preg_replace('/[^\x20-\x7E]/', '', $url);
$content .= "\t" . '<url>' . "\n";
$content .= "\t\t" . '<loc>' . $url . '</loc>' . "\n";
$content .= "\t\t" . '<lastmod>' . $article['lastmod'] . '</lastmod>' . "\n";
$content .= "\t\t" . '<changefreq>weekly</changefreq>' . "\n";
$content .= "\t\t" . '<priority>0.8</priority>' . "\n";
$content .= "\t" . '</url>' . "\n";
}
$content .= '</urlset>';
return response($content)
->withHeaders([
'Content-Type' => 'text/xml'
]);
}
public function getCategories()
{
return Category::select('slug', 'updated_at')->get();
}
public function getTags()
{
return Tag::select('slug', 'updated_at')->get();
}
public function setLastUpdatedAt($lastUpdatedAt): void
{
if ($this->lastUpdatedAt == null) {
$this->lastUpdatedAt = $lastUpdatedAt;
} else {
if ($lastUpdatedAt > $this->lastUpdatedAt) {
$this->lastUpdatedAt = $lastUpdatedAt;
}
}
if ($this->lastUpdatedAt == null) {
$this->lastUpdatedAt = now();
}
$this->lastUpdatedAt = Carbon::parse($this->lastUpdatedAt)->tz('UTC')->toAtomString();
}
/**
* @return <[loc, lastmod]>
*/
public function getAllPosts($categories, $tags, $where = null): array
{
$query = Post::select('slug', 'updated_at', 'post_or_tutorial');
if ($where) {
$query->where($where);
}
$posts = $query->get();
$data = [];
$posts->map(function ($post) use (&$data) {
$type = $post->post_or_tutorial == 0 ? 'blog/' : 'tutorials/';
$data[] = [
'loc' => url($type . $post->slug),
'lastmod' => $post->updated_at->tz('UTC')->toAtomString(),
];
});
$this->setLastUpdatedAt($posts->max('updated_at'));
$data[] = [
'loc' => url('blog'),
'lastmod' => $this->lastUpdatedAt,
];
$data[] = [
'loc' => url('tutorials'),
'lastmod' => $this->lastUpdatedAt,
];
$data[] = [
'loc' => url('tutorials-center'),
'lastmod' => $this->lastUpdatedAt,
];
$data[] = [
'loc' => url('search-tutorial'),
'lastmod' => $this->lastUpdatedAt,
];
$data[] = [
'loc' => url('playlists'),
'lastmod' => $this->lastUpdatedAt,
];
foreach ($categories as $category) {
$data[] = [
'loc' => url('tutorials/category/' . $category->slug),
'lastmod' => $this->lastUpdatedAt,
];
$data[] = [
'loc' => url('blog/category/' . $category->slug),
'lastmod' => $this->lastUpdatedAt,
];
}
foreach ($tags as $tag) {
$data[] = [
'loc' => url('tutorials/tag/' . $tag->slug),
'lastmod' => $this->lastUpdatedAt,
];
$data[] = [
'loc' => url('blog/tag/' . $tag->slug),
'lastmod' => $this->lastUpdatedAt,
];
}
return $data;
}
/**
* @return <[loc, lastmod]>
*/
public function getAllCodeExaples(): array
{
$data = [];
$codeExamples = CodeExample::select('slug', 'updated_at')->get();
$codeExamples->map(function ($codeExample) use (&$data) {
$codeExample->slug = 'example/' . $codeExample->slug;
$data[] = [
'loc' => url($codeExample->slug),
'lastmod' => $codeExample->updated_at->tz('UTC')->toAtomString(),
];
});
$this->setLastUpdatedAt($codeExamples->max('updated_at'));
$data[] = [
'loc' => url('codes'),
'lastmod' => $this->lastUpdatedAt,
];
$data[] = [
'loc' => url('codes/run/code'),
'lastmod' => $this->lastUpdatedAt,
];
return $data;
}
/**
* @return <[loc, lastmod]>
*/
public function getAllPages(): array
{
$data = [];
$pages = Page::select('slug', 'updated_at')->get();
$pages->map(function ($page) use (&$data) {
$data[] = [
'loc' => url('page/' . $page->slug),
'lastmod' => $page->updated_at->tz('UTC')->toAtomString(),
];
});
$this->setLastUpdatedAt($pages->max('updated_at'));
return $data;
}
/**
* @return <[loc, lastmod]>
*/
public function getAllUsers(): array
{
$data = [];
$users = Developer::select('username', 'updated_at')->get();
$users->map(function ($user) use (&$data) {
$data[] = [
'loc' => url('users/' . $user->username),
'lastmod' => $user->updated_at->tz('UTC')->toAtomString(),
];
});
return $data;
}
/**
* @return <[loc, lastmod]>
*/
public function getAllServices(): array
{
$data = [];
$services = Service::select('slug', 'updated_at')->get();
$services->map(function ($service) use (&$data) {
$data[] = [
'loc' => url('services/' . $service->slug),
'lastmod' => $service->updated_at->tz('UTC')->toAtomString(),
];
});
return $data;
}
/**
* @return <[loc, lastmod]>
*/
public function getAllDesigns(): array
{
$data = [];
// Get all frameworks
$frameworks = ['tailwind', 'bootstrap'];
$cssCodes = CssCode::select('framework', 'slug', 'updated_at')->get();
foreach ($frameworks as $framework) {
$frameworkCSSCodes = collect($cssCodes)->where('framework', $framework);
foreach ($frameworkCSSCodes as $cssCode) {
$data[] = [
'loc' => url('design/' . $cssCode->framework . '/' . $cssCode->slug),
'lastmod' => $cssCode->updated_at->tz('UTC')->toAtomString(),
];
$data[] = [
'loc' => url('design/' . $cssCode->framework . '/' . $cssCode->slug . '/view'),
'lastmod' => $cssCode->updated_at->tz('UTC')->toAtomString(),
];
$this->setLastUpdatedAt($cssCode->updated_at);
}
$data[] = [
'loc' => url('design/' . $framework),
'lastmod' => $this->lastUpdatedAt,
];
$data[] = [
'loc' => url('design'),
'lastmod' => $this->lastUpdatedAt,
];
}
return $data;
}
/**
* @return <[loc, lastmod]>
*/
public function getAllProjects(): array
{
$data = [];
// Get all projects
$projects = Project::select('slug', 'updated_at')->get();
$projects->map(function ($project) use (&$data) {
$data[] = [
'loc' => url('portfolio/' . $project->slug),
'lastmod' => $project->updated_at->tz('UTC')->toAtomString(),
];
});
return $data;
}
/**
* @return <[loc, lastmod]>
*/
public function getAllCourses(): array
{
$data = [];
$lastUpdatedAt = null;
// Get all courses
$courses = Course::select('slug', 'updated_at')->get();
$courses->map(function ($course) use (&$data, &$lastUpdatedAt) {
$data[] = [
'loc' => url('course/' . $course->slug),
'lastmod' => $course->updated_at->tz('UTC')->toAtomString(),
];
$lastUpdatedAt = $course->updated_at->tz('UTC')->toAtomString();
});
$data[] = [
'loc' => url('course'),
'lastmod' => $lastUpdatedAt,
];
return $data;
}
/**
* @return <[loc, lastmod]>
*/
public function getAllRootGenericPages(): array
{
$data[] = [
'loc' => url('/'),
'lastmod' => $this->lastUpdatedAt,
];
$data[] = [
'loc' => url('about-us'),
'lastmod' => $this->lastUpdatedAt,
];
$data[] = [
'loc' => url('contact'),
'lastmod' => $this->lastUpdatedAt,
];
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment