怎么用laravel生成sitemap
这篇文章给大家分享的是有关怎么用laravel生成sitemap的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

成都创新互联长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为广水企业提供专业的成都网站设计、网站制作、外贸营销网站建设,广水网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。
之前用yaf和yii框架写过sitemap:思路是根据需求生成.xml文件保存到项目指定目录中。
用laravel换一个思路,生成.xml动态链接,而不是保存文件到目录
1.配置routes,生成xml访问链接
2.根据项目逻辑生成sitemap
上代码:
配置routes
//sitemap
Route::get('/sitemap/m/{type}.xml', 'SitemapController@siteMap');核心代码
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;
}
}感谢各位的阅读!关于“怎么用laravel生成sitemap”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
文章名称:怎么用laravel生成sitemap
当前地址:http://www.jxjierui.cn/article/ipgogh.html


咨询
建站咨询
