paulund / rss-feed
Rss feed package for Laravel
Fund package maintenance!
paulund
Requires
- php: ^8.3
- illuminate/contracts: ^10.0||^11.0
- illuminate/support: ^10.0||^11.0
Requires (Dev)
- larastan/larastan: ^2.0
- laravel/pint: ^1.0
- orchestra/testbench: ^9.4
- pestphp/pest: ^3.0
- rector/rector: ^1.2
This package is auto-updated.
Last update: 2024-12-16 11:41:11 UTC
README
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