Customization

Adding an RSS Feed to Your Prezet Blog

If you want your Prezet blog to be syndicated via RSS, you can easily integrate Spatie's Laravel Feed package to generate an RSS (Atom) feed for your content. You can find the RSS for this website at prezet.com/feed.

This guide walks you through the process of setting up your own RSS feed.

#Step 1. Install the Required Package

The RSS feed functionality is powered by Spatie's Laravel Feed package. If you haven't already added it to your project, run:

1composer require spatie/laravel-feed

#Step 2. Run the Package Installer

Run the package installer which will publish the configuration file to your config directory and add the necessary views to the public/vendor/feed. Execute the following command:

1php artisan feed:install

After running this command, a config/feed.php file will be generated. You can customize it as needed. For more details, check out the Spatie Laravel Feed documentation.

#Step 3. Update Web Routes

To register the feed route, update your routes/web.php file with the following:

1Route::feeds();

#Step 4. Create the RSS Document Model

Next, create a model that extends Prezet's document model and implements the Feedable interface provided by Spatie. For example, create a new file named RssDocument.php in your app/Models directory:

1<?php
2 
3namespace App\Models;
4 
5use BenBjurstrom\Prezet\Models\Document as DocumentModel;
6use Spatie\Feed\Feedable;
7use Spatie\Feed\FeedItem;
8 
9class RssDocument extends DocumentModel implements Feedable
10{
11 public $table = 'documents';
12 
13 public static function getAllFeedItems()
14 {
15 return self::query()
16 ->orderBy('created_at', 'desc')
17 ->get();
18 }
19 
20 public function toFeedItem(): FeedItem
21 {
22 $authors = config('prezet.authors');
23 $author = $authors[$this->frontmatter->author]['name'] ?? 'Unknown';
24 return FeedItem::create()
25 ->id($this->id)
26 ->title($this->frontmatter->title)
27 ->summary($this->frontmatter->excerpt)
28 ->updated($this->created_at)
29 ->link(route('prezet.show', $this->slug))
30 ->authorName($author);
31 }
32}

This model defines a getAllFeedItems method to fetch the documents that should appear in your feed and a toFeedItem method to transform each document into the required RSS item format.

#Step 5. Add a Link to Your Feed

Let your visitors know that an RSS feed is available by adding an alternate link in your site's <head> section. For example, in your Blade template (typically found at resources/views/vendor/prezet/components/template.blade.php), add the following just before your scripts:

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

#Conclusion

By following these steps, you've successfully integrated an RSS (Atom) feed into your Prezet powered blog. Now your visitors and feed readers can subscribe to stay updated with your latest documentation!