paulund/rss-feed

There is no license information available for the latest version (0.0.1) of this package.

Rss feed package for Laravel

Fund package maintenance!
paulund

0.0.1 2024-10-21 19:02 UTC

README

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

A Laravel package that will allow you to generate an RSS feed from your website.

Installation

Install the package via composer:

composer require paulund/rss-feed

Usage

To use this package you need to create the controller that will generate the RSS feed.

This controller will then use the classes in this package to generate your RSS feed.

Create Controller

php artisan make:controller RssFeedController

Add Route

Add a route to your web.php file that will point to the controller.

Route::get('rss', 'RssFeedController@index');

Create RSS Feed

In the controller you need to create a new instance of the FeedMeta class that will create your feed meta information.

$feedMeta = new FeedMeta(
    title: 'RSS Feed Title',
    description: 'This RSS feed description',
    link: 'https://www.example.com/rss',
    pubDate: now()->format(\DateTime::RFC822),
    lastBuildDate: now()->toDateTimeString(),
    category: 'General',
    generator: 'Paulund RSS Feed Generator'
);

Then you can create a collection for the feed items you want to add to the feed.

$feedItems = $blogPosts->map(function ($post): array {
    return [
        'title' => $post->title,
        'description' => $post->description,
        'link' => route('blog.show', $post->slug),
        'pubDate' => (new \DateTime($post->date))->format(\DateTime::RFC822),
        'category' => $post->category,
    ];
});

Then you can create a new instance of the Feed class and pass in the meta and the items. Using the toResponse method you can return the RSS feed response.

return (new Feed($feedMeta, $feedItems))->toResponse();

Full Controller

<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;
use Paulund\RssFeed\Feed;
use Paulund\RssFeed\FeedMeta;

class RssFeedController extends Controller
{
    public function index()
    {
        $blogPosts = Post::all();

        $feedMeta = new FeedMeta(
            title: 'RSS Feed Title',
            description: 'This RSS feed description',
            link: 'https://www.example.com/rss',
            pubDate: now()->format(\DateTime::RFC822),
            lastBuildDate: now()->toDateTimeString(),
            category: 'General',
            generator: 'Paulund RSS Feed Generator'
        );

        $feedItems = $blogPosts->map(function ($post): array {
            return [
                'title' => $post->title,
                'description' => $post->description,
                'link' => route('blog.show', $post->slug),
                'pubDate' => (new \DateTime($post->date))->format(\DateTime::RFC822),
                'category' => $post->category,
            ];
        });

        return (new Feed($feedMeta, $feedItems))->toResponse();
    }
}

Now navigate to the /rss route on your website and you should see the RSS feed.

Testing

composer test