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

如何用laravel生成sitemap

藏色散人
藏色散人转载
2020-01-15 14:39:233469浏览

之前用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删除