Home>Article>PHP Framework> How to generate sitemap with laravel

How to generate sitemap with laravel

藏色散人
藏色散人 forward
2020-01-15 14:39:23 3354browse

I have written sitemap using yaf and yii frameworks before: the idea is to generate .xml files based on requirements and save them in the project-specified directory.

Use laravel to change the idea and generate .xml dynamic link instead of saving the file to the directory

1. Configure routes and generate xml access link

2. Generate sitemap based on project logic

Code:

Configuration routes

//sitemap Route::get('/sitemap/m/{type}.xml', 'SitemapController@siteMap');

Core code

buildSiteMap($type); Cache::add($cacheKey, $siteMap, 120); } return response($siteMap) ->header('Content-type', 'text/xml'); } /** * Build the Site Map */ protected function buildSiteMap($type) { $sitemapInfo = []; switch ($type) { case 'video': $sitemapInfo = $this->getVideoInfo(); break; case 'article': $sitemapInfo = $this->getArticleInfo(); break; case 'bbs': $sitemapInfo = $this->getBbsInfo(); break; case 'ask': $sitemapInfo = $this->getAskInfo(); break; case 'series': $sitemapInfo = $this->getSeriesInfo();//车型库 break; } $lastmod = $sitemapInfo[0]['pub_time']; $xml = []; $xml[] = ''; $xml[] = ''; $xml[] = ' '; $xml[] = " https://m.xxx.com"; $xml[] = " $lastmod"; $xml[] = ' daily'; $xml[] = ' 0.8'; $xml[] = ' '; foreach ($sitemapInfo as $sitemap) { $xml[] = ' '; $xml[] = " {$sitemap['url']}"; $xml[] = " "; $xml[] = " {$sitemap['pub_time']}"; $xml[] = " "; } $xml[] = ''; return join("\n", $xml); } /** * Return all the posts as $url => $date */ protected function getVideoInfo() { $videos = Video::where('pub_time', '<=', Carbon::now()) ->where('published', 2) ->where('is_del', 0) ->orderBy('id', 'desc') ->pluck('pub_time', 'id') ->all(); $res = $article = []; foreach ($videos as $id => $pub_time) { $article['id'] = $id; $article['pub_time'] = substr($pub_time, 0, 10); $article['url'] = "https://m.xxx.com/video_" . $id . ".html"; $res[] = $article; } return $res; } protected function getArticleInfo() { $articles = Article::where('pub_time', '<=', Carbon::now()) ->where('published', 2) ->where('is_del', 0) ->orderBy('id', 'desc') ->pluck('pub_time', 'id') ->take(5000) ->all(); $res = $article = []; foreach ($articles as $id => $pub_time) { $article['id'] = $id; $article['pub_time'] = substr($pub_time, 0, 10); $article['url'] = "https://m.xxx.com/news/article_" . $id . ".html"; $res[] = $article; } return $res; } protected function getBbsInfo() { $articles = Thread::where('visible', 1) ->where('is_del', 0) ->orderBy('id', 'desc') ->pluck('dateline', 'id') ->take(10000) ->all(); $res = $article = []; foreach ($articles as $id => $pub_time) { $article['id'] = $id; $article['pub_time'] = substr($pub_time, 0, 10); $article['url'] = "https://m.xxx.com/bbs/thread_" . $id . ".html"; $res[] = $article; } return $res; } protected function getAskInfo() { $articles = Ask::where('state', 1) ->orderBy('id', 'desc') ->pluck('dateline', 'id') ->take(10000) ->all(); $res = $article = []; foreach ($articles as $id => $pub_time) { $article['id'] = $id; $article['pub_time'] = substr($pub_time, 0, 10); $article['url'] = "https://m.xxx.com/ask_" . $id . ".html"; $res[] = $article; } return $res; } //车型库 protected function getSeriesInfo() { $articles = SeriesInfoModel::where('status', 1) ->where('is_stop', 0) ->pluck('name', 'id') ->all(); $res = $article = []; foreach ($articles as $id => $pub_time) { $article['id'] = $id; $article['pub_time'] = date('Y-m-d', time()); $article['url'] = "https://m.xxx.com/series/" . $id . "/details"; $res[] = $article; } return $res; } }

For more technical articles related to the laravel framework, please visit thelaravel tutorialcolumn!

The above is the detailed content of How to generate sitemap with laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete