Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jpeterson579's avatar

Create dynamic sitemap in laravel project

Hi there. I have a laravel project where users can submit blog posts. I want to be able to create a sitemap that automatically adds these posts to it when a new user creates one.

Can someone recommend a way to do this easily>

0 likes
3 replies
nikocraft's avatar

I don't think you can do that easy. You can't update sitemap with latest post by adding them to the sitemap. You need to query database eachtime you want to read sitemap.

Here is how I've done sitemap:

class SitemapController extends Controller
{

    protected $cacheTime;
    public function __construct()
    {
        $setting = Setting::where('name', 'sitemap_cache')->first();
        $this->cacheTime = (int)$setting->body;
    }

    public function index()
    {
        $album = Album::published()->orderBy('published_at', 'desc')->first();
        $image = Image::public()->orderBy('created_at', 'desc')->first();
        $user = User::orderBy('created_at', 'desc')->first();
        $tag = Tag::orderBy('created_at', 'desc')->first();

        return response()->view('sitemap.index', [
            'album' => $album,
            'image' => $image,
            'user' => $user,
            'tag' => $tag,
        ])->header('Content-Type', 'text/xml');
    }

    public function albums()
    {
        $albums = Cache::remember('sitemap-albums', $this->cacheTime,  function() {
            return Album::published()->with('images')->orderBy('published_at', 'desc')->limit(50000)->get();
        });

        return response()->view('sitemap.albums', [
            'albums' => $albums,
        ])->header('Content-Type', 'text/xml');
    }

    public function images()
    {
        $images = Cache::remember('sitemap-images', $this->cacheTime,  function() {
            return Image::public()->orderBy('created_at', 'desc')->limit(50000)->get();
        });

        return response()->view('sitemap.images', [
            'images' => $images,
        ])->header('Content-Type', 'text/xml');
    }

    public function users()
    {
        $users = Cache::remember('sitemap-users', $this->cacheTime,  function() {
            return User::orderBy('created_at', 'desc')->limit(50000)->get();
        });

        return response()->view('sitemap.users', [
            'users' => $users,
        ])->header('Content-Type', 'text/xml');
    }

    public function tags()
    {
        $tags = Cache::remember('sitemap-users', $this->cacheTime,  function() {
            return Tag::orderBy('created_at', 'desc')->get();
        });

        return response()->view('sitemap.tags', [
            'tags' => $tags,
        ])->header('Content-Type', 'text/xml');
    }

}

Routes

 Route::get('/sitemap.xml', 'SitemapController@index');
 Route::group(['prefix' => 'sitemap'], function () {
    Route::get('albums.xml', ['as' => 'sitemap.albums', 'uses' => 'SitemapController@albums']);
    Route::get('images.xml', ['as' => 'sitemap.images', 'uses' => 'SitemapController@images']);
    Route::get('users.xml', ['as' => 'sitemap.users', 'uses' => 'SitemapController@users']);
    Route::get('tags.xml', ['as' => 'sitemap.tags', 'uses' => 'SitemapController@tags']);
 });
2 likes
merwinpoulose's avatar

You can use Laravel to generate sitemaps with out using any packages.

Step 1: Use route file to serve sitemap index and other sitemaps urlset files.

Step 2: Create a Controller to process data and render the XML view.

Step 3: Create View files to render the XML Sitemap.

Step 4: Submit your Sitemap for indexing.

You can refer this link: Create XML Sitemap with index file in php.

Please or to participate in this conversation.