溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用laravel生成sitemap

發(fā)布時(shí)間:2021-03-05 10:51:03 來源:億速云 閱讀:211 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)怎么用laravel生成sitemap的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

之前用yaf和yii框架寫過sitemap:思路是根據(jù)需求生成.xml文件保存到項(xiàng)目指定目錄中。

用laravel換一個(gè)思路,生成.xml動(dòng)態(tài)鏈接,而不是保存文件到目錄

1.配置routes,生成xml訪問鏈接

2.根據(jù)項(xiàng)目邏輯生成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 補(bǔ)充其他模塊
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
class SitemapController extends BaseController
{
    //todo 寫一個(gè)匯總文件
    public function siteMap($type)
    {
        $cacheKey = "site-" . $type;
        //2小時(shí)緩存 保證加載速度
        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;
    }
}

感謝各位的閱讀!關(guān)于“怎么用laravel生成sitemap”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI