- Generate XML feeds (RSS & Atom formats)
- Items based on your entities
- Read XML feeds and populate your Symfony entities
- Easy to configurate & use
- Available on packagist (to install via composer)
- Dump your feeds into a file via a Symfony console command
Add this to deps
[EkoFeedBundle]
git=https://github.com/eko/FeedBundle.git
target=/bundles/Eko/FeedBundle
Add this to app/AppKernel.php
<?php
public function registerBundles()
{
$bundles = array(
...
new Eko\FeedBundle\EkoFeedBundle(),
);
...
return $bundles;
}
Add this to app/autoload.php
<?php
$loader->registerNamespaces(array(
...
'Eko' => __DIR__.'/../vendor/bundles'
));
The following configuration lines are required:
eko_feed:
feeds:
article:
title: 'My articles/posts'
description: 'Latests articles'
link: 'http://vincent.composieux.fr'
encoding: 'utf-8'
author: 'Vincent Composieux' # Only required for Atom feeds
Each entities you will use to generate an RSS feed needs to implement Eko\FeedBundle\Item\Writer\ItemInterface
or Eko\FeedBundle\Item\Writer\RoutedItemInterface
as demonstrated in this example for an Article
entity of a blog:
<?php
namespace Bundle\BlogBundle\Entity;
use Eko\FeedBundle\Item\Writer\ItemInterface;
/**
* Bundle\BlogBundle\Entity\Article
*/
class Article implements ItemInterface
{
In this same entity, just implement those required methods:
public function getFeedItemTitle() { … }
: this method returns entity item titlepublic function getFeedItemDescription() { … }
: this method returns entity item description (or content)public function getFeedItemPubDate() { … }
: this method returns entity item publication datepublic function getFeedItemLink() { … }
: this method returns entity item link (URL)
Alternatively, if you need to make use of the router service to generate the link for your entity you can use the following interface. You don't need to worry about injecting the router to your entity.
<?php
namespace Bundle\BlogBundle\Entity;
use Eko\FeedBundle\Item\Writer\RoutedItemInterface;
/**
* Bundle\BlogBundle\Entity\Article
*/
class Article implements RoutedItemInterface
{
In this entity, you'll need to implement the following methods:
public function getFeedItemTitle() { … }
: this method returns entity item titlepublic function getFeedItemDescription() { … }
: this method returns entity item description (or content)public function getFeedItemPubDate() { … }
: this method returns entity item publication datepublic function getFeedItemRouteName() { … }
: this method returns the name of the routepublic function getFeedItemRouteParameters() { … }
: this method must return an array with the parameters that are required for the routepublic function getFeedItemUrlAnchor() { … }
: this method returns the anchor that will be appended to the router-generated url. Note: can be an empty string.
The action now takes place in your controller. Just declare a new action with those examples lines:
<?php
namespace Bundle\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class BlogController extends Controller
{
/**
* Generate the article feed
*
* @return Response XML Feed
*/
public function feedAction()
{
$articles = $this->getDoctrine()->getRepository('BundleBlogBundle:Article')->findAll();
$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->addFromArray($articles);
return new Response($feed->render('rss')); // or 'atom'
}
}
Please note that for better performances you can add a cache control.
You can add custom fields to main channel by adding them this way:
<?php
$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->add(new FakeEntity());
$feed->addChannelField(new ChannelField('custom_name', 'custom_value'));
You can add custom items fields for your entities nodes by adding them this way:
<?php
$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->add(new FakeEntity());
$feed->addItemField(new ItemField('fake_custom', 'getFeedItemCustom'));
You can also add group item fields using this way, if your method returns an array:
<?php
$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->add(new FakeEntity());
$feed->addItemField(
new GroupItemField('categories', new ItemField('category', 'getFeedCategoriesCustom'))
);
Of course, getFeedItemCustom()
method needs to be declared in your entity.
Moreover, entities objects can be added separatly with add method:
<?php
$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->add($article);
You can dump your feeds into a .xml file if you don't want to generate it on the fly by using the php app/console eko:feed:dump
Symfony command.
Here are the options :
Option | Description |
---|---|
--name | Feed name defined in eko_feed configuration |
--entity | Entity to use to generate the feed |
--filename | Defines feed filename |
--orderBy | Order field to sort by using findBy() method |
--direction | Direction to give to sort field with findBy() method |
--format | Formatter to use to generate, "rss" is default |
--limit | Defines a limit of entity items to retrieve |
An example with all the options:
php app/console eko:feed:dump --name=article --entity=AcmeDemoBundle:Fake --filename=test.xml --format=atom --orderBy=id --direction=DESC
This will result:
Start dumping "article" feed from "AcmeDemoBundle:Fake" entity...
done!
Feed has been dumped and located in "/Users/vincent/dev/perso/symfony/web/test.xml"
For any question, do not hesitate to contact me and/or participate.
If you only want to read an XML feed, here is the way:
<?php
$reader = $this->get('eko_feed.feed.reader');
$feed = $reader->load('http://php.net/feed.atom')->get();
$feed
will be a \Zend\Feed\Reader\Feed\FeedInterface
that you can manipulate.
You can also populate an entity from an XML feed. This is very easy.
Just load the feed and call the populate method with your entity name which needs to implement Eko\FeedBundle\Item\Reader\ItemInterface
, take a look on this example:
<?php
$reader = $this->get('eko_feed.feed.reader');
$items = $reader->load('http://php.net/feed.atom')->populate('MyNamespace\Entity\Name');
In this example, $items
will be an array that will contains an array with your entities populated with the given feed content.
-
Vincent Composieux [email protected] (Twitter: @vcomposieux)
-
Rob Masters [email protected]
-
Anyone want to contribute ? Do not hesitate, you will be listed here!