### Installing Laravel Sitemap Package with Composer Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This command provides the Composer instruction required to install the 'spatie/laravel-sitemap' package into a PHP project, making it available for use. ```bash composer require spatie/laravel-sitemap ``` -------------------------------- ### Starting Test Server for Spatie Sitemap (Bash) Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This bash command sequence is used to start a local test server for the Spatie Laravel Sitemap package. It navigates into the `tests/server` directory and executes the `start_server.sh` script, which is a prerequisite for running the package's tests. ```Bash cd tests/server ./start_server.sh ``` -------------------------------- ### Manually Creating a Sitemap in PHP Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This example shows how to construct a sitemap by manually adding individual URLs, allowing for fine-grained control over properties like the last modification date for each entry. ```php use Carbon\Carbon; use Spatie\Sitemap\Sitemap; use Spatie\Sitemap\Tags\Url; Sitemap::create() ->add(Url::create('/home') ->setLastModificationDate(Carbon::yesterday())) ->add(...) ->writeToFile($path); ``` -------------------------------- ### Example XML Output for Sitemap Index Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This XML snippet provides an example of the structure and content of a generated sitemap index file. It shows how `loc` (location) and `lastmod` (last modification date) elements are formatted for each sitemap entry within the index. ```XML http://www.example.com/pages_sitemap.xml 2016-01-01T00:00:00+00:00 http://www.example.com/posts_sitemap.xml 2015-12-31T00:00:00+00:00 ``` -------------------------------- ### Executing Tests for Spatie Sitemap (Bash) Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This bash command executes the test suite for the Spatie Laravel Sitemap package. It uses Composer's `test` script, which typically runs PHPUnit or similar testing frameworks, to verify the package's functionality after the test server has been started. ```Bash $ composer test ``` -------------------------------- ### Setting Public Visibility for Sitemap on Disk in PHP Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This example demonstrates how to make a sitemap publicly accessible when writing it to a disk, particularly useful for cloud storage like S3, by passing 'true' as the third parameter to the 'writeToDisk()' method. ```php SitemapGenerator::create('https://example.com')->getSitemap()->writeToDisk('public', 'sitemap.xml', true); ``` -------------------------------- ### Creating Sitemap Index with Custom Last Modification Date (PHP) Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This example shows how to create a sitemap index and specify a `lastModificationDate` for individual sitemap entries within the index. It uses a `Sitemap` object to set the modification date for a specific sitemap file, providing more control over the index's metadata. ```PHP use Spatie\Sitemap\SitemapIndex; use Spatie\Sitemap\Tags\Sitemap; SitemapIndex::create() ->add('/pages_sitemap.xml') ->add(Sitemap::create('/posts_sitemap.xml') ->setLastModificationDate(Carbon::yesterday())) ->writeToFile($sitemapIndexPath); ``` -------------------------------- ### Limiting Crawl Count for Sitemap Generation (PHP) Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This example shows how to limit the number of pages crawled by the sitemap generator using the `setMaximumCrawlCount` method. It ensures that only a specified number of pages (e.g., 500) are processed during sitemap creation, optimizing performance for large sites. ```PHP use Spatie\Sitemap\SitemapGenerator; SitemapGenerator::create('https://example.com') ->setMaximumCrawlCount(500) // only the 500 first pages will be crawled ... ``` -------------------------------- ### Creating a Basic Sitemap Index (PHP) Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This snippet illustrates the creation of a sitemap index, which is useful for managing multiple sitemap files. It uses `SitemapIndex::create()` to add references to individual sitemap XML files (e.g., `pages_sitemap.xml`, `posts_sitemap.xml`) before writing the index to a file. ```PHP use Spatie\Sitemap\SitemapIndex; SitemapIndex::create() ->add('/pages_sitemap.xml') ->add('/posts_sitemap.xml') ->writeToFile($sitemapIndexPath); ``` -------------------------------- ### Adding Videos to Sitemap URLs with Optional Parameters (PHP) Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This snippet expands on video sitemap generation by showing how to include optional parameters like `family_friendly`, `live`, `platform`, and `restriction`. It defines arrays for these options and passes them to the `addVideo()` method, providing more detailed video metadata for search engines. ```PHP use Spatie\Sitemap\Sitemap; use Spatie\Sitemap\Tags\Url; use Spatie\Sitemap\Tags\Video; $options = ['family_friendly' => Video::OPTION_YES, 'live' => Video::OPTION_NO]; $allowOptions = ['platform' => Video::OPTION_PLATFORM_MOBILE]; $denyOptions = ['restriction' => 'CA']; Sitemap::create() ->add( Url::create('https://example.com') ->addVideo('https://example.com/images/thumbnail.jpg', 'Video title', 'Video Description', 'https://example.com/videos/source.mp4', 'https://example.com/video/123', $options, $allowOptions, $denyOptions) ) ->writeToFile($sitemapPath); ``` -------------------------------- ### Generating Sitemap by Crawling in PHP Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This snippet demonstrates the simplest way to generate a sitemap by crawling an entire website from a given URL and writing the output to a specified file path. ```php use Spatie\Sitemap\SitemapGenerator; SitemapGenerator::create('https://example.com')->writeToFile($path); ``` -------------------------------- ### Combining Crawling and Manual Sitemap Additions in PHP Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This snippet illustrates how to first generate a sitemap by crawling a site and then append additional, manually defined URLs to it before writing the complete sitemap to a file. ```php SitemapGenerator::create('https://example.com') ->getSitemap() ->add(Url::create('/extra-page') ->setLastModificationDate(Carbon::yesterday())) ->add(...) ->writeToFile($path); ``` -------------------------------- ### Manually Creating a Sitemap (PHP) Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This code demonstrates how to manually construct a sitemap by adding individual URLs. It shows how to add simple paths and how to create `Url` objects to set additional properties like `lastModificationDate` using Carbon, providing fine-grained control over sitemap entries. ```PHP use Carbon\Carbon; Sitemap::create() ->add('/page1') ->add('/page2') ->add(Url::create('/page3')->setLastModificationDate(Carbon::create('2016', '1', '1'))) ->writeToFile($sitemapPath); ``` -------------------------------- ### Publishing Laravel Sitemap Configuration in Bash Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This Artisan command publishes the default configuration file for the Laravel Sitemap package to 'config/sitemap.php', allowing users to customize various crawler and sitemap generation options. ```bash php artisan vendor:publish --provider="Spatie\Sitemap\SitemapServiceProvider" --tag=sitemap-config ``` -------------------------------- ### Writing Sitemap to a Filesystem Disk in PHP Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This snippet shows how to save the generated sitemap to a specified filesystem disk, such as 'public', with a given filename, leveraging Laravel's filesystem capabilities. ```php SitemapGenerator::create('https://example.com')->getSitemap()->writeToDisk('public', 'sitemap.xml'); ``` -------------------------------- ### Default Laravel Sitemap Configuration File in PHP Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This snippet displays the default configuration options found in 'config/sitemap.php', including Guzzle HTTP client settings, JavaScript execution flags, Chrome binary path, and the default crawl profile class. ```php use GuzzleHttp\RequestOptions; use Spatie\Sitemap\Crawler\Profile; return [ /* * These options will be passed to GuzzleHttp\Client when it is created. * For in-depth information on all options see the Guzzle docs: * * http://docs.guzzlephp.org/en/stable/request-options.html */ 'guzzle_options' => [ /* * Whether or not cookies are used in a request. */ RequestOptions::COOKIES => true, /* * The number of seconds to wait while trying to connect to a server. * Use 0 to wait indefinitely. */ RequestOptions::CONNECT_TIMEOUT => 10, /* * The timeout of the request in seconds. Use 0 to wait indefinitely. */ RequestOptions::TIMEOUT => 10, /* * Describes the redirect behavior of a request. */ RequestOptions::ALLOW_REDIRECTS => false, ], /* * The sitemap generator can execute JavaScript on each page so it will * discover links that are generated by your JS scripts. This feature * is powered by headless Chrome. */ 'execute_javascript' => false, /* * The package will make an educated guess as to where Google Chrome is installed. * You can also manually pass it's location here. */ 'chrome_binary_path' => '', /* * The sitemap generator uses a CrawlProfile implementation to determine * which urls should be crawled for the sitemap. */ 'crawl_profile' => Profile ``` -------------------------------- ### Adding Models and Collections to Sitemap in PHP Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This snippet demonstrates how to add individual model instances or entire collections of models (that implement 'Sitemapable') directly to a manually created sitemap. ```php use Spatie\Sitemap\Sitemap; Sitemap::create() ->add($post) ->add(Post::all()); ``` -------------------------------- ### Configuring Crawler for Sitemap Generation (PHP) Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This snippet demonstrates how to configure the underlying crawler used by the sitemap generator. It shows how to ignore robot checks by passing a closure to `configureCrawler` and calling `ignoreRobots()` on the `Crawler` instance. The sitemap is then written to a specified file. ```PHP SitemapGenerator::create('http://localhost:4020') ->configureCrawler(function (Crawler $crawler) { $crawler->ignoreRobots(); }) ->writeToFile($file); ``` -------------------------------- ### Creating an Artisan Command for Sitemap Generation (PHP) Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This PHP code defines a Laravel Artisan command (`sitemap:generate`) responsible for generating the sitemap. It uses `SitemapGenerator::create()` to crawl the application URL and write the sitemap to the public path, making it suitable for scheduled execution. ```PHP namespace App\Console\Commands; use Illuminate\Console\Command; use Spatie\Sitemap\SitemapGenerator; class GenerateSitemap extends Command { /** * The console command name. * * @var string */ protected $signature = 'sitemap:generate'; /** * The console command description. * * @var string */ protected $description = 'Generate the sitemap.'; /** * Execute the console command. * * @return mixed */ public function handle() { // modify this to your own needs SitemapGenerator::create(config('app.url')) ->writeToFile(public_path('sitemap.xml')); } } ``` -------------------------------- ### Scheduling Sitemap Generation Artisan Command (PHP) Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This snippet shows how to schedule the previously defined `sitemap:generate` Artisan command within Laravel's console kernel. By calling `daily()`, the sitemap generation process will automatically run once every day, ensuring the sitemap remains up-to-date. ```PHP // app/Console/Kernel.php protected function schedule(Schedule $schedule) { ... $schedule->command('sitemap:generate')->daily(); ... } ``` -------------------------------- ### Implementing Sitemapable Interface for Models in PHP Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This code illustrates how to make a Laravel model sitemap-aware by implementing the 'Sitemapable' interface and defining the 'toSitemapTag()' method to return a URL string, an array, or a 'Url' object for sitemap inclusion. ```php use Spatie\Sitemap\Contracts\Sitemapable; use Spatie\Sitemap\Tags\Url; class Post extends Model implements Sitemapable { public function toSitemapTag(): Url | string | array { // Simple return: return route('blog.post.show', $this); // Return with fine-grained control: return Url::create(route('blog.post.show', $this)) ->setLastModificationDate(Carbon::create($this->updated_at)); } } ``` -------------------------------- ### Adding Videos to Sitemap URLs with Required Attributes (PHP) Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This code demonstrates how to add video information to a sitemap URL, adhering to video sitemap standards. It uses `addVideo()` to include required attributes such as thumbnail, title, description, content location, and player location for a video associated with a specific URL. ```PHP use Spatie\Sitemap\Sitemap; use Spatie\Sitemap\Tags\Url; Sitemap::create() ->add( Url::create('https://example.com') ->addVideo('https://example.com/images/thumbnail.jpg', 'Video title', 'Video Description', 'https://example.com/videos/source.mp4', 'https://example.com/video/123') ) ->writeToFile($sitemapPath); ``` -------------------------------- ### Manually Adding Links to a Sitemap (PHP) Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This snippet illustrates how to manually add individual links to a sitemap. It uses `SitemapGenerator::create()->getSitemap()` to obtain the sitemap instance, allowing for the addition of custom URLs before writing the sitemap to a file. ```PHP use Spatie\Sitemap\SitemapGenerator; use Spatie\Sitemap\Tags\Url; SitemapGenerator::create('https://example.com') ->getSitemap() // here we add one extra link, but you can add as many as you'd like ->writeToFile($sitemapPath); ``` -------------------------------- ### Adding Images to Sitemap URLs (PHP) Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This snippet shows how to associate images with a URL in a sitemap, following Google's image sitemap guidelines. It uses `Url::create()` and then `addImage()` to specify the image URL and an optional title, enhancing search engine visibility for image content. ```PHP use Spatie\Sitemap\Sitemap; use Spatie\Sitemap\Tags\Url; Sitemap::create() // here we add an image to a URL ->add(Url::create('https://example.com')->addImage('https://example.com/images/home.jpg', 'Home page image')) ->writeToFile($sitemapPath); ``` -------------------------------- ### Generating Sitemap with Max Tags Per Sitemap (PHP) Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This snippet demonstrates how to limit the number of tags (URLs) per sitemap file when generating a sitemap index. By calling `maxTagsPerSitemap()`, the generator will automatically split the sitemap into multiple files if the tag count exceeds the specified limit, creating a sitemap index to link them. ```PHP use Spatie\Sitemap\SitemapGenerator; SitemapGenerator::create('https://example.com') ->maxTagsPerSitemap(20000) ->writeToFile(public_path('sitemap.xml')); ``` -------------------------------- ### Adding Alternate Language Links to Sitemap (PHP) Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This code demonstrates how to add alternate language versions for a URL within a sitemap, crucial for multilingual sites. It uses the `addAlternate` function, which takes an alternate URL and its corresponding locale, though the specific `addAlternate` call is not shown in the provided snippet, the context implies its usage. ```PHP use Spatie\Sitemap\SitemapGenerator; use Spatie\Sitemap\Tags\Url; SitemapGenerator::create('https://example.com') ->getSitemap() // here we add one extra link, but you can add as many as you'd like ->writeToFile($sitemapPath); ``` -------------------------------- ### Setting Maximum Crawl Depth for Sitemap Generation in PHP Source: https://github.com/spatie/laravel-sitemap/blob/main/README.md This code demonstrates how to configure the underlying crawler to limit the maximum depth of pages it will visit when generating the sitemap, preventing excessively deep crawls. ```php SitemapGenerator::create('https://example.com') ->configureCrawler(function (Crawler $crawler) { $crawler->setMaximumDepth(3); }) ->writeToFile($path); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.