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 .= '' . "\n"; $content .= '' . "\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('&', '&', $url); $url = preg_replace('/[^\x20-\x7E]/', '', $url); $content .= "\t" . '' . "\n"; $content .= "\t\t" . '' . $url . '' . "\n"; $content .= "\t\t" . '' . $article['lastmod'] . '' . "\n"; $content .= "\t\t" . 'weekly' . "\n"; $content .= "\t\t" . '0.8' . "\n"; $content .= "\t" . '' . "\n"; } $content .= ''; 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; } }