One Hat Cyber Team
Your IP:
216.73.216.215
Server IP:
198.54.114.155
Server:
Linux server71.web-hosting.com 4.18.0-513.18.1.lve.el8.x86_64 #1 SMP Thu Feb 22 12:55:50 UTC 2024 x86_64
Server Software:
LiteSpeed
PHP Version:
5.6.40
Create File
|
Create Folder
Execute
Dir :
~
/
proc
/
thread-self
/
root
/
proc
/
self
/
cwd
/
Edit File:
laravel-sitemap.tar
src/Contracts/Sitemapable.php 0000644 00000000236 15111420047 0012226 0 ustar 00 <?php namespace Spatie\Sitemap\Contracts; use Spatie\Sitemap\Tags\Url; interface Sitemapable { public function toSitemapTag(): Url | string | array; } src/SitemapServiceProvider.php 0000644 00000001207 15111420047 0012475 0 ustar 00 <?php namespace Spatie\Sitemap; use Spatie\Crawler\Crawler; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; class SitemapServiceProvider extends PackageServiceProvider { public function configurePackage(Package $package): void { $package ->name('laravel-sitemap') ->hasConfigFile() ->hasViews(); } public function packageRegistered(): void { $this->app->when(SitemapGenerator::class) ->needs(Crawler::class) ->give(static fn (): Crawler => Crawler::create(config('sitemap.guzzle_options'))); } } src/Crawler/Observer.php 0000644 00000003151 15111420047 0011225 0 ustar 00 <?php namespace Spatie\Sitemap\Crawler; use GuzzleHttp\Exception\RequestException; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; use Spatie\Crawler\CrawlObservers\CrawlObserver; class Observer extends CrawlObserver { /** @var callable */ protected $hasCrawled; public function __construct(callable $hasCrawled) { $this->hasCrawled = $hasCrawled; } /** * Called when the crawler will crawl the url. * * @param \Psr\Http\Message\UriInterface $url */ public function willCrawl(UriInterface $url): void { } /** * Called when the crawl has ended. */ public function finishedCrawling(): void { } /** * Called when the crawler has crawled the given url successfully. * * @param \Psr\Http\Message\UriInterface $url * @param \Psr\Http\Message\ResponseInterface $response * @param \Psr\Http\Message\UriInterface|null $foundOnUrl */ public function crawled( UriInterface $url, ResponseInterface $response, ?UriInterface $foundOnUrl = null ): void { ($this->hasCrawled)($url, $response); } /** * Called when the crawler had a problem crawling the given url. * * @param \Psr\Http\Message\UriInterface $url * @param \GuzzleHttp\Exception\RequestException $requestException * @param \Psr\Http\Message\UriInterface|null $foundOnUrl */ public function crawlFailed( UriInterface $url, RequestException $requestException, ?UriInterface $foundOnUrl = null ): void { } } src/Crawler/Profile.php 0000644 00000000670 15111420047 0011041 0 ustar 00 <?php namespace Spatie\Sitemap\Crawler; use Psr\Http\Message\UriInterface; use Spatie\Crawler\CrawlProfiles\CrawlProfile; class Profile extends CrawlProfile { /** @var callable */ protected $callback; public function shouldCrawlCallback(callable $callback): void { $this->callback = $callback; } public function shouldCrawl(UriInterface $url): bool { return ($this->callback)($url); } } src/SitemapIndex.php 0000644 00000003531 15111420047 0010433 0 ustar 00 <?php namespace Spatie\Sitemap; use Illuminate\Contracts\Support\Renderable; use Illuminate\Contracts\Support\Responsable; use Illuminate\Support\Facades\Response; use Illuminate\Support\Facades\Storage; use Spatie\Sitemap\Tags\Sitemap; use Spatie\Sitemap\Tags\Tag; class SitemapIndex implements Responsable, Renderable { /** @var \Spatie\Sitemap\Tags\Sitemap[] */ protected array $tags = []; public static function create(): static { return new static(); } public function add(string | Sitemap $tag): static { if (is_string($tag)) { $tag = Sitemap::create($tag); } $this->tags[] = $tag; return $this; } public function getSitemap(string $url): ?Sitemap { return collect($this->tags)->first(function (Tag $tag) use ($url) { return $tag->getType() === 'sitemap' && $tag->url === $url; }); } public function hasSitemap(string $url): bool { return (bool) $this->getSitemap($url); } public function render(): string { $tags = $this->tags; return view('sitemap::sitemapIndex/index') ->with(compact('tags')) ->render(); } public function writeToFile(string $path): static { file_put_contents($path, $this->render()); return $this; } public function writeToDisk(string $disk, string $path): static { Storage::disk($disk)->put($path, $this->render()); return $this; } /** * Create an HTTP response that represents the object. * * @param \Illuminate\Http\Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function toResponse($request) { return Response::make($this->render(), 200, [ 'Content-Type' => 'text/xml', ]); } } src/SitemapGenerator.php 0000644 00000012512 15111420047 0011311 0 ustar 00 <?php namespace Spatie\Sitemap; use Closure; use GuzzleHttp\Psr7\Uri; use Illuminate\Support\Collection; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; use Spatie\Browsershot\Browsershot; use Spatie\Crawler\Crawler; use Spatie\Crawler\CrawlProfiles\CrawlProfile; use Spatie\Sitemap\Crawler\Observer; use Spatie\Sitemap\Crawler\Profile; use Spatie\Sitemap\Tags\Url; class SitemapGenerator { protected Collection $sitemaps; protected Uri $urlToBeCrawled; protected Crawler $crawler; /** @var callable */ protected $shouldCrawl; /** @var callable */ protected $hasCrawled; protected int $concurrency = 10; protected bool | int $maximumTagsPerSitemap = false; protected ?int $maximumCrawlCount = null; public static function create(string $urlToBeCrawled): static { return app(static::class)->setUrl($urlToBeCrawled); } public function __construct(Crawler $crawler) { $this->crawler = $crawler; $this->sitemaps = new Collection([new Sitemap]); $this->hasCrawled = fn (Url $url, ResponseInterface $response = null) => $url; } public function configureCrawler(Closure $closure): static { call_user_func_array($closure, [$this->crawler]); return $this; } public function setConcurrency(int $concurrency): static { $this->concurrency = $concurrency; return $this; } public function setMaximumCrawlCount(int $maximumCrawlCount): static { $this->maximumCrawlCount = $maximumCrawlCount; return $this; } public function maxTagsPerSitemap(int $maximumTagsPerSitemap = 50000): static { $this->maximumTagsPerSitemap = $maximumTagsPerSitemap; return $this; } public function setUrl(string $urlToBeCrawled): static { $this->urlToBeCrawled = new Uri($urlToBeCrawled); if ($this->urlToBeCrawled->getPath() === '') { $this->urlToBeCrawled = $this->urlToBeCrawled->withPath('/'); } return $this; } public function shouldCrawl(callable $shouldCrawl): static { $this->shouldCrawl = $shouldCrawl; return $this; } public function hasCrawled(callable $hasCrawled): static { $this->hasCrawled = $hasCrawled; return $this; } public function getSitemap(): Sitemap { if (config('sitemap.execute_javascript')) { $this->crawler->executeJavaScript(); } if (config('sitemap.chrome_binary_path')) { $this->crawler ->setBrowsershot((new Browsershot)->setChromePath(config('sitemap.chrome_binary_path'))) ->acceptNofollowLinks(); } if (! is_null($this->maximumCrawlCount)) { $this->crawler->setTotalCrawlLimit($this->maximumCrawlCount); } $this->crawler ->setCrawlProfile($this->getCrawlProfile()) ->setCrawlObserver($this->getCrawlObserver()) ->setConcurrency($this->concurrency) ->startCrawling($this->urlToBeCrawled); return $this->sitemaps->first(); } public function writeToFile(string $path): static { $sitemap = $this->getSitemap(); if ($this->maximumTagsPerSitemap) { $sitemap = SitemapIndex::create(); $format = str_replace('.xml', '_%d.xml', $path); // Parses each sub-sitemaps, writes and push them into the sitemap index $this->sitemaps->each(function (Sitemap $item, int $key) use ($sitemap, $format) { $path = sprintf($format, $key); $item->writeToFile(sprintf($format, $key)); $sitemap->add(last(explode('public', $path))); }); } $sitemap->writeToFile($path); return $this; } protected function getCrawlProfile(): CrawlProfile { $shouldCrawl = function (UriInterface $url) { if ($url->getHost() !== $this->urlToBeCrawled->getHost()) { return false; } if (! is_callable($this->shouldCrawl)) { return true; } return ($this->shouldCrawl)($url); }; $profileClass = config('sitemap.crawl_profile', Profile::class); $profile = new $profileClass($this->urlToBeCrawled); if (method_exists($profile, 'shouldCrawlCallback')) { $profile->shouldCrawlCallback($shouldCrawl); } return $profile; } protected function getCrawlObserver(): Observer { $performAfterUrlHasBeenCrawled = function (UriInterface $crawlerUrl, ResponseInterface $response = null) { $sitemapUrl = ($this->hasCrawled)(Url::create((string) $crawlerUrl), $response); if ($this->shouldStartNewSitemapFile()) { $this->sitemaps->push(new Sitemap); } if ($sitemapUrl) { $this->sitemaps->last()->add($sitemapUrl); } }; return new Observer($performAfterUrlHasBeenCrawled); } protected function shouldStartNewSitemapFile(): bool { if (! $this->maximumTagsPerSitemap) { return false; } $currentNumberOfTags = count($this->sitemaps->last()->getTags()); return $currentNumberOfTags >= $this->maximumTagsPerSitemap; } } src/Sitemap.php 0000644 00000004504 15111420047 0007444 0 ustar 00 <?php namespace Spatie\Sitemap; use Illuminate\Contracts\Support\Renderable; use Illuminate\Contracts\Support\Responsable; use Illuminate\Support\Facades\Response; use Illuminate\Support\Facades\Storage; use Spatie\Sitemap\Contracts\Sitemapable; use Spatie\Sitemap\Tags\Tag; use Spatie\Sitemap\Tags\Url; class Sitemap implements Responsable, Renderable { /** @var \Spatie\Sitemap\Tags\Url[] */ protected array $tags = []; public static function create(): static { return new static(); } public function add(string | Url | Sitemapable | iterable $tag): static { if (is_object($tag) && array_key_exists(Sitemapable::class, class_implements($tag))) { $tag = $tag->toSitemapTag(); } if (is_iterable($tag)) { foreach ($tag as $item) { $this->add($item); } return $this; } if (is_string($tag)) { $tag = Url::create($tag); } if (! in_array($tag, $this->tags)) { $this->tags[] = $tag; } return $this; } public function getTags(): array { return $this->tags; } public function getUrl(string $url): ?Url { return collect($this->tags)->first(function (Tag $tag) use ($url) { return $tag->getType() === 'url' && $tag->url === $url; }); } public function hasUrl(string $url): bool { return (bool) $this->getUrl($url); } public function render(): string { $tags = collect($this->tags)->unique('url')->filter(); return view('sitemap::sitemap') ->with(compact('tags')) ->render(); } public function writeToFile(string $path): static { file_put_contents($path, $this->render()); return $this; } public function writeToDisk(string $disk, string $path): static { Storage::disk($disk)->put($path, $this->render()); return $this; } /** * Create an HTTP response that represents the object. * * @param \Illuminate\Http\Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function toResponse($request) { return Response::make($this->render(), 200, [ 'Content-Type' => 'text/xml', ]); } } src/Tags/Url.php 0000644 00000006646 15111420047 0007513 0 ustar 00 <?php namespace Spatie\Sitemap\Tags; use Carbon\Carbon; use DateTimeInterface; class Url extends Tag { const CHANGE_FREQUENCY_ALWAYS = 'always'; const CHANGE_FREQUENCY_HOURLY = 'hourly'; const CHANGE_FREQUENCY_DAILY = 'daily'; const CHANGE_FREQUENCY_WEEKLY = 'weekly'; const CHANGE_FREQUENCY_MONTHLY = 'monthly'; const CHANGE_FREQUENCY_YEARLY = 'yearly'; const CHANGE_FREQUENCY_NEVER = 'never'; public string $url; public Carbon $lastModificationDate; public string $changeFrequency; public float $priority = 0.8; /** @var \Spatie\Sitemap\Tags\Alternate[] */ public array $alternates = []; /** @var \Spatie\Sitemap\Tags\Image[] */ public array $images = []; /** @var \Spatie\Sitemap\Tags\Video[] */ public array $videos = []; /** @var \Spatie\Sitemap\Tags\News[] */ public array $news = []; public static function create(string $url): static { return new static($url); } public function __construct(string $url) { $this->url = $url; $this->changeFrequency = static::CHANGE_FREQUENCY_DAILY; } public function setUrl(string $url = ''): static { $this->url = $url; return $this; } public function setLastModificationDate(DateTimeInterface $lastModificationDate): static { $this->lastModificationDate = Carbon::instance($lastModificationDate); return $this; } public function setChangeFrequency(string $changeFrequency): static { $this->changeFrequency = $changeFrequency; return $this; } public function setPriority(float $priority): static { $this->priority = max(0, min($priority, 1)); return $this; } public function addAlternate(string $url, string $locale = ''): static { $this->alternates[] = new Alternate($url, $locale); return $this; } public function addImage( string $url, string $caption = '', string $geo_location = '', string $title = '', string $license = '' ): static { $this->images[] = new Image($url, $caption, $geo_location, $title, $license); return $this; } public function addVideo(string $thumbnailLoc, string $title, string $description, $contentLoc = null, $playerLoc = null, array $options = [], array $allow = [], array $deny = [], array $tags = []): static { $this->videos[] = new Video($thumbnailLoc, $title, $description, $contentLoc, $playerLoc, $options, $allow, $deny, $tags); return $this; } public function addNews(string $name, string $language, string $title, DateTimeInterface $publicationDate, array $options = []): static { $this->news[] = new News($name, $language, $title, $publicationDate, $options); return $this; } public function path(): string { return parse_url($this->url, PHP_URL_PATH) ?? ''; } public function segments(?int $index = null): array|string|null { $segments = collect(explode('/', $this->path())) ->filter(function ($value) { return $value !== ''; }) ->values() ->toArray(); if (! is_null($index)) { return $this->segment($index); } return $segments; } public function segment(int $index): ?string { return $this->segments()[$index - 1] ?? null; } } src/Tags/Alternate.php 0000644 00000001157 15111420047 0010660 0 ustar 00 <?php namespace Spatie\Sitemap\Tags; class Alternate { public string $locale; public string $url; public static function create(string $url, string $locale = ''): static { return new static($url, $locale); } public function __construct(string $url, $locale = '') { $this->setUrl($url); $this->setLocale($locale); } public function setLocale(string $locale = ''): static { $this->locale = $locale; return $this; } public function setUrl(string $url = ''): static { $this->url = $url; return $this; } } src/Tags/News.php 0000644 00000003256 15111420047 0007657 0 ustar 00 <?php namespace Spatie\Sitemap\Tags; use Carbon\Carbon; use DateTimeInterface; class News { public const OPTION_ACCESS_SUB = 'Subscription'; public const OPTION_ACCESS_REG = 'Registration'; public const OPTION_GENRES_PR = 'PressRelease'; public const OPTION_GENRES_SATIRE = 'Satire'; public const OPTION_GENRES_BLOG = 'Blog'; public const OPTION_GENRES_OPED = 'OpEd'; public const OPTION_GENRES_OPINION = 'Opinion'; public const OPTION_GENRES_UG = 'UserGenerated'; public string $name; public string $language; public string $title; public Carbon $publicationDate; public ?array $options; public function __construct( string $name, string $language, string $title, DateTimeInterface $publicationDate, array $options = [] ) { $this ->setName($name) ->setLanguage($language) ->setTitle($title) ->setPublicationDate($publicationDate) ->setOptions($options); } public function setName(string $name): self { $this->name = $name; return $this; } public function setLanguage(string $language): self { $this->language = $language; return $this; } public function setTitle(string $title): self { $this->title = $title; return $this; } public function setPublicationDate(DateTimeInterface $publicationDate): self { $this->publicationDate = Carbon::instance($publicationDate); return $this; } public function setOptions(array $options): self { $this->options = $options; return $this; } } src/Tags/Video.php 0000644 00000004777 15111420047 0010022 0 ustar 00 <?php namespace Spatie\Sitemap\Tags; class Video { public const OPTION_PLATFORM_WEB = 'web'; public const OPTION_PLATFORM_MOBILE = 'mobile'; public const OPTION_PLATFORM_TV = 'tv'; public const OPTION_NO = "no"; public const OPTION_YES = "yes"; public string $thumbnailLoc; public string $title; public string $description; public ?string $contentLoc; public ?string $playerLoc; public array $options; public array $allow; public array $deny; public array $tags; public function __construct(string $thumbnailLoc, string $title, string $description, string $contentLoc = null, string|array $playerLoc = null, array $options = [], array $allow = [], array $deny = [], array $tags = []) { if ($contentLoc === null && $playerLoc === null) { // https://developers.google.com/search/docs/crawling-indexing/sitemaps/video-sitemaps throw new \Exception("It's required to provide either a Content Location or Player Location"); } $this->setThumbnailLoc($thumbnailLoc) ->setTitle($title) ->setDescription($description) ->setContentLoc($contentLoc) ->setPlayerLoc($playerLoc) ->setOptions($options) ->setAllow($allow) ->setDeny($deny) ->setTags($tags); } public function setThumbnailLoc(string $thumbnailLoc): self { $this->thumbnailLoc = $thumbnailLoc; return $this; } public function setTitle(string $title): self { $this->title = $title; return $this; } public function setDescription(string $description): self { $this->description = $description; return $this; } public function setContentLoc(?string $contentLoc): self { $this->contentLoc = $contentLoc; return $this; } public function setPlayerLoc(?string $playerLoc): self { $this->playerLoc = $playerLoc; return $this; } public function setOptions(?array $options): self { $this->options = $options; return $this; } public function setAllow(array $allow): self { $this->allow = $allow; return $this; } public function setDeny(array $deny): self { $this->deny = $deny; return $this; } public function setTags(array $tags): self { $this->tags = array_slice($tags, 0, 32); // maximum 32 tags allowed return $this; } } src/Tags/Tag.php 0000644 00000000255 15111420047 0007452 0 ustar 00 <?php namespace Spatie\Sitemap\Tags; abstract class Tag { public function getType(): string { return mb_strtolower(class_basename(static::class)); } } src/Tags/Image.php 0000644 00000002617 15111420047 0007765 0 ustar 00 <?php namespace Spatie\Sitemap\Tags; class Image { public string $url; public string $caption; public string $geo_location; public string $title; public string $license; public static function create(string $url, string $caption = '', string $geo_location = '', string $title = '', string $license = ''): static { return new static($url, $caption, $geo_location, $title, $license); } public function __construct(string $url, string $caption = '', string $geo_location = '', string $title = '', string $license = '') { $this->setUrl($url); $this->setCaption($caption); $this->setGeoLocation($geo_location); $this->setTitle($title); $this->setLicense($license); } public function setUrl(string $url = ''): static { $this->url = $url; return $this; } public function setCaption(string $caption = ''): static { $this->caption = $caption; return $this; } public function setGeoLocation(string $geo_location = ''): static { $this->geo_location = $geo_location; return $this; } public function setTitle(string $title = ''): static { $this->title = $title; return $this; } public function setLicense(string $license = ''): static { $this->license = $license; return $this; } } src/Tags/Sitemap.php 0000644 00000001520 15111420047 0010335 0 ustar 00 <?php namespace Spatie\Sitemap\Tags; use Carbon\Carbon; use DateTimeInterface; class Sitemap extends Tag { public string $url; public Carbon $lastModificationDate; public static function create(string $url): static { return new static($url); } public function __construct(string $url) { $this->url = $url; $this->lastModificationDate = Carbon::now(); } public function setUrl(string $url = ''): static { $this->url = $url; return $this; } public function setLastModificationDate(DateTimeInterface $lastModificationDate): static { $this->lastModificationDate = Carbon::instance($lastModificationDate); return $this; } public function path(): string { return parse_url($this->url, PHP_URL_PATH) ?? ''; } } README.md 0000644 00000043111 15111420047 0006016 0 ustar 00 # Generate sitemaps with ease [](https://packagist.org/packages/spatie/laravel-sitemap) [](LICENSE.md) [](https://github.com/spatie/laravel-sitemap/actions/workflows/run-tests.yml) [](https://github.com/spatie/laravel-sitemap/actions/workflows/php-cs-fixer.yml) [](https://packagist.org/packages/spatie/laravel-sitemap) This package can generate a sitemap without you having to add urls to it manually. This works by crawling your entire site. ```php use Spatie\Sitemap\SitemapGenerator; SitemapGenerator::create('https://example.com')->writeToFile($path); ``` You can also create your sitemap manually: ```php use Carbon\Carbon; use Spatie\Sitemap\Sitemap; use Spatie\Sitemap\Tags\Url; Sitemap::create() ->add(Url::create('/home') ->setLastModificationDate(Carbon::yesterday()) ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY) ->setPriority(0.1)) ->add(...) ->writeToFile($path); ``` Or you can have the best of both worlds by generating a sitemap and then adding more links to it: ```php SitemapGenerator::create('https://example.com') ->getSitemap() ->add(Url::create('/extra-page') ->setLastModificationDate(Carbon::yesterday()) ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY) ->setPriority(0.1)) ->add(...) ->writeToFile($path); ``` You can also control the maximum depth of the sitemap: ```php SitemapGenerator::create('https://example.com') ->configureCrawler(function (Crawler $crawler) { $crawler->setMaximumDepth(3); }) ->writeToFile($path); ``` The generator has [the ability to execute JavaScript](https://github.com/spatie/laravel-sitemap#executing-javascript) on each page so links injected into the dom by JavaScript will be crawled as well. You can also use one of your available filesystem disks to write the sitemap to. ```php SitemapGenerator::create('https://example.com')->getSitemap()->writeToDisk('public', 'sitemap.xml'); ``` You can also add your models directly by implementing the `\Spatie\Sitemap\Contracts\Sitemapable` interface. ```php use Spatie\Sitemap\Contracts\Sitemapable; use Spatie\Sitemap\Tags\Url; class Post extends Model implements Sitemapable { public function toSitemapTag(): Url | string | array { return route('blog.post.show', $this); return Url::create(route('blog.post.show', $this)) ->setLastModificationDate(Carbon::create($this->updated_at)) ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY) ->setPriority(0.1); } } ``` Now you can add a single post model to the sitemap or even a whole collection. ```php use Spatie\Sitemap\Sitemap; Sitemap::create() ->add($post) ->add(Post::all()); ``` This way you can add all your pages super fast without the need to crawl them all. ## Support us [<img src="https://github-ads.s3.eu-central-1.amazonaws.com/laravel-sitemap.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/laravel-sitemap) We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). ## Installation First, install the package via composer: ``` bash composer require spatie/laravel-sitemap ``` The package will automatically register itself. If you want to update your sitemap automatically and frequently you need to perform [some extra steps](https://github.com/spatie/laravel-sitemap#generating-the-sitemap-frequently). ## Configuration You can override the default options for the crawler. First publish the configuration: ```bash php artisan vendor:publish --provider="Spatie\Sitemap\SitemapServiceProvider" --tag=sitemap-config ``` This will copy the default config to `config/sitemap.php` where you can edit it. ```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::class, ]; ``` ## Usage ### Generating a sitemap The easiest way is to crawl the given domain and generate a sitemap with all found links. The destination of the sitemap should be specified by `$path`. ```php SitemapGenerator::create('https://example.com')->writeToFile($path); ``` The generated sitemap will look similar to this: ```xml <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>https://example.com</loc> <lastmod>2016-01-01T00:00:00+00:00</lastmod> <changefreq>daily</changefreq> <priority>0.8</priority> </url> <url> <loc>https://example.com/page</loc> <lastmod>2016-01-01T00:00:00+00:00</lastmod> <changefreq>daily</changefreq> <priority>0.8</priority> </url> ... </urlset> ``` ### Customizing the sitemap generator #### Define a custom Crawl Profile You can create a custom crawl profile by implementing the `Spatie\Crawler\CrawlProfiles\CrawlProfile` interface and by customizing the `shouldCrawl()` method for full control over what url/domain/sub-domain should be crawled: ```php use Spatie\Crawler\CrawlProfiles\CrawlProfile; use Psr\Http\Message\UriInterface; class CustomCrawlProfile extends CrawlProfile { public function shouldCrawl(UriInterface $url): bool { if ($url->getHost() !== 'localhost') { return false; } return $url->getPath() === '/'; } } ``` and register your `CustomCrawlProfile::class` in `config/sitemap.php`. ```php return [ ... /* * The sitemap generator uses a CrawlProfile implementation to determine * which urls should be crawled for the sitemap. */ 'crawl_profile' => CustomCrawlProfile::class, ]; ``` #### Changing properties To change the `lastmod`, `changefreq` and `priority` of the contact page: ```php use Carbon\Carbon; use Spatie\Sitemap\SitemapGenerator; use Spatie\Sitemap\Tags\Url; SitemapGenerator::create('https://example.com') ->hasCrawled(function (Url $url) { if ($url->segment(1) === 'contact') { $url->setPriority(0.9) ->setLastModificationDate(Carbon::create('2016', '1', '1')); } return $url; }) ->writeToFile($sitemapPath); ``` #### Leaving out some links If you don't want a crawled link to appear in the sitemap, just don't return it in the callable you pass to `hasCrawled `. ```php use Spatie\Sitemap\SitemapGenerator; use Spatie\Sitemap\Tags\Url; SitemapGenerator::create('https://example.com') ->hasCrawled(function (Url $url) { if ($url->segment(1) === 'contact') { return; } return $url; }) ->writeToFile($sitemapPath); ``` #### Preventing the crawler from crawling some pages You can also instruct the underlying crawler to not crawl some pages by passing a `callable` to `shouldCrawl`. **Note:** `shouldCrawl` will only work with the default crawl `Profile` or custom crawl profiles that implement a `shouldCrawlCallback` method. ```php use Spatie\Sitemap\SitemapGenerator; use Psr\Http\Message\UriInterface; SitemapGenerator::create('https://example.com') ->shouldCrawl(function (UriInterface $url) { // All pages will be crawled, except the contact page. // Links present on the contact page won't be added to the // sitemap unless they are present on a crawlable page. return strpos($url->getPath(), '/contact') === false; }) ->writeToFile($sitemapPath); ``` #### Configuring the crawler The crawler itself can be [configured](https://github.com/spatie/crawler#usage) to do a few different things. You can configure the crawler used by the sitemap generator, for example: to ignore robot checks; like so. ```php SitemapGenerator::create('http://localhost:4020') ->configureCrawler(function (Crawler $crawler) { $crawler->ignoreRobots(); }) ->writeToFile($file); ``` #### Limiting the amount of pages crawled You can limit the amount of pages crawled by calling `setMaximumCrawlCount` ```php use Spatie\Sitemap\SitemapGenerator; SitemapGenerator::create('https://example.com') ->setMaximumCrawlCount(500) // only the 500 first pages will be crawled ... ``` #### Executing Javascript The sitemap generator can execute JavaScript on each page so it will discover links that are generated by your JS scripts. You can enable this feature by setting `execute_javascript` in the config file to `true`. Under the hood, [headless Chrome](https://github.com/spatie/browsershot) is used to execute JavaScript. Here are some pointers on [how to install it on your system](https://github.com/spatie/browsershot#requirements). The package will make an educated guess as to where Chrome is installed on your system. You can also manually pass the location of the Chrome binary to `executeJavaScript()`. #### Manually adding links You can manually add links to a sitemap: ```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 ->add(Url::create('/extra-page')->setPriority(0.5)) ->writeToFile($sitemapPath); ``` #### Adding alternates to links Multilingual sites may have several alternate versions of the same page (one per language). Based on the previous example adding an alternate can be done as follows: ```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 ->add(Url::create('/extra-page')->setPriority(0.5)->addAlternate('/extra-pagina', 'nl')) ->writeToFile($sitemapPath); ``` Note the ```addAlternate``` function which takes an alternate URL and the locale it belongs to. #### Adding images to links Urls can also have images. See also https://developers.google.com/search/docs/advanced/sitemaps/image-sitemaps ```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); ``` #### Adding videos to links As well as images, videos can be wrapped by URL tags. See https://developers.google.com/search/docs/crawling-indexing/sitemaps/video-sitemaps You can set required attributes like so: ```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); ``` If you want to pass the optional parameters like `family_friendly`, `live`, or `platform`: ```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); ``` ### Manually creating a sitemap You can also create a sitemap fully manual: ```php use Carbon\Carbon; Sitemap::create() ->add('/page1') ->add('/page2') ->add(Url::create('/page3')->setLastModificationDate(Carbon::create('2016', '1', '1'))) ->writeToFile($sitemapPath); ``` ### Creating a sitemap index You can create a sitemap index: ```php use Spatie\Sitemap\SitemapIndex; SitemapIndex::create() ->add('/pages_sitemap.xml') ->add('/posts_sitemap.xml') ->writeToFile($sitemapIndexPath); ``` You can pass a `Spatie\Sitemap\Tags\Sitemap` object to manually set the `lastModificationDate` property. ```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); ``` the generated sitemap index will look similar to this: ```xml <?xml version="1.0" encoding="UTF-8"?> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <loc>http://www.example.com/pages_sitemap.xml</loc> <lastmod>2016-01-01T00:00:00+00:00</lastmod> </sitemap> <sitemap> <loc>http://www.example.com/posts_sitemap.xml</loc> <lastmod>2015-12-31T00:00:00+00:00</lastmod> </sitemap> </sitemapindex> ``` ### Create a sitemap index with sub-sequent sitemaps You can call the `maxTagsPerSitemap` method to generate a sitemap that only contains the given amount of tags ```php use Spatie\Sitemap\SitemapGenerator; SitemapGenerator::create('https://example.com') ->maxTagsPerSitemap(20000) ->writeToFile(public_path('sitemap.xml')); ``` ## Generating the sitemap frequently Your site will probably be updated from time to time. In order to let your sitemap reflect these changes, you can run the generator periodically. The easiest way of doing this is to make use of Laravel's default scheduling capabilities. You could set up an artisan command much like this one: ```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')); } } ``` That command should then be scheduled in the console kernel. ```php // app/Console/Kernel.php protected function schedule(Schedule $schedule) { ... $schedule->command('sitemap:generate')->daily(); ... } ``` ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. ## Testing First start the test server in a separate terminal session: ``` bash cd tests/server ./start_server.sh ``` With the server running you can execute the tests: ``` bash $ composer test ``` ## Contributing Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. ## Security If you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker. ## Credits - [Freek Van der Herten](https://github.com/freekmurze) - [All Contributors](../../contributors) ## Support us Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource). Does your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/spatie). All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff. ## License The MIT License (MIT). Please see [License File](LICENSE.md) for more information. resources/views/sitemapIndex/index.blade.php 0000644 00000000353 15111420047 0015227 0 ustar 00 <?= '<'.'?'.'xml version="1.0" encoding="UTF-8"?>'."\n" ?> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> @foreach($tags as $tag) @include('sitemap::sitemapIndex/' . $tag->getType()) @endforeach </sitemapindex> resources/views/sitemapIndex/sitemap.blade.php 0000644 00000000352 15111420047 0015561 0 ustar 00 <sitemap> @if (! empty($tag->url)) <loc>{{ url($tag->url) }}</loc> @endif @if (! empty($tag->lastModificationDate)) <lastmod>{{ $tag->lastModificationDate->format(DateTime::ATOM) }}</lastmod> @endif </sitemap> resources/views/news.blade.php 0000644 00000000651 15111420047 0012443 0 ustar 00 <news:news> <news:publication> <news:name>{{ $news->name }}</news:name> <news:language>{{ $news->language }}</news:language> </news:publication> <news:title>{{ $news->title }}</news:title> <news:publication_date>{{ $news->publicationDate->toW3cString() }}</news:publication_date> @foreach($news->options as $tag => $value) <news:{{$tag}}>{{$value}}</news:{{$tag}}> @endforeach </news:news>