用laravel生成sitemap

之前用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;
    }
}

点这里有福利:爆款云产品低至2.4折

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王中阳Go

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值