⚠️ This repo as been archived. Please use our new Laravel Google News Sitemap Package instead.
This package is still in development and can be subject to breaking changes.
This package allows you to generate a Google News sitemap for your Laravel application. It can be used in combination with spatie/laravel-sitemap to generate a sitemap that includes both regular and news sitemaps.
- PHP 7.4 or higher
- Laravel 8.0 or higher
You can install the package via composer:
composer require the-3labs-team/laravel-google-news-sitemap
You can publish and run the migrations with:
php artisan vendor:publish --tag="laravel-google-news-sitemap-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="laravel-google-news-sitemap-config"
This is the contents of the published config file:
return [
];
Optionally, you can publish the views using
php artisan vendor:publish --tag="laravel-google-news-sitemap-views"
The best way to use this package is creating a command that will generate the sitemap and then schedule it to run periodically.
First, you need to create two methods in your model that will be used to generate the sitemap.
<?php
public static function getGoogleNewsFeedItems()
{
Article::query()->wherePublished()
->where('published_at', '>', now()
->subDays(2))
->orderBy('published_at', 'desc')
->limit(50)
->get();
}
public function toGoogleNewsFeedItem(): GoogleNewsFeedItem
{
return GoogleNewsFeedItem::create()
->id($this->id)
->title($this->title) // Change this to the title of your article
->keywords('News') // Change this to the keywords of your article
->publicationDate($this->published_at) // Change this to the publication date of your article
->publicationName($this->getAuthorNameAttribute()) // Change this to the name of your author or publication
->publicationLanguage("it") // Change this to the language of your article
->link($this->route); // Change this to the route of your article
}
You can create the command using the following command:
php artisan make:command GoogleSitemapBuild
Then you can edit the command to look like this:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Article;
class GoogleSitemapBuild extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'google-sitemap:build';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Build the sitemap for Google News';
/**
* Execute the console command.
*/
public function handle()
{
$this->info('Collecting articles...');
$articles = Article::getGoogleNewsFeedItems();
$this->info('Done, ' . $articles->count() . ' articles collected.');
$feeds = [];
$this->info('Building feed items from collected articles...');
foreach ($articles as $article) {
$feeds[] = $article->toGoogleNewsFeedItem();
}
$this->info('Done, ' . count($feeds) . ' feed items built.');
$this->info('Building google news sitemap...');
$xml = view('google-news-feed::rss', [
'items' => $feeds,
])->render();
file_put_contents(public_path('google-news-sitemap.xml'), $xml);
$this->info('Google News Sitemap built successfully!');
return 0;
}
}
Then you can schedule the command to run periodically in your app/Console/Kernel.php
file:
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
class Kernel extends ConsoleKernel
{
protected function schedule(Schedule $schedule)
{
$schedule->command('google-sitemap:build')->everyFiveMinutes()->withoutOverlapping();
}
}
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.