之前用yaf和yii框架写过sitemap:思路是根据需求生成.xml文件保存到项目指定目录中。
用laravel换一个思路,生成.xml动态链接,而不是保存文件到目录
1.配置routes,生成xml访问链接
2.根据项目逻辑生成sitemap
上代码:
配置routes
//sitemap route::get('/sitemap/m/{type}.xml', 'sitemapcontroller@sitemap');
核心代码
<?phpnamespace app\http\controllers\m;use app\http\controllers\basecontroller;use app\model\bbs\article;use app\model\bbs\ask;use app\model\bbs\thread;use app\model\main\video;use app\model\garage\seriesinfomodel;//todo 补充其他模块use carbon\carbon;use illuminate\support\facades\cache;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("\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框架相关技术文章,请访问laravel教程栏目!
以上就是如何用laravel生成sitemap的详细内容。