简体   繁体   中英

Site map generator in laravel 5 with RoumenDianoff sitemap

Im implementing the laravel RoumenDianoff Sitemap in my application

https://github.com/RoumenDamianoff/laravel-sitemap/wiki/Dynamic-sitemap

And im really confused on this part of the code

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)

$sitemap->setCache('laravel.sitemap', 60);

$posts = DB::table('posts')->orderBy('created_at', 'desc')->get();


     foreach ($posts as $post)
     {
        $sitemap->add($post->slug, $post->modified, $post->priority, $post->freq);
     }

As I understand i'm creating a route to use sitemap as that function so the part I don't get is when do I iterate trough every link of my website, or how do i get this links from my website to add then to the for each on that function, i mean it looks like that $posts variable but I don't have record of my links on any database so how can i get this links.

What I would do is generate the links dynamically (probably using values from the database). Then create a sitemap object that will be populated with all the links (use foreach loop) and finally store this. Check out the code fragment below:

Route::get('sitemap',function(){

    // Generate your links dynamically here
    $link = [];
    $locations = DB::locations(); /** some database values **/

    foreach($locations as $location){
        $link = route('home') . '/shoes-for-sale-in-' . strtolower($location);

        // create new sitemap object
        $sitemap = \App::make("sitemap");

        // Add link, priority,last modified and change frequency
        $sitemap->add($link, null, 0.5, 'monthly');

    }

    // generate sitemap (format, filename)
    $sitemap->store('xml', 'commercial');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM