-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ability to generate a sitemap for SEO
- Loading branch information
Showing
14 changed files
with
1,420 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.