Home > PHP Framework > Laravel > How to generate sitemap with laravel

How to generate sitemap with laravel

藏色散人
Release: 2020-01-15 14:39:23
forward
3575 people have browsed it

I have written sitemap using yaf and yii frameworks before: the idea is to generate .xml files based on requirements and save them in the project-specified directory.

Use laravel to change the idea and generate .xml dynamic link instead of saving the file to the directory

1. Configure routes and generate xml access link

2. Generate sitemap based on project logic

Code:

Configuration routes

1

2

//sitemap

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

Copy after login

Core code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

<?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(&#39;Content-type&#39;, &#39;text/xml&#39;);

    }

    /**

     * Build the Site Map

     */

    protected function buildSiteMap($type)

    {

        $sitemapInfo = [];

        switch ($type) {

            case &#39;video&#39;:

                $sitemapInfo = $this->getVideoInfo();

                break;

            case &#39;article&#39;:

                $sitemapInfo = $this->getArticleInfo();

                break;

            case &#39;bbs&#39;:

                $sitemapInfo = $this->getBbsInfo();

                break;

            case &#39;ask&#39;:

                $sitemapInfo = $this->getAskInfo();

                break;

            case &#39;series&#39;:

                $sitemapInfo = $this->getSeriesInfo();//车型库

                break;

        }

        $lastmod = $sitemapInfo[0][&#39;pub_time&#39;];

        $xml = [];

        $xml[] = &#39;<?xml version="1.0" encoding="UTF-8"?&#39; . &#39;>&#39;;

        $xml[] = &#39;<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">&#39;;

        $xml[] = &#39;  <url>&#39;;

        $xml[] = "    <loc>https://m.xxx.com</loc>";

        $xml[] = "    <lastmod>$lastmod</lastmod>";

        $xml[] = &#39;    <changefreq>daily</changefreq>&#39;;

        $xml[] = &#39;    <priority>0.8</priority>&#39;;

        $xml[] = &#39;  </url>&#39;;

        foreach ($sitemapInfo as $sitemap) {

            $xml[] = &#39;  <url>&#39;;

            $xml[] = "    <loc>{$sitemap[&#39;url&#39;]}</loc>";

            $xml[] = "    <mobile:mobile type=\"mobile\"/>";

            $xml[] = "    <lastmod>{$sitemap[&#39;pub_time&#39;]}</lastmod>";

            $xml[] = "  </url>";

        }

        $xml[] = &#39;</urlset>&#39;;

        return join("\n", $xml);

    }

    /**

     * Return all the posts as $url => $date

     */

    protected function getVideoInfo()

    {

        $videos = Video::where(&#39;pub_time&#39;, &#39;<=&#39;, Carbon::now())

            ->where(&#39;published&#39;, 2)

            ->where(&#39;is_del&#39;, 0)

            ->orderBy(&#39;id&#39;, &#39;desc&#39;)

            ->pluck(&#39;pub_time&#39;, &#39;id&#39;)

            ->all();

        $res = $article = [];

        foreach ($videos as $id => $pub_time) {

            $article[&#39;id&#39;] = $id;

            $article[&#39;pub_time&#39;] = substr($pub_time, 0, 10);

            $article[&#39;url&#39;] = "https://m.xxx.com/video_" . $id . ".html";

            $res[] = $article;

        }

        return $res;

    }

    protected function getArticleInfo()

    {

        $articles = Article::where(&#39;pub_time&#39;, &#39;<=&#39;, Carbon::now())

            ->where(&#39;published&#39;, 2)

            ->where(&#39;is_del&#39;, 0)

            ->orderBy(&#39;id&#39;, &#39;desc&#39;)

            ->pluck(&#39;pub_time&#39;, &#39;id&#39;)

            ->take(5000)

            ->all();

        $res = $article = [];

        foreach ($articles as $id => $pub_time) {

            $article[&#39;id&#39;] = $id;

            $article[&#39;pub_time&#39;] = substr($pub_time, 0, 10);

            $article[&#39;url&#39;] = "https://m.xxx.com/news/article_" . $id . ".html";

            $res[] = $article;

        }

        return $res;

    }

    protected function getBbsInfo()

    {

        $articles = Thread::where(&#39;visible&#39;, 1)

            ->where(&#39;is_del&#39;, 0)

            ->orderBy(&#39;id&#39;, &#39;desc&#39;)

            ->pluck(&#39;dateline&#39;, &#39;id&#39;)

            ->take(10000)

            ->all();

        $res = $article = [];

        foreach ($articles as $id => $pub_time) {

            $article[&#39;id&#39;] = $id;

            $article[&#39;pub_time&#39;] = substr($pub_time, 0, 10);

            $article[&#39;url&#39;] = "https://m.xxx.com/bbs/thread_" . $id . ".html";

            $res[] = $article;

        }

        return $res;

    }

    protected function getAskInfo()

    {

        $articles = Ask::where(&#39;state&#39;, 1)

            ->orderBy(&#39;id&#39;, &#39;desc&#39;)

            ->pluck(&#39;dateline&#39;, &#39;id&#39;)

            ->take(10000)

            ->all();

        $res = $article = [];

        foreach ($articles as $id => $pub_time) {

            $article[&#39;id&#39;] = $id;

            $article[&#39;pub_time&#39;] = substr($pub_time, 0, 10);

            $article[&#39;url&#39;] = "https://m.xxx.com/ask_" . $id . ".html";

            $res[] = $article;

        }

        return $res;

    }

    //车型库

    protected function getSeriesInfo()

    {

        $articles = SeriesInfoModel::where(&#39;status&#39;, 1)

            ->where(&#39;is_stop&#39;, 0)

            ->pluck(&#39;name&#39;, &#39;id&#39;)

            ->all();

        $res = $article = [];

        foreach ($articles as $id => $pub_time) {

            $article[&#39;id&#39;] = $id;

            $article[&#39;pub_time&#39;] = date(&#39;Y-m-d&#39;, time());

            $article[&#39;url&#39;] = "https://m.xxx.com/series/" . $id . "/details";

            $res[] = $article;

        }

        return $res;

    }

}

Copy after login

For more technical articles related to the laravel framework, please visit the laravel tutorial column!

The above is the detailed content of How to generate sitemap with laravel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template