laravel 网站地图轮子

https://github.com/Laravelium/laravel-sitemap

add the following to your composer.json file :

For Laravel 5.7

"laravelium/sitemap": "3.0.*"

For Laravel 5.6

"laravelium/sitemap": "2.8.*"

For Laravel 5.5

"laravelium/sitemap": "2.7.*"

Publish needed assets (styles, views, config files) :

php artisan vendor:publish --provider="Laravelium\Sitemap\SitemapServiceProvider"

Note: Composer won't update them after composer update, you'll need to do it manually!

public function sitemap(Request $request) {
        // create new sitemap object
        $sitemap = App::make("sitemap");
        $sitemap->setCache('laravel.sitemap', 60);
        // get all posts from db
        $posts = News::where('audit_status', 0)
            ->orderBy('id', 'desc')
            ->get();
        // add every post to the sitemap
        foreach ($posts as $post) {

            $sitemap->add(getenv('APP_URL')."/news/".$post->id.'.html', $post->created_at, 0.9, 'monthly');
        }

        $sitemap->add(getenv('APP_URL')."/news.html", $post->created_at, 1, 'monthly');

        $xml=$sitemap->render('xml');

//        header('Content-Type: application/xml');
//        header('Content-Disposition: attachment;filename="sitemap.xml"');
        $path=public_path()."\sitemap.xml";
        $path=str_replace('\\','/',$path);

        return file_put_contents($path,$xml->getContent());
    }
posted @ 2018-12-27 18:53  心之所依  阅读(687)  评论(0编辑  收藏  举报