Skip to content
This repository has been archived by the owner on Mar 2, 2024. It is now read-only.

The-3Labs-Team/laravel-google-news-sitemap

Repository files navigation

⚠️ This repo as been archived. Please use our new Laravel Google News Sitemap Package instead.

Laravel Google News Sitemap

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.

Requirements

  • PHP 7.4 or higher
  • Laravel 8.0 or higher

Installation

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"

Usage

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();
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.