PHP code example of leafs / sitemap

1. Go to this page and download the library: Download leafs/sitemap library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

leafs / sitemap example snippets


    sitemap()->generate();
    

app()->get('/about', [
    'sitemap' => [
        'changefreq' => 'monthly',
        'priority' => 0.5,
    ],
    function() {
        echo 'About Us';
    }
]);

app()->get('/blog/{slug}', [
    'sitemap' => [
        'changefreq' => 'weekly',
        'priority' => 0.8,
        'model' => \App\Models\Post::class, // or 'posts' if you don't have a model but have a table named 'posts',
        'parameter' => 'slug', // the parameter in the route to replace with the model value,
        'exclude' => [
            'status' => 'draft' // you can also exclude certain items from the sitemap based on model attributes
        ]
    ],
    'BlogController@show'
]);

sitemap()->source(function() {
    // Fetch blog posts from the database
    $posts = db()->select('posts')->get(); // or with a model like Post::all();

    // Add each post to the sitemap
    foreach ($posts as $post) {
        sitemap()->map('/blog/{slug}', "/blog/{$post->slug}", [
            'lastmod' => $post->updated_at,
            'changefreq' => 'weekly',
            'priority' => 0.8,
        ]);
    }
});

// original route
app()->get('/blog/{slug}', 'BlogController@show');

sitemap()->source(function() {
    // Fetch blog posts from the database
    $posts = db()->select('posts')->get(); // or with a model like Post::all();

    // Add each post to the sitemap
    foreach ($posts as $post) {
        sitemap()->map('/blog/{slug}', [
            "/en/blog/{$post->slug_en}" => [
                'lastmod' => $post->updated_at,
                'changefreq' => 'weekly',
                'priority' => 0.8,
            ],
            "/fr/blog/{$post->slug_fr}" => [
                'lastmod' => $post->updated_at,
                'changefreq' => 'weekly',
                'priority' => 0.8,
            ],
        ]);
    }
});

// original route
app()->get('/blog/{slug}', 'BlogController@show');

sitemap()->source(function() {
    // Fetch blog posts from the database
    $posts = db()->select('posts')->get(); // or with a model like Post::all();

    // Add each post to the sitemap
    foreach ($posts as $post) {
        sitemap()->add("/blog/{$post->slug}", [
            'lastmod' => $post->updated_at,
            'changefreq' => 'weekly',
            'priority' => 0.8,
        ]);
    }
});

sitemap()->source(function() {
    sitemap()->add('/about', [
        'changefreq' => 'monthly',
        'priority' => 0.5,
    ]);

    sitemap()->add('/contact', [
        'changefreq' => 'monthly',
        'priority' => 0.5,
    ]);
});

sitemap()->source(function() {
    ...

    sitemap()->add('/about', [
        'changefreq' => 'monthly',
        'priority' => 0.5,
    ]);
});

app()->get('/about', function() {
    echo 'About Us';
});