Installation:
composer require desarrolla2/rss-client-bundle
Register the bundle in app/Kernel.php:
new Desarrolla2\Bundle\RSSClientBundle\RSSClientBundle(),
Basic Configuration:
Add RSS channels in config/packages/rss_client.yaml (or config.yml for older Laravel versions):
rss_client:
channels:
news:
- 'https://example.com/rss-feed'
- 'https://example.com/another-feed'
cache:
ttl: 3600 # Cache TTL in seconds (default: 3600)
First Use Case: Fetch and display RSS items in a controller:
use Desarrolla2\Bundle\RSSClientBundle\Service\RSSClient;
public function index(RSSClient $rssClient)
{
$items = $rssClient->getItems('news'); // 'news' is the channel name
return view('rss.items', compact('items'));
}
Fetching and Displaying Feeds:
RSSClient service in controllers or services.$items = $rssClient->getItems('news', 5);
Customizing Item Processing:
Desarrolla2\Bundle\RSSClientBundle\Service\RSSItemInterface.class CustomRSSItem extends AbstractRSSItem
{
public function getTitle(): string
{
return '[Custom] ' . parent::getTitle();
}
}
services.yaml:
servicios.rss_item:
class: App\Service\CustomRSSItem
tags: ['rss_client.item']
Caching Strategies:
$rssClient->clearCache('news');
Handling Multiple Channels:
news, tech) for easier management.rss_client:
channels:
tech_news:
- 'https://tech-site.com/rss'
- 'https://another-tech-site.com/feed'
Integration with Twig:
return view('rss.index', [
'newsItems' => $rssClient->getItems('news'),
'techItems' => $rssClient->getItems('tech_news'),
]);
{% for item in newsItems %}
<h3>{{ item.title }}</h3>
<p>{{ item.description|truncate(100) }}</p>
{% endfor %}
Scheduled Fetching:
// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$rssClient = app('rss_client');
$rssClient->fetchAllChannels();
})->hourly();
}
Feed Parsing Errors:
try {
$items = $rssClient->getItems('news');
} catch (\Exception $e) {
Log::error('RSS fetch failed: ' . $e->getMessage());
$items = []; // Fallback to empty array
}
Caching Issues:
php bin/console cache:clear
$rssClient->clearCache(); // Clears all channels
$rssClient->clearCache('news'); // Clears a specific channel
Channel Configuration:
config.yml match those used in code (case-sensitive).tech-news instead of tech/news).Rate Limiting:
ttl) to avoid hitting limits:
rss_client:
cache:
ttl: 10800 # 3 hours
SSL/TLS Issues:
$client = new \GuzzleHttp\Client([
'curl' => [
CURLOPT_SSL_VERIFYPEER => false,
],
]);
// Inject the client into the bundle (requires customization)
Large Feeds:
$items = $rssClient->getItems('news', 10); // Fetch only 10 items
Custom Item Fields:
AbstractRSSItem class to add custom fields (e.g., getAuthor(), getImageUrl()):
class ExtendedRSSItem extends AbstractRSSItem
{
public function getAuthor(): ?string
{
return $this->getElement('author') ?? null;
}
}
Logging:
rss_client:
logging: true
storage/logs/laravel.log.Testing:
RSSClient service in tests:
$this->mock(RSSClient::class)
->shouldReceive('getItems')
->with('news')
->andReturn([/* mock items */]);
Performance Optimization:
FetchRSSFeedsJob::dispatch('news')->onQueue('rss');
class FetchRSSFeedsJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;
public function handle(RSSClient $rssClient)
{
$rssClient->fetchChannel($this->channelName);
}
}
Dynamic Channel Configuration:
ChannelManager service.Handling Different RSS Formats:
RSSClient class and override the parseFeed method.Security:
use Symfony\Component\DomCrawler\Crawler;
$cleanedDescription = (new Crawler($item->getDescription()))->text();
How can I help you explore Laravel packages today?