之前用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适合快速构建Web应用,而Python适用于更广泛的应用场景。 1.Laravel提供EloquentORM、Blade模板引擎和Artisan工具,简化Web开发。 2.Python以动态类型、丰富的标准库和第三方生态系统着称,适用于Web开发、数据科学等领域。

Laravel和Python各有优势:Laravel适合快速构建功能丰富的Web应用,Python在数据科学和通用编程领域表现出色。1.Laravel提供EloquentORM和Blade模板引擎,适合构建现代Web应用。2.Python拥有丰富的标准库和第三方库,Django和Flask框架满足不同开发需求。

Laravel值得选择,因为它能使代码结构清晰,开发过程更具艺术性。1)Laravel基于PHP,遵循MVC架构,简化Web开发。2)其核心功能如EloquentORM、Artisan工具和Blade模板增强了开发的优雅与健壮性。3)通过路由、控制器、模型和视图,开发者能高效构建应用。4)队列和事件监听等高级功能进一步提升应用性能。

Laravel不仅是后端框架,还是完整的Web开发解决方案。它提供了强大的后端功能,如路由、数据库操作、用户认证等,并支持前端开发,提升了整个Web应用的开发效率。

Laravel适合Web开发,Python适用于数据科学和快速原型开发。 1.Laravel基于PHP,提供优雅的语法和丰富功能,如EloquentORM。 2.Python以简洁着称,广泛应用于Web开发和数据科学,拥有丰富的库生态系统。

laravelcanbeeffectefection ininreal-worldapplications forbuildingscalablewebsolutions.1)ITSImplifieCrudoperationsInrestfulaPisusingEloquentorm.2)laravel'secosystem,包括Toolslikenova,包括Toolslikenova,增强功能

Laravel在后端开发中的核心功能包括路由系统、EloquentORM、迁移功能、缓存系统和队列系统。1.路由系统简化了URL映射,提高了代码组织和维护性。2.EloquentORM提供了面向对象的数据操作,提升了开发效率。3.迁移功能通过版本控制管理数据库结构,确保一致性。4.缓存系统减少数据库查询,提升响应速度。5.队列系统有效处理大规模数据,避免阻塞用户请求,提升整体性能。

Laravel在后端开发中表现强大,通过EloquentORM简化数据库操作,控制器和服务类处理业务逻辑,并提供队列、事件等功能。1)EloquentORM通过模型映射数据库表,简化查询。2)业务逻辑在控制器和服务类中处理,提高模块化和可维护性。3)其他功能如队列系统帮助处理复杂需求。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

WebStorm Mac版
好用的JavaScript开发工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中