PHP code example of otas / sitemap

1. Go to this page and download the library: Download otas/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/ */

    

otas / sitemap example snippets


return [
    // Base URL for all sitemap links (falls back to app.url if null)
    'website_url' => null,

    // Subdomain prefix (e.g. 'www', 'shop')
    'subdomain' => null,

    // Optional base path for multi-tenant sitemap file storage
    'base_path' => null,

    // Default locale — used for x-default hreflang and locale prefixing
    'default_locale' => 'en',

    // Static pages (see Static Links section)
    'static_links' => [],

    // Model classes implementing SitemapContract (see Dynamic Links section)
    'dynamic_links' => [],

    // Whether to load the package's built-in /sitemap route
    'load_routes' => true,

    // Route prefix for the sitemap endpoint
    'package_routes_prefix' => 'api',

    // Subdirectory for published route files
    'routes_publish_subdirectory' => '',
];

'static_links' => [
    // Homepage
    [
        'loc' => '',
        'other_locs' => ['ar'],
        'alternates' => [
            ['hreflang' => 'en', 'href' => ''],
            ['hreflang' => 'ar', 'href' => 'ar'],
            ['hreflang' => 'x-default', 'href' => ''],
        ],
        'priority' => '1.0',
    ],

    // About page
    [
        'loc' => 'en/about',
        'other_locs' => [],
        'alternates' => [
            ['hreflang' => 'en', 'href' => 'en/about'],
            ['hreflang' => 'ar', 'href' => 'عن-الشركة'],
        ],
        'priority' => '0.8',
    ],

    // Contact page
    [
        'loc' => 'en/contact',
        'other_locs' => [],
        'alternates' => [
            ['hreflang' => 'en', 'href' => 'en/contact'],
            ['hreflang' => 'ar', 'href' => 'اتصل-بنا'],
        ],
        'priority' => '0.7',
    ],
],

'dynamic_links' => [
    App\Models\Offer::class,
    App\Models\Blog::class,
    App\Models\Product::class,
],

use Otas\Sitemap\Contracts\SitemapContract;

class Offer extends Model implements SitemapContract
{
    public static function sitemapTranslatedSegments(): array
    {
        // Path template per locale with :placeholders
    }

    public static function buildSitemapUrls(): array
    {
        // Build and return the URL array
    }
}

public static function sitemapTranslatedSegments(): array
{
    return [
        'en' => 'offers/:modelIdentifier',
        'ar' => ':modelIdentifier/العروض',
    ];
}

slugResolver: fn($modelItem) => [
    ':modelIdentifier' => $modelItem->slug,
    ':categorySlug' => $modelItem->category->slug,
]

HandleDynamicSitemapHelper::buildDefaultUrls(
    Collection $modelItems,       // The Eloquent collection
    array $translatedSegments,    // Path template per locale
    callable $slugResolver,       // Required: provide all placeholders
    float $priority,              // URL priority (default: 0.8)
);

use Otas\Sitemap\Contracts\SitemapContract;
use Otas\Sitemap\Helpers\HandleDynamicSitemapHelper;

class Offer extends Model implements SitemapContract
{
    public static function sitemapTranslatedSegments(): array
    {
        return [
            'en' => 'offers/:modelIdentifier',
            'ar' => ':modelIdentifier/العروض',
        ];
    }

    public static function buildSitemapUrls(): array
    {
        $offers = self::visible()->get();

        return HandleDynamicSitemapHelper::buildDefaultUrls(
            modelItems: $offers,
            translatedSegments: self::sitemapTranslatedSegments(),
            slugResolver: fn($offer) => [
                ':modelIdentifier' => $offer->slug,
            ]
        );
    }
}

HandleDynamicSitemapHelper::buildLocalizedUrls(
    Collection $modelItems,       // The Eloquent collection
    array $translatedSegments,    // Path template per locale
    ?callable $slugResolver,      // Optional: resolve extra placeholders
    string $routeKeyName,         // Translatable attribute to pluck (default: 'slug')
    float $priority,              // URL priority (default: 0.8)
);

use Otas\Sitemap\Contracts\SitemapContract;
use Otas\Sitemap\Helpers\HandleDynamicSitemapHelper;

class Blog extends Model implements SitemapContract
{
    public static function sitemapTranslatedSegments(): array
    {
        return [
            'en' => 'blogs/:modelIdentifier',
            'ar' => ':modelIdentifier/المدونة',
        ];
    }

    public static function buildSitemapUrls(): array
    {
        $blogs = self::visible()->hasVisibleBlogCategory()->get();

        return HandleDynamicSitemapHelper::buildLocalizedUrls(
            modelItems: $blogs,
            translatedSegments: self::sitemapTranslatedSegments(),
        );
    }
}

class Offer extends Model implements SitemapContract
{
    public static function sitemapTranslatedSegments(): array
    {
        return [
            'en' => 'offers/:categorySlug/details/:modelIdentifier',
            'ar' => ':modelIdentifier/تفاصيل/:categorySlug/العروض',
        ];
    }

    public static function buildSitemapUrls(): array
    {
        $offers = self::visible()->with('category')->get();

        return HandleDynamicSitemapHelper::buildDefaultUrls(
            modelItems: $offers,
            translatedSegments: self::sitemapTranslatedSegments(),
            slugResolver: fn($offer) => [
                ':modelIdentifier' => $offer->slug,
                ':categorySlug' => $offer->category->slug,
            ],
        );
    }
}

class Blog extends Model implements SitemapContract
{
    public static function sitemapTranslatedSegments(): array
    {
        return [
            'en' => 'blogs/:categorySlug/:modelIdentifier',
            'ar' => ':modelIdentifier/:categorySlug/المدونة',
        ];
    }

    public static function buildSitemapUrls(): array
    {
        $blogs = self::visible()->with('category')->get();

        return HandleDynamicSitemapHelper::buildLocalizedUrls(
            modelItems: $blogs,
            translatedSegments: self::sitemapTranslatedSegments(),
            slugResolver: fn($blog) => [
                ':categorySlug' => $blog->category->slug,
            ],
        );
    }
}

class Product extends Model implements SitemapContract
{
    public static function sitemapTranslatedSegments(): array
    {
        return [
            'en' => 'shop/:brandSlug/:categorySlug/:modelIdentifier',
            'ar' => ':modelIdentifier/:categorySlug/:brandSlug/متجر',
        ];
    }

    public static function buildSitemapUrls(): array
    {
        $products = self::visible()->with(['brand', 'category'])->get();

        return HandleDynamicSitemapHelper::buildDefaultUrls(
            modelItems: $products,
            translatedSegments: self::sitemapTranslatedSegments(),
            slugResolver: fn($product) => [
                ':modelIdentifier' => $product->slug,
                ':brandSlug' => $product->brand->slug,
                ':categorySlug' => $product->category->slug,
            ],
        );
    }
}

protected function schedule(Schedule $schedule)
{
    $schedule->command('app:generate-sitemap')->daily();
}

// config/sitemap.php
'default_locale' => 'en',
'dynamic_links' => [
    App\Models\Offer::class,
],

use Otas\Sitemap\Contracts\SitemapContract;
use Otas\Sitemap\Helpers\HandleDynamicSitemapHelper;

class Offer extends Model implements SitemapContract
{
    public static function sitemapTranslatedSegments(): array
    {
        return [
            'en' => 'offers/:modelIdentifier',
            'ar' => ':modelIdentifier/العروض',
        ];
    }

    public static function buildSitemapUrls(): array
    {
        return HandleDynamicSitemapHelper::buildDefaultUrls(
            modelItems: self::visible()->get(),
            translatedSegments: self::sitemapTranslatedSegments(),
            slugResolver: fn($offer) => [
                ':modelIdentifier' => $offer->slug,
            ]
        );
    }
}

// config/sitemap.php
'website_url' => 'https://example.com',
'subdomain' => null,
'default_locale' => 'en',

'static_links' => [
    [
        'loc' => '',
        'other_locs' => ['ar'],
        'alternates' => [
            ['hreflang' => 'en', 'href' => ''],
            ['hreflang' => 'ar', 'href' => 'ar'],
            ['hreflang' => 'x-default', 'href' => ''],
        ],
        'priority' => '1.0',
    ],
    [
        'loc' => 'en/about',
        'other_locs' => [],
        'alternates' => [
            ['hreflang' => 'en', 'href' => 'en/about'],
            ['hreflang' => 'ar', 'href' => 'عن-الشركة'],
        ],
        'priority' => '0.8',
    ],
],

'dynamic_links' => [
    App\Models\Offer::class,
    App\Models\Blog::class,
    App\Models\Product::class,
],

// Simple — non-translated slug
class Offer extends Model implements SitemapContract
{
    public static function sitemapTranslatedSegments(): array
    {
        return [
            'en' => 'offers/:modelIdentifier',
            'ar' => ':modelIdentifier/العروض',
        ];
    }

    public static function buildSitemapUrls(): array
    {
        return HandleDynamicSitemapHelper::buildDefaultUrls(
            modelItems: self::visible()->get(),
            translatedSegments: self::sitemapTranslatedSegments(),
            slugResolver: fn($offer) => [
                ':modelIdentifier' => $offer->slug,
            ]
        );
    }
}

// Simple — translated slug
class Blog extends Model implements SitemapContract
{
    public static function sitemapTranslatedSegments(): array
    {
        return [
            'en' => 'blogs/:modelIdentifier',
            'ar' => ':modelIdentifier/المدونة',
        ];
    }

    public static function buildSitemapUrls(): array
    {
        return HandleDynamicSitemapHelper::buildLocalizedUrls(
            modelItems: self::visible()->get(),
            translatedSegments: self::sitemapTranslatedSegments(),
        );
    }
}

// Multi-segment — with slugResolver
class Product extends Model implements SitemapContract
{
    public static function sitemapTranslatedSegments(): array
    {
        return [
            'en' => 'shop/:brandSlug/:categorySlug/:modelIdentifier',
            'ar' => ':modelIdentifier/:categorySlug/:brandSlug/متجر',
        ];
    }

    public static function buildSitemapUrls(): array
    {
        return HandleDynamicSitemapHelper::buildDefaultUrls(
            modelItems: self::visible()->with(['brand', 'category'])->get(),
            translatedSegments: self::sitemapTranslatedSegments(),
            slugResolver: fn($product) => [
                ':modelIdentifier' => $product->slug,
                ':brandSlug' => $product->brand->slug,
                ':categorySlug' => $product->category->slug,
            ],
        );
    }
}
bash
php artisan setup:sitemap
bash
php artisan vendor:publish --provider="Otas\Sitemap\SitemapServiceProvider" --tag="sitemap-config"
bash
php artisan app:generate-sitemap
bash
php artisan sitemap:publish-routes
bash
php artisan app:generate-sitemap