generate dynamic sitemaps in a laravel project without using composer

前端 未结 4 1266
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 13:30

I want to generate Dynamic sitemap for my laravel project. I had already searched in so many sites for an answer. some of them describes it using composer. I di

4条回答
  •  一向
    一向 (楼主)
    2020-12-14 14:16

    check https://github.com/RoumenDamianoff/laravel-sitemap

    A simple sitemap generator for Laravel 4.

    Route::get('sitemap', function(){
    
        // create new sitemap object
        $sitemap = App::make("sitemap");
    
        // set cache (key (string), duration in minutes (Carbon|Datetime|int), turn on/off (boolean))
        // by default cache is disabled
        $sitemap->setCache('laravel.sitemap', 3600);
    
        // check if there is cached sitemap and build new only if is not
        if (!$sitemap->isCached())
        {
             // add item to the sitemap (url, date, priority, freq)
             $sitemap->add(URL::to('/'), '2012-08-25T20:10:00+02:00', '1.0', 'daily');
             $sitemap->add(URL::to('page'), '2012-08-26T12:30:00+02:00', '0.9', 'monthly');
    
             // get all posts from db
             $posts = DB::table('posts')->orderBy('created_at', 'desc')->get();
    
             // add every post to the sitemap
             foreach ($posts as $post)
             {
                $sitemap->add($post->slug, $post->modified, $post->priority, $post->freq);
             }
        }
    
        // show your sitemap (options: 'xml' (default), 'html', 'txt', 'ror-rss', 'ror-rdf')
        return $sitemap->render('xml');
    
    });
    

    i have used it. works great!

    update #1

    to clear the confusion, let's take an example.

    let's say i have a blog table with id, title, blog

    i have the route as, Route::get('blog/{blog}',['as' => 'blog.show', 'uses' => 'Blog@show'];

    first i will fetch the content, by $blogs = DB::table('blog')->get();

    $blogs will contain the results.

    i will just loop,

    foreach($blogs as $i)
    {
         $sitemap->add(route('blog.show',[$i->title]), '2014-11-11', '1.0','daily');
    }
    

    all my blogs are added in the sitemap.

提交回复
热议问题