0

0

如何用laravel生成sitemap

藏色散人

藏色散人

发布时间:2020-01-15 14:39:23

|

3544人浏览过

|

来源于csdn

转载

之前用yaf和yii框架写过sitemap:思路是根据需求生成.xml文件保存到项目指定目录中。

laravel换一个思路,生成.xml动态链接,而不是保存文件到目录

1.配置routes,生成xml访问链接

2.根据项目逻辑生成sitemap

上代码:

Glif
Glif

Glif.app 是一个有趣的AI沙盒工具,用于创建名为 glifs 的微型AI生成器,例如自拍生成器、Meme梗图、表情包、漫画、故事等

下载

配置routes

    //sitemap
    Route::get('/sitemap/m/{type}.xml', 'SitemapController@siteMap');

核心代码

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[] = '';
        $xml[] = '  ';
        $xml[] = "    https://m.xxx.com";
        $xml[] = "    $lastmod";
        $xml[] = '    daily';
        $xml[] = '    0.8';
        $xml[] = '  ';
        foreach ($sitemapInfo as $sitemap) {
            $xml[] = '  ';
            $xml[] = "    {$sitemap['url']}";
            $xml[] = "    ";
            $xml[] = "    {$sitemap['pub_time']}";
            $xml[] = "  ";
        }
        $xml[] = '';
        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组件介绍
laravel组件介绍

laravel 提供了丰富的组件,包括身份验证、模板引擎、缓存、命令行工具、数据库交互、对象关系映射器、事件处理、文件操作、电子邮件发送、队列管理和数据验证。想了解更多laravel的相关内容,可以阅读本专题下面的文章。

316

2024.04.09

laravel中间件介绍
laravel中间件介绍

laravel 中间件分为五种类型:全局、路由、组、终止和自定。想了解更多laravel中间件的相关内容,可以阅读本专题下面的文章。

275

2024.04.09

laravel使用的设计模式有哪些
laravel使用的设计模式有哪些

laravel使用的设计模式有:1、单例模式;2、工厂方法模式;3、建造者模式;4、适配器模式;5、装饰器模式;6、策略模式;7、观察者模式。想了解更多laravel的相关内容,可以阅读本专题下面的文章。

369

2024.04.09

thinkphp和laravel哪个简单
thinkphp和laravel哪个简单

对于初学者来说,laravel 的入门门槛较低,更易上手,原因包括:1. 更简单的安装和配置;2. 丰富的文档和社区支持;3. 简洁易懂的语法和 api;4. 平缓的学习曲线。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

371

2024.04.10

laravel入门教程
laravel入门教程

本专题整合了laravel入门教程,想了解更多详细内容,请阅读专题下面的文章。

81

2025.08.05

laravel实战教程
laravel实战教程

本专题整合了laravel实战教程,阅读专题下面的文章了解更多详细内容。

64

2025.08.05

laravel面试题
laravel面试题

本专题整合了laravel面试题相关内容,阅读专题下面的文章了解更多详细内容。

67

2025.08.05

pdf怎么转换成xml格式
pdf怎么转换成xml格式

将 pdf 转换为 xml 的方法:1. 使用在线转换器;2. 使用桌面软件(如 adobe acrobat、itext);3. 使用命令行工具(如 pdftoxml)。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

1885

2024.04.01

PS使用蒙版相关教程
PS使用蒙版相关教程

本专题整合了ps使用蒙版相关教程,阅读专题下面的文章了解更多详细内容。

23

2026.01.19

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Laravel---API接口
Laravel---API接口

共7课时 | 0.6万人学习

PHP自制框架
PHP自制框架

共8课时 | 0.6万人学习

PHP面向对象基础课程(更新中)
PHP面向对象基础课程(更新中)

共12课时 | 0.7万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号