mbpp's avatar
Level 3

Rss feed generator

Hi guys, i have a news website, and i wish to create a rss feed, does anybody knows a package available and easy to implement on laravel.

0 likes
3 replies
mbpp's avatar
Level 3

Thanks a lot for sharing, but im having some issues, im trying to select the rss view, but is showing a different view, i believe is the "atom" view, because is showing different name tags, instead of appearing "item" tags in rss, is giving me "entry".

Here is my code:

Route: Route::get('news/feed', 'PostController@rssFeed');

Post controller:
public function rssFeed(){
         // create new feed
    $feed = App::make("feed");

    // multiple feeds are supported
    // if you are using caching you should set different cache keys for your feeds

    // cache the feed for 60 minutes (second parameter is optional)
    $feed->setCache(60, 'laravelFeedKey');

    // check if there is cached feed and build new only if is not
    if (!$feed->isCached())
    {
       // creating rss feed with our most recent 20 posts
       $posts = \DB::table('posts')->orderBy('created_at', 'desc')->take(20)->get();

       // set your feed's title, description, link, pubdate and language
       $feed->title = 'some title';
       $feed->description = 'Noticias';
       $feed->logo = 'http://dmain.com/my.png';
       $feed->link = url('feed');
       $feed->setDateFormat('datetime'); // 'datetime', 'timestamp' or 'carbon'
       $feed->pubdate = $posts[0]->created_at;
       $feed->lang = 'pt';
       $feed->setShortening(true); // true or false
       $feed->setTextLimit(100); // maximum length of description text

       foreach ($posts as $post)
       {
           // set item's title, author, url, pubdate, description, content, enclosure (optional) URL::to($post->slug)*
           $feed->add($post->title, $post->user_id, "http://domain.com", $post->created_at, $post->sub_title, $post->body);
       }

    }
    $feed->ctype = "text/xml";
    // first param is the feed format
    // optional: second param is cache duration (value of 0 turns off caching)
    // optional: you can set custom cache key with 3rd param as string
    return $feed->render('rss');
   // return $feed->setView('admin.rss');

    // to return your feed as a string set second param to -1
    // $xml = $feed->render('atom', -1);
    }

ohffs's avatar

Not sure - 'it worked for me' ;-) I used it about a year ago mind you. See if setting the ctype to 'application/rss+xml'' (or not setting it at all) works though?

Please or to participate in this conversation.