如何用laravel生成sitemap

之前用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) ????????????-&gt;header('Content-type',?'text/xml'); ????} ????/** ?????*?Build?the?Site?Map ?????*/ ????protected?function?buildSiteMap($type) ????{ ????????$sitemapInfo?=?[]; ????????switch?($type)?{ ????????????case?'video': ????????????????$sitemapInfo?=?$this-&gt;getVideoInfo(); ????????????????break; ????????????case?'article': ????????????????$sitemapInfo?=?$this-&gt;getArticleInfo(); ????????????????break; ????????????case?'bbs': ????????????????$sitemapInfo?=?$this-&gt;getBbsInfo(); ????????????????break; ????????????case?'ask': ????????????????$sitemapInfo?=?$this-&gt;getAskInfo(); ????????????????break; ????????????case?'series': ????????????????$sitemapInfo?=?$this-&gt;getSeriesInfo();//車型庫 ????????????????break; ????????} ????????$lastmod?=?$sitemapInfo[0]['pub_time']; ????????$xml?=?[]; ????????$xml[]?=?'<?xml  version="1.0" encoding="UTF-8"?&#39; . &#39;>'; ????????$xml[]?=?'<urlset>'; ????????$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>"; ????????????$xml[]?=?"????<lastmod>{$sitemap['pub_time']}</lastmod>"; ????????????$xml[]?=?"??</url>"; ????????} ????????$xml[]?=?'</urlset>'; ????????return?join("n",?$xml); ????} ????/** ?????*?Return?all?the?posts?as?$url?=&gt;?$date ?????*/ ????protected?function?getVideoInfo() ????{ ????????$videos?=?Video::where('pub_time',?'where('published',?2) ????????????-&gt;where('is_del',?0) ????????????-&gt;orderBy('id',?'desc') ????????????-&gt;pluck('pub_time',?'id') ????????????-&gt;all(); ????????$res?=?$article?=?[]; ????????foreach?($videos?as?$id?=&gt;?$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',?'where('published',?2) ????????????-&gt;where('is_del',?0) ????????????-&gt;orderBy('id',?'desc') ????????????-&gt;pluck('pub_time',?'id') ????????????-&gt;take(5000) ????????????-&gt;all(); ????????$res?=?$article?=?[]; ????????foreach?($articles?as?$id?=&gt;?$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) ????????????-&gt;where('is_del',?0) ????????????-&gt;orderBy('id',?'desc') ????????????-&gt;pluck('dateline',?'id') ????????????-&gt;take(10000) ????????????-&gt;all(); ????????$res?=?$article?=?[]; ????????foreach?($articles?as?$id?=&gt;?$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) ????????????-&gt;orderBy('id',?'desc') ????????????-&gt;pluck('dateline',?'id') ????????????-&gt;take(10000) ????????????-&gt;all(); ????????$res?=?$article?=?[]; ????????foreach?($articles?as?$id?=&gt;?$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) ????????????-&gt;where('is_stop',?0) ????????????-&gt;pluck('name',?'id') ????????????-&gt;all(); ????????$res?=?$article?=?[]; ????????foreach?($articles?as?$id?=&gt;?$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教程欄目!

以上就是如何用

? 版權聲明
THE END
喜歡就支持一下吧
點贊10 分享