Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :
InvestmentNovel

Latest Programming Tutorial Blog

Laravel RSS Feed Tutorial With Example

/
/
/
119 Views

Hello developers, In this example, I will describe to you how to do Laravel RSS Feed Tutorial With Example. Creating an RSS feed in Laravel isn’t the difficult task, but using a package and a few hints can help you create an RSS feed relatively quick. We are going to use the roumen/feed package to walk through going from a brand new Laravel 5.6 application to serving RSS feeds. Currently, this package supports only RSS and Atom, and make Atom the default choice.

RSS stands for Rich Site Summary. It is often called Really Simple Syndication is a type of web feed. Websites usually use RSS feeds to publish regularly updated information, such as blog entries, news headlines, audio, video. RSS also feeds useful for users who want to receive timely updates from popular websites or to aggregate data from many sites.

There are thousands of application such as  Apple News, Google Newsstand, Feedly that allows people to follow and read your content whenever new content is published. Feeds allow webmasters to drive traffic to their website.

Laravel RSS Feed Tutorial With Example

We are going to Configure Laravel Project.

#1: Download Laravel Application

Establish  Laravel Application by the typing following command.

composer create-project --prefer-dist laravel/laravel laravelrssfeed

#2: Configure SQL Database

Now we can setup database credentials.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravelrssfeed
DB_USERNAME=root
DB_PASSWORD=

#3: Install roumen/feed Package

First, install the roumen/feed package via the Composer package manager.

composer require roumen/feed

Or add the following to your composer.json file :

For Laravel 5.6

"roumen/feed": "2.12.*"

For Laravel 5.5

"roumen/feed": "2.11.*"

For Laravel 5.4 and lower

"roumen/feed": "2.10.*"

If you are using laravel 5.5 or higher you can skip the service provider registration.

Let’s Add ServiceProvider in config/app.php

'providers' => [
                 Roumen\Feed\FeedServiceProvider::class,
               ]

Let’s Add an alias config/app.php

'aliases' => [
               'Feed' => Roumen\Feed\Feed::class,
             ]

#4: Build Model and Migration File

php artisan make:model Post -m

It will create a Post.php file and also create a create_posts_table.php  migration file.

//create_posts_table

public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('slug');
            $table->string('author');
            $table->text('description');
            $table->text('content');
            $table->timestamps();
        });
    }

Save and run.

php artisan migrate

#5: Create Dummy data

Next up, open the database/factories/UserFactory.php file and add the following:

$factory->define(App\Post::class, function (Faker $faker) {
    return [
        'title' => $faker->title,
        'slug' => $faker->slug,
        'author' => $faker->title,
        'description' => $faker->text,
        'content' => $faker->text,
    ];
    
});

Run below command in terminal

php artisan tinker
factory(App\Post::class, 50)->create();

Now you can see database post table have 50 records.

 

laravel rssfeed example

#6: Create one controller

$ php artisan make:controller PostController

It will build a controller file called PostController.php.

//PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use FarhanWazir\GoogleMaps\GMaps;


class PostController extends Controller
{
    public function render()
    {
        /* create new feed */
    $feed = \App::make("feed");
     /* cache the feed for 60 minutes */
    $feed->setCache(60);
 
    if (!$feed->isCached())
    {
    /* creating rss feed with our most recent 10 posts */
    $posts = \DB::table('posts')->orderBy('created_at', 'desc')->take(20)->get();
 
 
    /* set your feed's title, description, link, pubdate and language */
    $feed->title = 'Hello';
    $feed->description = 'Investmentnovel';
    $feed->logo = 'https://www.google.co.in/search?q=logo&num=100&rlz=1C1CHBF_enIN743IN743&tbm=isch&source=iu&ictx=1&fir=lMZRWGwAoXsJ5M%253A%252C1FqkMM1hJtCa2M%252C_&usg=__ERH-20j8h9sjprz_SrnLkrx0nf4%3D&sa=X&ved=0ahUKEwi8ssiwr9DbAhXKQo8KHSmlCVwQ9QEIMDAD#imgrc=msBh6MkKlnSlVM:';
    $feed->link = url('feed');
    $feed->setDateFormat('datetime');
    $feed->pubdate = $posts[0]->created_at;
    $feed->lang = 'en';
    $feed->setShortening(true);
    $feed->setTextLimit(100);

 
    foreach ($posts as $post)
    {
        $feed->add($post->title, $post->author, \URL::to($post->slug), $post->created_at, $post->description, $post->content);
    }
 
  }
    return $feed->render('atom');
 }
}

#7: Define Route

We register route in a web.php file.

Route::get('feed', 'PostController@render');

Our feed should be available at  http://localhost:8000/feed now.

 

How to Generate RSS Feed in Laravel Example

#8: Linking to the Feed

You can add an RSS link to the welcome page by adding Feed:: link  in the <head> of the view:

//resources/views/welcome.blade.php

<head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel</title>
        {!!Feed::link(url('feed'), 'atom', 'My Feed')!!}

If you go to the homepage of the project, you’ll get something like the following linktag:

<link rel="alternate" type="application/atom+xml" href="http://localhost:8000/feed" title="My Feed">

Paste this url in your browser view-source:http://localhost:8000/. You can see below screen.

 

How To Create RSS Feed in Laravel Tutorial With Example

At last, our Laravel RSS Feed Tutorial With Example is over.

  • Facebook
  • Twitter
  • Google+
  • Linkedin
  • Pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This div height required for enabling the sticky sidebar