用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全部内容,希望文章能够帮你解决如何用laravel生成sitemap所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)