之前用yaf和yii框架寫過sitemap:想法是根據需求產生.xml檔保存到專案指定目錄中。
用laravel換一個思路,生成.xml動態鏈接,而不是保存文件到目錄
1.配置routes,產生xml訪問鏈接
2.根據專案邏輯產生sitemap
上程式碼:
設定routes
//sitemap Route::get('/sitemap/m/{type}.xml', 'SitemapController@siteMap');
核心程式碼
<?php namespace 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的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Laravel在後端開發中的核心功能包括路由系統、EloquentORM、遷移功能、緩存系統和隊列系統。 1.路由系統簡化了URL映射,提高了代碼組織和維護性。 2.EloquentORM提供了面向對象的數據操作,提升了開發效率。 3.遷移功能通過版本控制管理數據庫結構,確保一致性。 4.緩存系統減少數據庫查詢,提升響應速度。 5.隊列系統有效處理大規模數據,避免阻塞用戶請求,提升整體性能。

Laravel在後端開發中表現強大,通過EloquentORM簡化數據庫操作,控制器和服務類處理業務邏輯,並提供隊列、事件等功能。 1)EloquentORM通過模型映射數據庫表,簡化查詢。 2)業務邏輯在控制器和服務類中處理,提高模塊化和可維護性。 3)其他功能如隊列系統幫助處理複雜需求。

選擇Laravel開發項目是因為其靈活性和強大功能適應不同規模和復雜度的需求。 Laravel提供路由系統、EloquentORM、Artisan命令行等功能,支持從簡單博客到復雜企業級系統的開發。

Laravel和Python在開發環境和生態系統上的對比如下:1.Laravel的開發環境簡單,僅需PHP和Composer,提供了豐富的擴展包如LaravelForge,但擴展包維護可能不及時。 2.Python的開發環境也簡單,僅需Python和pip,生態系統龐大,涵蓋多個領域,但版本和依賴管理可能複雜。

Laravel是如何在後端邏輯中發揮作用的?它通過路由系統、EloquentORM、認證與授權、事件與監聽器以及性能優化來簡化和增強後端開發。 1.路由系統允許定義URL結構和請求處理邏輯。 2.EloquentORM簡化數據庫交互。 3.認證與授權系統便於用戶管理。 4.事件與監聽器實現松耦合代碼結構。 5.性能優化通過緩存和隊列提高應用效率。

Laravel受歡迎的原因包括其簡化開發過程、提供愉快的開發環境和豐富的功能。 1)它吸收了RubyonRails的設計理念,結合PHP的靈活性。 2)提供瞭如EloquentORM、Blade模板引擎等工具,提高開發效率。 3)其MVC架構和依賴注入機制使代碼更加模塊化和可測試。 4)提供了強大的調試工具和性能優化方法,如緩存系統和最佳實踐。

Django和Laravel都是全棧框架,Django適合Python開發者和復雜業務邏輯,Laravel適合PHP開發者和優雅語法。 1.Django基於Python,遵循“電池齊全”哲學,適合快速開發和高並發。 2.Laravel基於PHP,強調開發者體驗,適合小型到中型項目。

PHP和Laravel不是直接可比的,因為Laravel是基於PHP的框架。 1.PHP適合小型項目或快速原型開發,因其簡單直接。 2.Laravel適合大型項目或高效開發,因其提供豐富功能和工具,但學習曲線較陡,性能可能不如純PHP。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。