Installation Add the bundle via Composer:
composer require darvinstudio/darvin-rss-bundle
Enable the bundle in config/bundles.php:
DarvinStudio\DarvinRssBundle\DarvinRssBundle::class => ['all' => true],
First Use Case: Generate a Basic RSS Feed Create a controller method to generate an RSS feed:
use DarvinStudio\DarvinRssBundle\Generator\RssGenerator;
use Symfony\Component\HttpFoundation\Response;
public function generateRss(RssGenerator $rssGenerator): Response
{
$feed = $rssGenerator->generate([
'title' => 'My Blog',
'link' => '/',
'description' => 'Latest blog posts',
'items' => [
[
'title' => 'First Post',
'link' => '/post/1',
'description' => 'Content of the first post',
'pubDate' => new \DateTime(),
],
],
]);
return new Response($feed, 200, ['Content-Type' => 'application/rss+xml']);
}
Routing
Add a route in config/routes.yaml:
rss_feed:
path: /rss
controller: App\Controller\RssController::generateRss
View the Feed
Visit /rss in your browser or use a tool like RSS Validator to validate.
Fetch data from a repository or service and pass it to the generator:
public function generateBlogRss(PostRepository $postRepo, RssGenerator $rssGenerator): Response
{
$posts = $postRepo->findLatest(10);
$items = array_map(function ($post) {
return [
'title' => $post->title,
'link' => $post->getAbsoluteUrl(),
'description' => $post->excerpt,
'pubDate' => new \DateTime($post->publishedAt),
];
}, $posts);
$feed = $rssGenerator->generate([
'title' => 'My Blog',
'link' => '/',
'description' => 'Latest posts',
'items' => $items,
]);
return new Response($feed, 200, ['Content-Type' => 'application/rss+xml']);
}
Extend the generator or use closures for dynamic fields:
$feed = $rssGenerator->generate([
'title' => 'Custom Feed',
'items' => array_map(function ($item) {
return [
'title' => $item->title,
'link' => $item->url,
'description' => $item->content,
'customField' => $item->getCustomData(), // Custom logic
'pubDate' => new \DateTime($item->date),
'enclosure' => [
'url' => $item->mediaUrl,
'length' => $item->mediaSize,
'type' => $item->mediaType,
],
];
}, $items),
]);
Cache the generated RSS feed to reduce load:
use Symfony\Contracts\Cache\CacheInterface;
public function generateCachedRss(CacheInterface $cache, RssGenerator $rssGenerator): Response
{
$feed = $cache->get('rss_feed', function () use ($rssGenerator) {
return $rssGenerator->generate([
'title' => 'Cached Feed',
'items' => $this->getFeedItems(),
]);
});
return new Response($feed, 200, ['Content-Type' => 'application/rss+xml']);
}
Render RSS feeds in templates (if needed for partials):
{% embed '@DarvinRss/partials/feed.html.twig' %}
{% block feed_items %}
{% for item in feed.items %}
<item>
<title>{{ item.title }}</title>
<link>{{ item.link }}</link>
<description>{{ item.description }}</description>
</item>
{% endfor %}
{% endblock %}
{% endembed %}
Inject RssGenerator directly into services or controllers for reusability.
Dispatch events when feed items are created/updated to trigger feed regeneration:
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
public function __construct(
private EventDispatcherInterface $dispatcher
) {}
public function createPost(Post $post)
{
$this->dispatcher->dispatch(new FeedUpdatedEvent($post));
}
Use tools like RSS Validator to ensure compliance.
Pass localized strings to the generator:
$feed = $rssGenerator->generate([
'title' => $this->translator->trans('feed.title'),
'description' => $this->translator->trans('feed.description'),
// ...
]);
Ensure pubDate is a valid \DateTime object or a string parsable by DateTime. Invalid dates may break RSS validation:
// Bad:
'pubDate' => 'now', // Ambiguous
// Good:
'pubDate' => new \DateTime(), // Explicit
RSS feeds must use UTF-8 encoding. Ensure your response headers include:
return new Response($feed, 200, [
'Content-Type' => 'application/rss+xml; charset=UTF-8',
]);
The bundle likely handles escaping, but manually adding HTML/XML to fields (e.g., description) may cause issues. Use htmlspecialchars or the bundle’s built-in escaping if available.
The bundle was last updated in 2020 and may not support newer Symfony versions (e.g., 6.x). Test thoroughly or fork the package if needed.
The bundle does not auto-update feeds when data changes. Implement a cron job, event listener, or cache invalidation strategy.
Use RSS Validator to catch malformed XML or non-compliant fields.
Temporarily log the output to debug:
file_put_contents(
'var/log/rss_feed.xml',
$rssGenerator->generate($data)
);
Enable Symfony’s debug mode to catch compatibility issues:
APP_DEBUG=1 php bin/console server:run
Extend the RssGenerator class to add custom fields or logic:
namespace App\Rss;
use DarvinStudio\DarvinRssBundle\Generator\RssGenerator as BaseGenerator;
class CustomRssGenerator extends BaseGenerator
{
public function generate(array $data): string
{
// Add custom logic (e.g., modify XML structure)
return parent::generate($data);
}
}
Register the service in services.yaml:
services:
App\Rss\CustomRssGenerator:
tags: ['rss.generator']
Create a Twig extension to generate feeds in templates:
namespace App\Twig;
use DarvinStudio\DarvinRssBundle\Generator\RssGenerator;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class RssExtension extends AbstractExtension
{
public function __construct(private RssGenerator $rssGenerator) {}
public function getFunctions(): array
{
return [
new TwigFunction('generate_rss', [$this, 'generateRss']),
];
}
public function generateRss(array $data): string
{
return $this->rssGenerator->generate($data);
}
}
The bundle may use Twig templates for rendering. Override them in templates/bundles/DarvinRss/ to customize
How can I help you explore Laravel packages today?