ARCANEDEV / LaravelSitemap

A sitemap generator for Laravel.
MIT License
12 stars 5 forks source link

How can i manage an already existing sitemap! #18

Closed vkyeswa closed 5 years ago

arcanedev-maroc commented 5 years ago

The existing sitemap was generated by this package ? If it's not, you can't manage it.

vkyeswa commented 5 years ago

the sitemap was generated by the package, though i would like to read the number of urls in the sitemap in my dashboard. Can it load a sitemap instance basing on the sitemap it already created coz currently $sitemap = SiteMap::make()->setPath($filepath); $sitemap->getUrls(); returns empty even if the path is to an and already generated siteMap

arcanedev-maroc commented 5 years ago

NOTE: This package doesn't have a read from file feature.

The only solution is to extract/create a class that has a single method: Register all the urls

For example:

<?php namespace App;

use Arcanedev\LaravelSitemap\Contracts\SitemapManager;
use Arcanedev\LaravelSitemap\Entities\Sitemap;
use Arcanedev\LaravelSitemap\Entities\Url;

class SitemapRegistrar
{
    /**
     * @return \Arcanedev\LaravelSitemap\Contracts\SitemapManager
     */
    public static function load()
    {
        return tap(sitemap(), function (SitemapManager $manager) {
            // Pages urls
            $manager->add('pages', tap(new Sitemap, function (Sitemap $sitemap) {
                $baseUrl = 'http://example.com';
                $lastMod = '2019-01-01 00:00:00';

                $sitemap->setPath("{$baseUrl}/sitemap-pages.xml");
                $sitemap->add(Url::make("{$baseUrl}/")->setLastMod($lastMod));
                $sitemap->add(Url::make("{$baseUrl}/about-us")->setLastMod($lastMod));
                $sitemap->add(Url::make("{$baseUrl}/contact")->setLastMod($lastMod));
            }));

            // Other urls
        });
    }
}

After that you can do whatever you want with the loaded sitemap manager:

<?php

use App\SitemapRegistrar;
use Illuminate\Support\Facades\Route;

Route::get('save-sitemap', function () {
    SitemapRegistrar::load()->save(base_path('sitemap.xml'));

    return 'The sitemap.xml was saved.';
});

Route::get('dashboard', function () {
    $manager = SitemapRegistrar::load();

    dump(
        $manager->all(),
        $manager->get('pages')->getUrls()
    );
});

I hope this will help you :+1: