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

How to generate sitemap with laravel

藏色散人
Release: 2020-01-15 14:39:23
forward
3476 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

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

Core code

<?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:
source:csdn.net
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