## Getting Started
### Minimal Setup
1. **Install the package**:
```bash
composer require rumenx/php-feed
php artisan vendor:publish --provider="RumenDamyanov\Feed\FeedServiceProvider" --tag="config"
RumenDamyanov\Feed\FeedItem):
namespace App\Feeds;
use RumenDamyanov\Feed\FeedItem;
class BlogPostItem extends FeedItem
{
public function __construct(
public string $title,
public string $description,
public string $url,
public string $date,
public string $author
) {}
}
use RumenDamyanov\Feed\Feed;
use App\Feeds\BlogPostItem;
$feed = new Feed('RSS', 'https://example.com/feed');
$feed->addItem(new BlogPostItem(
'Post Title',
'Post description...',
'https://example.com/post',
now()->toAtomString(),
'Author Name'
));
return $feed->render();
Feed class to create an RSS/Atom feed for blog posts.FeedItem objects.Route::get('/feed', function () {
$posts = Post::latest()->take(10)->get();
$feed = new Feed('RSS', url('/feed'));
$feed->setTitle('My Blog');
$feed->setDescription('Latest blog posts');
foreach ($posts as $post) {
$feed->addItem(new BlogPostItem(
$post->title,
$post->excerpt,
route('posts.show', $post),
$post->published_at->toAtomString(),
$post->author->name
));
}
return $feed->render();
});
FeedItem objects.$feed = new Feed('Atom', url('/feed'));
$feed->setTitle('Product Updates');
foreach (ProductRelease::published()->get() as $release) {
$feed->addItem(new ProductReleaseItem(
$release->title,
$release->summary,
route('releases.show', $release),
$release->released_at->toAtomString(),
'Team Name'
));
}
config/feed.php:
'cache' => [
'driver' => 'file', // or 'database', 'redis', etc.
'prefix' => 'feed_',
'ttl' => 3600, // Cache for 1 hour
],
Feed class with caching enabled:
$feed = new Feed('RSS', url('/feed'), [
'cache' => true,
]);
php artisan vendor:publish --provider="RumenDamyanov\Feed\FeedServiceProvider" --tag="views"
resources/views/vendor/feed/rss.blade.php or atom.blade.php.FeedItem properties or use setViewData():
$feed->setViewData(['custom_key' => 'custom_value']);
Feed class to the container for easier testing and reuse.// In a service provider
$this->app->bind(Feed::class, function ($app) {
return new Feed('RSS', url('/feed'));
});
// In a controller
public function __construct(private Feed $feed) {}
public function showFeed() {
$this->feed->addItem(...);
return $this->feed->render();
}
$category = request('category', 'all');
$feed = new Feed('Atom', url('/feed'), [
'title' => "Category: $category",
]);
if ($category === 'all') {
$items = Post::latest()->take(10)->get();
} else {
$items = Post::where('category', $category)->latest()->take(10)->get();
}
foreach ($items as $item) { ... }
$feed->addItem(new FeedItem(
'Post Title',
'Description',
route('posts.show', $post), // Dynamic URL
$post->published_at->toAtomString(),
$post->author->name
));
$feed = new Feed('RSS', url('/feed'));
return view('feed', ['feed' => $feed->render()]);
FeedItem objects in unit tests:
$mockItem = Mockery::mock(FeedItem::class);
$mockItem->shouldReceive('toArray')->andReturn([
'title' => 'Test',
'description' => 'Test',
// ...
]);
$feed->addItem($mockItem);
Date Formatting:
Carbon instances and chain toAtomString() or toRssString():
$post->published_at->toAtomString(); // Correct
// Avoid: $post->published_at->format('Y-m-d') // May not be compatible
Caching Quirks:
Cache::tags(['feed'])->put()).URL Generation:
url() helper or named routes:
// Bad: 'https://example.com/post'
// Good: url('posts/1') or route('posts.show', $post)
Character Encoding:
&, <, >) in titles/descriptions may break XML.htmlspecialchars() or let the package handle escaping (it does by default).Feed Item Order:
$sortedPosts = Post::latest()->get();
foreach ($sortedPosts as $post) { ... }
Validate Feed Output:
$feed->render(); // Throws exceptions for invalid data
Check Cache Storage:
php artisan cache:clear
file driver):
ls storage/framework/cache/
Template Overrides:
resources/views/vendor/feed/ and the filename matches exactly (e.g., rss.blade.php for RSS).FeedItem to add custom properties and logic:
class CustomFeedItem extends FeedItem
{
public function __construct(
public string $title,
public string $description,
public string $url,
public string $date,
public string $
How can I help you explore Laravel packages today?