Skip to content

Commit

Permalink
Implement RSS feed generation for posts and update routes; add locali…
Browse files Browse the repository at this point in the history
…zation support in HTML
  • Loading branch information
abdessamadbettal committed Dec 14, 2024
1 parent a804c97 commit 42fe5da
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 9 deletions.
20 changes: 20 additions & 0 deletions _ide_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14642,6 +14642,16 @@
*/ public static function inertia($uri, $component, $props = [])
{
return \Illuminate\Routing\Router::inertia($uri, $component, $props);
}
/**
*
*
* @see \Spatie\Feed\FeedServiceProvider::registerRouteMacro()
* @param mixed $baseUrl
* @static
*/ public static function feeds($baseUrl = '')
{
return \Illuminate\Routing\Router::feeds($baseUrl);
}
}
/**
Expand Down Expand Up @@ -20000,6 +20010,16 @@
*/ public static function inertia($uri, $component, $props = [])
{
return \Illuminate\Routing\Router::inertia($uri, $component, $props);
}
/**
*
*
* @see \Spatie\Feed\FeedServiceProvider::registerRouteMacro()
* @param mixed $baseUrl
* @static
*/ public static function feeds($baseUrl = '')
{
return \Illuminate\Routing\Router::feeds($baseUrl);
}
}
/**
Expand Down
58 changes: 52 additions & 6 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@

use Carbon\Carbon;
use Spatie\Tags\HasTags;
use Spatie\Feed\Feedable;
use Spatie\Feed\FeedItem;
use Illuminate\Support\Str;
use Spatie\Sitemap\Tags\Url;
use Spatie\MediaLibrary\HasMedia;
use Spatie\Sluggable\SlugOptions;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
use Spatie\Sitemap\Contracts\Sitemapable;
use Illuminate\Support\Collection;
use Spatie\Sluggable\HasTranslatableSlug;
use Spatie\MediaLibrary\InteractsWithMedia;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

class Post extends Model implements HasMedia , Sitemapable
class Post extends Model implements HasMedia , Feedable
{
use HasFactory, HasTranslations, HasTranslatableSlug, SoftDeletes, InteractsWithMedia, HasTags;

const FEED_PAGE_SIZE = 20;

protected $fillable = ['name', 'slug', 'content', 'author_id', 'category_id', 'is_published', 'time_to_read'];

public $translatable = ['name', 'slug', 'content'];
Expand All @@ -41,14 +47,34 @@ public function getSlugOptions(): SlugOptions
->saveSlugsTo('slug');
}

public function toSitemapTag(): Url | string | array
public function toFeedItem(): FeedItem
{
return FeedItem::create()
->id($this->id)
->title($this->name)
->summary($this->excerpt())
->updated($this->updated_at)
->link($this->url())
->authorName($this->author->name)
->authorEmail($this->author->email);
}

public static function getFeedItems(): Collection
{
return Url::create(route('blog.post.show', $this))
->setLastModificationDate(Carbon::create($this->updated_at))
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority(0.1);
return self::published()
->recent()
->paginate(self::FEED_PAGE_SIZE)
->getCollection();
}

// public function toSitemapTag(): Url | string | array
// {
// return Url::create(route('blog.post.show', $this))
// ->setLastModificationDate(Carbon::create($this->updated_at))
// ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
// ->setPriority(0.1);
// }


/**
* Get the route key for the model.
Expand All @@ -60,6 +86,11 @@ public function getRouteKeyName()
return 'slug';
}

// public function excerpt(int $limit = 100): string
// {
// return Str::limit(strip_tags(md_to_html($this->body())), $limit);
// }

public function author()
{
return $this->belongsTo(User::class, 'author_id');
Expand All @@ -80,4 +111,19 @@ public function tags(): MorphToMany
->orderBy('order_column');
}

public function scopePublished($query)
{
return $query->where('is_published', true);
}

public function scopeRecent($query)
{
return $query->orderBy('created_at', 'desc');
}

public function url()
{
return route('posts.show', $this);
}

}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"rawilk/filament-password-input": "^2.0",
"solution-forest/filament-translate-field": "^1.3",
"spatie/laravel-activitylog": "^4.9",
"spatie/laravel-feed": "^4.4",
"spatie/laravel-medialibrary": "^11.9",
"spatie/laravel-missing-page-redirector": "^2.10",
"spatie/laravel-permission": "^6.10",
Expand Down
94 changes: 93 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions config/feed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

use App\Models\Post;

return [
'feeds' => [
'main' => [
/*
* Here you can specify which class and method will return
* the items that should appear in the feed. For example:
* [App\Model::class, 'getAllFeedItems']
*
* You can also pass an argument to that method. Note that their key must be the name of the parameter:
* [App\Model::class, 'getAllFeedItems', 'parameterName' => 'argument']
*/
'items' => '',

/*
* The feed will be available on this url.
*/
'url' => '',

'title' => 'Laravel starter Forum RSS Feed',
'description' => 'The Laravel starter Forum RSS feed',
'language' => 'en-US',

/*
* The image to display for the feed. For Atom feeds, this is displayed as
* a banner/logo; for RSS and JSON feeds, it's displayed as an icon.
* An empty value omits the image attribute from the feed.
*/
'image' => '',

/*
* The format of the feed. Acceptable values are 'rss', 'atom', or 'json'.
*/
'format' => 'atom',

/*
* The view that will render the feed.
*/
'view' => 'feed::atom',

/*
* The mime type to be used in the <link> tag. Set to an empty string to automatically
* determine the correct value.
*/
'type' => '',

/*
* The content type for the feed response. Set to an empty string to automatically
* determine the correct value.
*/
'contentType' => '',
],
'posts' => [
'items' => [Post::class, 'getFeedItems'],

'url' => '',

'title' => 'Laravel starter Blog RSS Feed',
'description' => 'The Laravel starter Blog RSS feed',
'language' => 'en-US',

'image' => '',

'format' => 'atom',

'view' => 'feed::atom',

'type' => '',

'contentType' => '',
],
],
];
Loading

0 comments on commit 42fe5da

Please sign in to comment.