Laravel动态生成sitemap
什么是Sitemap?Sitemap的作用是什么?
Sitemap 可方便网站管理员通知搜索引擎他们网站上有哪些可供抓取的网页。最简单的 Sitemap 形式,就是XML 文件,在其中列出网站中的网址以及关于每个网址的其他元数据(上次更新的时间、更改的频率以及相对于网站上其他网址的重要程度为何等),以便搜索引擎可以更加智能地抓取网站。
之前用yaf和yii框架写过sitemap:思路是根据需求生成.xml文件保存到项目指定目录中。
今天用laravel换一个思路实现:生成.xml动态链接,而不是保存文件到目录。
核心思路
- 配置routes,生成xml访问链接
- 根据项目逻辑生成sitemap
上代码:
- 配置routes
//sitemap
Route::get('/sitemap/m/{type}.xml', 'SitemapController@siteMap');
- 核心代码
<?php
class SitemapController extends BaseController
{
//写一个汇总文件
public function siteMap($type)
{
$cacheKey = "site-" . $type;
//2小时缓存 保证加载速度
if (Cache::has($cacheKey)) {
$siteMap = Cache::get($cacheKey);
} else {
$siteMap = $this->buildSiteMap($type);
Cache::add($cacheKey, $siteMap, 120);
}
return response($siteMap)
->header('Content-type', 'text/xml');
}
/**
* Build the Site Map
*/
protected function buildSiteMap($type)
{
$sitemapInfo = [];
//多模块的sitemap分类使用Switch case分离
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 version="1.0" encoding="UTF-8"?' . '>';
$xml[] = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">';
$xml[] = ' <url>';
$xml[] = " <loc>https://m.xxx.com</loc>";
$xml[] = " <lastmod>$lastmod</lastmod>";
$xml[] = ' <changefreq>daily</changefreq>';
$xml[] = ' <priority>0.8</priority>';
$xml[] = ' </url>';
foreach ($sitemapInfo as $sitemap) {
$xml[] = ' <url>';
$xml[] = " <loc>{$sitemap['url']}</loc>";
$xml[] = " <mobile:mobile type=\"mobile\"/>";
$xml[] = " <lastmod>{$sitemap['pub_time']}</lastmod>";
$xml[] = " </url>";
}
$xml[] = '</urlset>';
return join("\n", $xml);
}
//根据需求取出需要的字段...
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()
{
...
}
protected function getBbsInfo()
{
...
}
protected function getAskInfo()
{
...
}
protected function getSeriesInfo()
{
...
}
}
关键注释已经在代码段中说明,不再赘述。
分享一下网站SEO的坑
- 使用VUE搭建的动态网站SEO非常不友好,因为VUE是单页面应用(SPA),不利于百度等爬虫爬取各个子页面的数据。
- 如何解决这个问题呢?推荐使用Nuxt.js框架,Nuxt.js基于VUE,上手比较容易;支持服务器端渲染,对SEO友好。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanfbakk
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01