Skip to content

Commit

Permalink
feat: Add ability to generate a sitemap for SEO
Browse files Browse the repository at this point in the history
  • Loading branch information
zooley committed Aug 8, 2023
1 parent 3c14283 commit 2b76a00
Show file tree
Hide file tree
Showing 14 changed files with 1,420 additions and 82 deletions.
39 changes: 39 additions & 0 deletions app/Modules/Core/Console/GenerateSitemapCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Modules\Core\Console;

use Illuminate\Console\Command;
use App\Modules\Core\Events\GenerateSitemap;
use Spatie\Sitemap\Sitemap;

class GenerateSitemapCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sitemap:generate';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate a sitemap.';

/**
* Execute the actions
*
* @return void
*/
public function handle()
{
$map = Sitemap::create();

event($event = new GenerateSitemap($map));

$map = $event->map;
$map->writeToFile(public_path('sitemap.xml'));
}
}
26 changes: 26 additions & 0 deletions app/Modules/Core/Events/GenerateSitemap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Modules\Core\Events;

use Spatie\Sitemap\Sitemap;

class GenerateSitemap
{
/**
* Sitemap object
*
* @var Sitemap
*/
public $map;

/**
* Constructor
*
* @param Sitemap $map
* @return void
*/
public function __construct(Sitemap $map)
{
$this->map = $map;
}
}
14 changes: 14 additions & 0 deletions app/Modules/Core/Providers/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use App\Modules\Core\Http\Middleware\PublicPath;
use App\Modules\Core\Http\Middleware\LegacyFiles;
use App\Modules\Core\Console\GenerateSitemapCommand;
use Illuminate\Support\ServiceProvider;

class CoreServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -36,6 +37,7 @@ public function boot()
$this->registerConfig();
$this->registerAssets();
$this->registerViews();
$this->registerConsoleCommands();

$this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');

Expand All @@ -45,6 +47,18 @@ public function boot()
$kernel->pushMiddleware(LegacyFiles::class);
}

/**
* Register console commands.
*
* @return void
*/
protected function registerConsoleCommands()
{
$this->commands([
GenerateSitemapCommand::class,
]);
}

/**
* Register config.
*
Expand Down
71 changes: 71 additions & 0 deletions app/Modules/Knowledge/Listeners/PageCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace App\Modules\Knowledge\Listeners;

use Illuminate\Events\Dispatcher;
use App\Modules\Knowledge\Models\Page;
use App\Modules\Knowledge\Models\Associations;
use App\Modules\Core\Events\GenerateSitemap;
use Spatie\Sitemap\Tags\Url;

/**
* Listener for sitemap generator
*/
class PageCollector
{
/**
* Register the listeners for the subscriber.
*
* @param Dispatcher $events
* @return void
*/
public function subscribe(Dispatcher $events)
{
$events->listen(GenerateSitemap::class, self::class . '@handleGenerateSitemap');
}

/**
* Add items to the sitemap
*
* @param GenerateSitemap $event
* @return void
*/
public function handleGenerateSitemap(GenerateSitemap $event)
{
$p = (new Page)->getTable();
$a = (new Associations)->getTable();

$options = $lists = Page::query()
->join($a, $a . '.page_id', $p . '.id')
->select($p . '.*', $a . '.level', $a . '.lft', $a . '.rgt', $a . '.id AS assoc_id', $a . '.path AS assoc_path')
->where($a . '.state', '=', 1)
->orderBy($a . '.lft', 'asc')
->get();

foreach ($options as $page)
{
$priority = 0.5;

if ($page->level == 0)
{
$route = route('site.knowledge.index');
$priority = 0.8;
}
else
{
if ($page->level = 1)
{
$priority = 0.7;
}
$route = route('site.knowledge.page', ['uri' => $page->assoc_path]);
}

$event->map->add(
Url::create($route)
->setLastModificationDate($page->updated_at)
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority($priority)
);
}
}
}
2 changes: 2 additions & 0 deletions app/Modules/Knowledge/Providers/KnowledgeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace App\Modules\Knowledge\Providers;

use Illuminate\Support\ServiceProvider;
use App\Modules\Knowledge\Listeners\PageCollector;
use App\Modules\Knowledge\Listeners\RouteCollector;
use App\Modules\Knowledge\Console\ImportCommand;
use Nwidart\Modules\Facades\Module;
Expand Down Expand Up @@ -41,6 +42,7 @@ public function boot(): void
{
$this->app['events']->subscribe(new RouteCollector);
}
$this->app['events']->subscribe(new PageCollector);
}

/**
Expand Down
135 changes: 135 additions & 0 deletions app/Modules/News/Listeners/PageCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace App\Modules\News\Listeners;

use Illuminate\Events\Dispatcher;
use App\Modules\News\Models\Article;
use App\Modules\News\Models\Type;
use App\Modules\Core\Events\GenerateSitemap;
use Spatie\Sitemap\Tags\Url;
use Carbon\Carbon;

/**
* Listener for sitemap generator
*/
class PageCollector
{
/**
* Register the listeners for the subscriber.
*
* @param Dispatcher $events
* @return void
*/
public function subscribe(Dispatcher $events)
{
$events->listen(GenerateSitemap::class, self::class . '@handleGenerateSitemap');
}

/**
* Add items to the sitemap
*
* @param GenerateSitemap $event
* @return void
*/
public function handleGenerateSitemap(GenerateSitemap $event)
{
$event->map->add(
Url::create(route('site.news.index'))
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority(0.7)
);

$event->map->add(
Url::create(route('site.news.search'))
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority(0.5)
);

$event->map->add(
Url::create(route('site.news.rss'))
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority(0.5)
);

$options = Type::query()
//->where('name', 'NOT LIKE', 'coffee%')
->where('parentid', '=', 0)
->orderBy('ordering', 'asc')
->orderBy('name', 'asc')
->get();

foreach ($options as $type)
{
$event->map->add(
Url::create(route('site.news.type', ['name' => $type->alias]))
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority(0.5)
);

$children = $type->children()
->orderBy('ordering', 'asc')
->orderBy('name', 'asc')
->get();

foreach ($children as $child)
{
$event->map->add(
Url::create($route = route('site.news.type', ['name' => $child->alias]))
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
->setPriority(0.5)
);
}
}

$yearago = Carbon::now()->modify('-1 year');

$articles = Article::query()
->select('id', 'datetimenews', 'datetimenewsend', 'datetimecreated', 'datetimeedited')
->where('template', '=', 0)
->where('published', '=', 1)
->get();

foreach ($articles as $article)
{
$priority = 0.5;
$frequency = Url::CHANGE_FREQUENCY_YEARLY;

if ($article->hasEnd())
{
$frequency = Url::CHANGE_FREQUENCY_MONTHLY;

if ($article->ended())
{
$priority = 0.3;
$frequency = Url::CHANGE_FREQUENCY_YEARLY;

// If older than a year...
if ($article->datetimenewsend < $yearago)
{
$priority = 0.1;
$frequency = Url::CHANGE_FREQUENCY_NEVER;
}
}
}
else
{
if ($article->datetimenews < $yearago)
{
$priority = 0.1;
$frequency = Url::CHANGE_FREQUENCY_NEVER;
}
}

$route = route('site.news.show', ['id' => $article->id]);

$updated_at = $article->datetimeedited ? $article->datetimeedited : $article->datetimecreated;

$event->map->add(
Url::create($route)
->setLastModificationDate($updated_at)
->setChangeFrequency($frequency)
->setPriority($priority)
);
}
}
}
2 changes: 2 additions & 0 deletions app/Modules/News/Providers/NewsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Modules\News\Console\EventsTodayCommand;
use App\Modules\News\Listeners\Registrations;
use App\Modules\News\Listeners\RouteCollector;
use App\Modules\News\Listeners\PageCollector;
use Nwidart\Modules\Facades\Module;

class NewsServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -46,6 +47,7 @@ public function boot()
{
$this->app['events']->subscribe(new RouteCollector);
}
$this->app['events']->subscribe(new PageCollector);
}

/**
Expand Down
Loading

0 comments on commit 2b76a00

Please sign in to comment.