How To Generate RSS Feeds in PHP Laravel

How To Generate RSS Feeds in PHP Laravel

https://ift.tt/2SDxQwT

Hi friends this is sequel of my another article How To Generate Sitemap in PHP Laravel. As I had discussed that for every website SEO plays key and vital role for Google Page ranking.

If your working on WordPress, Drupal, October CMS, Joomla or any other CMS then generating RSS is on tip of your fingers. But it will be bit confusing or difficult to generate in PHP Laravel. In this article I will show you how easily you can generate RSS feed in PHP Laravel.


Prerequisites

Hope you guys have a Blog or CMS developed in Laravel where you want to implement this RSS feed. And if your looking to host and play for free credits then you can setup your server with DigitalOcean, Linode or any other cloud platform


Step 1 – Installing Laravel spatie/laravel-feed Package

Run the following composer command to install the spaite/larvel-feed package.

composer require spatie/laravel-feed 

This will be installed in your vendor folder and added in require key of composer.json which will be as follows

"require": { "spatie/laravel-feed": "^2.6", } 

Step 2 – Add Feed Routes in web.php

Thanks to the Spatie team this comes all built in. Its as simple as the following command

/** In web.php */  Route::feeds(); 

Step 3 – Publish feed.php config file

Once you run the following command it will generate feed.php in config/feed.php where you can do few customisation, which I will explain later.

php artisan vendor:publish --provider="Spatie\Feed\FeedServiceProvider" --tag="config" 

Step 4 – Generate News Feed Code

For example for my Posts which has Post model I want to generate the NewsItem, so first let me generate new model for it via PHP Artisan command

php artisan make:model NewsItem 

This creates NewsItem model in App/NewItem path.

Now paste the following code in NewsItem model

<?php namespace App; use Post; use Carbon\Carbon; use Spatie\Feed\Feedable; use Spatie\Feed\FeedItem; class NewsItem extends Post implements Feedable { public function toFeedItem() { /** This is a standard FeedItem which must return collection and I am just passing the data that is required for it. Feel free to changes as per you convince */ return FeedItem::create([ 'id' => env('APP_URL').'/posts/'.$this->slug, 'title' => $this->title, 'summary' => $this->summary, 'updated' => $this->published_at, 'link' => env('APP_URL').'/posts/'.$this->slug, 'author' => $this->user->name, ]); } /** This function is responsible to get all your NewsItem feeds. This NewsItems gets the data from the previous created feeds. */ public static function getFeedItems() { /** I am getting only the published details */ return NewsItem::published()->orderBy('published_at', 'DESC')->get(); } } 

Step 5 – Customise Functions in config/feed.php

In config/feed.php we can configure the feed fetching function, URL of feed and views.

'items' => 'App\NewsItem@getFeedItems', /** This function we can created in Step 4 */ 'url' => '/feed', /** We can fetch the feed from APP_URL/feed */ 'title' => 'StackCoder', 'description' => 'The description of the feed.', 'language' => 'en-US', 

items – Where to get the feed items from

url – From where you need to access the feeds

title, description, language as per you convenience.


Step 6 – Adding Feed Links In Blade Pages

Add the following line in your blade in head section

<head> @include('feed::links') </head> 

Or you can even add the following html link

<link rel="alternate" type="application/atom+xml" title="News" href="/feed"> 

Step 7 – Testing If Its Working

If you have followed the above steps properly and when you go to your http://project_url/feed then you must be able to see the XML file


Conclusion

To know more on this package kindly see their repository https://github.com/spatie/laravel-feed. This is part of SEO series which I will be releasing soon. Meanwhile if your looking out for generating the sitemap for your Laravel website then following my another article How To Generate Sitemap in PHP Laravel.

Hope it help you. Happy Coding 🙂

programming

via Laravel News Links https://ift.tt/2dvygAJ

May 4, 2020 at 09:03PM