最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

如何用laravel生成sitemap

来源:动视网 责编:小采 时间:2020-11-03 18:20:04
文档

如何用laravel生成sitemap

如何用laravel生成sitemap:之前用yaf和yii框架写过sitemap:思路是根据需求生成.xml文件保存到项目指定目录中。用laravel换一个思路,生成.xml动态链接,而不是保存文件到目录1.配置routes,生成xml访问链接2.根据项目逻辑生成sitemap上代码:配置routes //sitemap R
推荐度:
导读如何用laravel生成sitemap:之前用yaf和yii框架写过sitemap:思路是根据需求生成.xml文件保存到项目指定目录中。用laravel换一个思路,生成.xml动态链接,而不是保存文件到目录1.配置routes,生成xml访问链接2.根据项目逻辑生成sitemap上代码:配置routes //sitemap R


之前用yaf和yii框架写过sitemap:思路是根据需求生成.xml文件保存到项目指定目录中。

用laravel换一个思路,生成.xml动态链接,而不是保存文件到目录

1.配置routes,生成xml访问链接

2.根据项目逻辑生成sitemap

上代码:

配置routes

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

核心代码

<?php
namespace AppHttpControllersM;
use AppHttpControllersBaseController;
use AppModelBbsArticle;
use AppModelBbsAsk;
use AppModelBbsThread;
use AppModelMainVideo;
use AppModelGarageSeriesInfoModel;
//todo 补充其他模块
use CarbonCarbon;
use IlluminateSupportFacadesCache;
class SitemapController extends BaseController
{
 //todo 写一个汇总文件
 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 = [];
 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("
", $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框架相关技术文章,请访问laravel教程栏目!

文档

如何用laravel生成sitemap

如何用laravel生成sitemap:之前用yaf和yii框架写过sitemap:思路是根据需求生成.xml文件保存到项目指定目录中。用laravel换一个思路,生成.xml动态链接,而不是保存文件到目录1.配置routes,生成xml访问链接2.根据项目逻辑生成sitemap上代码:配置routes //sitemap R
推荐度:
标签: 生成 如何 laravel
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top