PHP code example of eko / feedbundle

1. Go to this page and download the library: Download eko/feedbundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eko / feedbundle example snippets




return [
    // ...
    Eko\FeedBundle\EkoFeedBundle::class => ['all' => true],
];



namespace Bundle\BlogBundle\Entity;

use Eko\FeedBundle\Item\Writer\ItemInterface;

/**
 * Bundle\BlogBundle\Entity\Article
 */
class Article implements ItemInterface
{



namespace Bundle\BlogBundle\Entity;

use Eko\FeedBundle\Item\Writer\RoutedItemInterface;

/**
 * Bundle\BlogBundle\Entity\Article
 */
class Article implements RoutedItemInterface
{



namespace App\Controller;

use Eko\FeedBundle\Feed\FeedManager;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class BlogController extends AbstractController
{
    /**
     * @var FeedManager
     */
    protected $feedManager;

    /**
     * Constructor.
     * 
     * @param FeedManager $feedManager
     */
    public function __construct(FeedManager $feedManager)
    {
        $this->feedManager = $feedManager;
    }

    /**
     * Generate the article feed
     * 
     * @Route("/feed.rss", name="app_feed")
     *
     * @return Response XML Feed
     */
    public function feed()
    {
        $articles = $this->getDoctrine()->getRepository('BundleBlogBundle:Article')->findAll();

        $feed = $this->feedManager->get('article');
        $feed->addFromArray($articles);

        return new Response($feed->render('rss')); // or 'atom'
    }
}


$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->add($article);


$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->add(new FakeEntity());
$feed->addChannelField(new ChannelField('custom_name', 'custom_value'));


$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->add(new FakeEntity());
$feed->addItemField(new ItemField('fake_custom', 'getFeedItemCustom'));


$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->add(new FakeEntity());
$feed->addItemField(
    new GroupItemField(
        'categories',
        new ItemField('category', 'getFeedCategoriesCustom', array(), array('category-attribute', 'test'),
        array('categories-attribute', 'getAttributeValue')
    )
);

$feed->addItemField(
    new GroupItemField('author', array(
        new ItemField('name', 'getFeedItemAuthorName', array('cdata' => true)),
        new ItemField('email', 'getFeedItemAuthorEmail')
    )
);

$feed->addItemField(
    new GroupItemField('authors', array(
        new GroupItemField('author', array(
            new ItemField('name', 'Vincent', array('cdata' => true)),
            new ItemField('email', '[email protected]')
        )),
        new GroupItemField('author', array(
            new ItemField('name', 'Audrey', array('cdata' => true)),
            new ItemField('email', '[email protected]')
        ))
    )
);

$feed->addChannelField(
    new GroupChannelField('author', array(
        new ChannelField('name', 'My author name'),
        new ChannelField('email', '[email protected]')
    )
);


$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->add(new FakeEntity());
$feed->addItemField(new MediaItemField('getFeedMediaItem'));

/**
 * Returns a custom media field
 *
 * @return string
 */
public function getFeedMediaItem()
{
    return array(
        'type'   => 'image/jpeg',
        'length' => 500,
        'value'  => 'http://website.com/image.jpg'
    );
}



use Eko\FeedBundle\Service\FeedDumpService;

$feedDumpService = $this->get(FeedDumpService::class);
$feedDumpService
        ->setName($name)
        //You can set an entity
        //->setEntity($entity)
        // Or set you Items
        ->setItems($MyOwnItemList)
        ->setFilename($filename)
        ->setFormat($format)
        ->setLimit($limit)
        ->setDirection($direction)
        ->setOrderBy($orderBy)
    ;

$feedDumpService->dump();


$reader = $this->get('eko_feed.feed.reader');
$reader->setHydrator(new DefaultHydrator());
$feed = $reader->load('http://php.net/feed.atom')->get();


$reader = $this->get('eko_feed.feed.reader');
$reader->setHydrator(new DefaultHydrator());
$items = $reader->load('http://php.net/feed.atom')->populate('MyNamespace\Entity\Name');

$reader = $this->get('eko_feed.feed.reader');
$reader->setHydrator(new MyCustomHydrator());

$items = $reader->load('http://php.net/feed.atom')->populate('MyNamespace\Entity\Name');