Skip to content

Commit

Permalink
Enhance sitemap generation with multilingual support and update feed …
Browse files Browse the repository at this point in the history
…configuration; adjust pagination size for posts
  • Loading branch information
abdessamadbettal committed Dec 14, 2024
1 parent 42fe5da commit dba00e3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 99 deletions.
83 changes: 39 additions & 44 deletions app/Console/Commands/GenerateSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Spatie\Sitemap\SitemapGenerator;
use App\Models\Post;
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;
use Psr\Http\Message\UriInterface;
use Illuminate\Support\Str;

use Illuminate\Console\Command;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class GenerateSitemap extends Command
{
Expand All @@ -23,55 +22,51 @@ class GenerateSitemap extends Command
*
* @var string
*/
protected $description = 'Crawl the site to generate a sitemap.xml file';
protected $description = 'Generate a sitemap.xml file with multilingual support.';


private array $noIndexPaths = [
'',
'/login/github',
'/login/google',
'/login/facebook',
'/login/twitter',
'/login',
'/register',
'/password/reset',
'/password/confirm',
'/password/email',
'/forgot-password',
'/reset-password/*',
'/email/verify',
'/user/*',
'/profile/*',
];
private array $languages = ['en', 'fr', 'es']; // Define supported languages

/**
* Execute the console command.
*/
public function handle(): void
{
SitemapGenerator::create(config('app.url'))
->shouldCrawl(function (UriInterface $url) {
return $this->shouldIndex($url->getPath());
})
->hasCrawled(function (Url $url) {
if ($this->shouldNotIndex($url->path())) {
return;
}
$sitemap = Sitemap::create();

return $url;
})
->writeToFile(public_path('sitemap.xml'));
// Add static pages
foreach ($this->languages as $lang) {
$homeUrl = LaravelLocalization::getLocalizedURL($lang, route('welcome'));
$sitemap->add(Url::create($homeUrl)
->setChangeFrequency(Url::CHANGE_FREQUENCY_DAILY)
->setPriority(1.0));

$this->info('Sitemap generated');
}
}

private function shouldNotIndex(string $path): bool
{
return Str::is($this->noIndexPaths, $path);
}
// Add paginated posts
$posts = Post::paginate(10);
for ($page = 1; $page <= $posts->lastPage(); $page++) {
foreach ($this->languages as $lang) {
$postsUrl = LaravelLocalization::getLocalizedURL($lang, route('posts.index', ['page' => $page]));
$sitemap->add(Url::create($postsUrl)
->setChangeFrequency(Url::CHANGE_FREQUENCY_DAILY)
->setPriority(0.9));
}
}

private function shouldIndex(string $path): bool
{
return ! $this->shouldNotIndex($path);
// Add individual posts
$posts = Post::all();
foreach ($posts as $post) {
foreach ($this->languages as $lang) {
$url = LaravelLocalization::getLocalizedURL($lang, route('posts.show', $post->slug));
$sitemap->add(Url::create($url)
->setLastModificationDate($post->updated_at)
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority(0.8));
}
}

$sitemap->writeToFile(public_path('sitemap.xml'));

$this->info('Sitemap generated successfully!');
}
}
12 changes: 8 additions & 4 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Post extends Model implements HasMedia , Feedable
{
use HasFactory, HasTranslations, HasTranslatableSlug, SoftDeletes, InteractsWithMedia, HasTags;

const FEED_PAGE_SIZE = 20;
const FEED_PAGE_SIZE = 30;

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

Expand Down Expand Up @@ -55,8 +55,7 @@ public function toFeedItem(): FeedItem
->summary($this->excerpt())
->updated($this->updated_at)
->link($this->url())
->authorName($this->author->name)
->authorEmail($this->author->email);
->authorName($this->author->name);
}

public static function getFeedItems(): Collection
Expand Down Expand Up @@ -123,7 +122,12 @@ public function scopeRecent($query)

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

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

}
51 changes: 1 addition & 50 deletions config/feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,10 @@

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' => '',
'url' => '/posts/feed',

'title' => 'Laravel starter Blog RSS Feed',
'description' => 'The Laravel starter Blog RSS feed',
Expand Down
2 changes: 2 additions & 0 deletions resources/views/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
<link rel="manifest" href="/site.webmanifest" />

@include('feed::links')

<!-- Scripts -->
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-MWK35BWK06"></script>
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;


Route::feeds();
Route::group(
[
'prefix' => LaravelLocalization::setLocale(),
'middleware' => ['localeSessionRedirect', 'localizationRedirect', 'localeViewPath']
],
function () {
Route::feeds();

Route::get('/', [HomeController::class, 'index'])->name('welcome');

Expand Down

0 comments on commit dba00e3

Please sign in to comment.