
Good content takes time and effort to come up with.
Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.
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
.
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
spatie/laravel-feed
PackageRun 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",
}
web.php
Thanks to the Spatie team this comes all built in. Its as simple as the following command
/** In web.php */
Route::feeds();
feed.php
config fileOnce 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"
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();
}
}
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.
Blade
PagesAdd 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">
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
To know more on this package kindly see their repository 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.
Factory States For Clean And Fluent Laravel Testing
Upload File From Frontend Server {GuzzleHttp} To REST API Server In PHP {Laravel}
SummerNote WYSIWYG Text Editor Save Images To Public Path In PHP Laravel
Make Laravel Controllers Slim By Skimming Form Validation Request
Install Packages Parallel For Faster Development In Composer
Generate SSH Key with ssh-keygen In Linux / Unix
What Is Composer? How Does It Work? Useful Composer Commands And Usage
Accessors And Mutators In PHP Laravel
Foreign Key Migrations Simplified In Laravel 7.x
Laravel Last Executed Query In Plain SQL Statement For Debugging
Comment And Like System Using Disqus
Free SSL Certificate For CPanel
Push Files To CPanel / Remote Server using FTP Software FileZilla