首頁 >php框架 >Laravel >如何用laravel生成sitemap

如何用laravel生成sitemap

藏色散人
藏色散人轉載
2020-01-15 14:39:233464瀏覽

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

更多laravel框架相關技術文章,請造訪laravel教學專欄!

以上是如何用laravel生成sitemap的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除