Skip to content

Commit

Permalink
Added auto generated sitemap to website (#1216)
Browse files Browse the repository at this point in the history
Added auto generated sitemap to website page
  • Loading branch information
norberttech authored Sep 7, 2024
1 parent 19e8c26 commit 69a6039
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 13 deletions.
1 change: 1 addition & 0 deletions web/landing/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ build/
vendor/
var/
public/assets
public/*.xml
7 changes: 4 additions & 3 deletions web/landing/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"twig/markdown-extra": "^3.11",
"twig/extra-bundle": "^3.8",
"league/commonmark": "^2.4",
"cocur/slugify": "^4.5"
"cocur/slugify": "^4.5",
"presta/sitemap-bundle": "^4.1"
},
"require-dev": {
"symfony/web-profiler-bundle": "^6.4",
Expand Down Expand Up @@ -58,8 +59,7 @@
],
"post-update-cmd": [
"@importmap:install",
"@build:tailwind",
"@tools:update"
"@build:tailwind"
],
"tools:install": [
"composer install --working-dir=./tools/phpunit"
Expand Down Expand Up @@ -88,6 +88,7 @@
"APP_ENV=prod bin/console importmap:install",
"APP_ENV=prod bin/console tailwind:build --minify",
"APP_ENV=prod bin/console asset-map:compile",
"APP_ENV=prod bin/console presta:sitemaps:dump",
"APP_ENV=prod bin/console static-content-generator:generate:routes --clean",
"APP_ENV=prod bin/console static-content-generator:copy:assets -d public"
]
Expand Down
70 changes: 69 additions & 1 deletion web/landing/composer.lock

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

1 change: 1 addition & 0 deletions web/landing/config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
Symfony\UX\StimulusBundle\StimulusBundle::class => ['all' => true],
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
\Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
\Presta\SitemapBundle\PrestaSitemapBundle::class => ['all' => true],
];
3 changes: 3 additions & 0 deletions web/landing/config/packages/presta_sitemap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
presta_sitemap:
defaults:
priority: 1
14 changes: 13 additions & 1 deletion web/landing/config/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,16 @@ controllers:
resource:
path: ../src/Flow/Website/Controller/
namespace: Flow\Website\Controller
type: attribute
type: attribute

PrestaSitemapBundle_index:
path: "/%presta_sitemap.sitemap_file_prefix%.{_format}"
defaults: { _controller: Presta\SitemapBundle\Controller\SitemapController::indexAction }
requirements:
_format: xml

PrestaSitemapBundle_section:
path: "/%presta_sitemap.sitemap_file_prefix%.{name}.{_format}"
defaults: { _controller: Presta\SitemapBundle\Controller\SitemapController::sectionAction }
requirements:
_format: xml
13 changes: 13 additions & 0 deletions web/landing/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# robots.txt for https://flow-php.com

# Allow all web crawlers to index the site
User-agent: *
Disallow:

# Allow assets required for rendering the page, such as JavaScript and CSS
Allow: /assets/controllers/
Allow: /assets/styles/
Allow: /assets/images/

# Sitemap location
Sitemap: https://flow-php.com/sitemap.xml
2 changes: 1 addition & 1 deletion web/landing/src/Flow/Website/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function post(string $date, string $slug) : Response
]);
}

#[Route('/blog', name: 'blog', priority: 100)]
#[Route('/blog', name: 'blog', options: ['sitemap' => true])]
public function posts() : Response
{
return $this->render('blog/posts.html.twig', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(
) {
}

#[Route('/documentation/dsl', name: 'documentation', priority: 100)]
#[Route('/documentation/dsl', name: 'documentation', options: ['sitemap' => true])]
public function dsl() : Response
{
$modules = $this->dslDefinitions->modules();
Expand All @@ -32,7 +32,7 @@ public function dsl() : Response
]);
}

#[Route('/documentation/dsl/{module}/{function}', name: 'documentation_dsl_function', priority: 100)]
#[Route('/documentation/dsl/{module}/{function}', name: 'documentation_dsl_function')]
public function dslFunction(string $module, string $function) : Response
{
$modules = $this->dslDefinitions->modules();
Expand All @@ -57,7 +57,7 @@ public function dslFunction(string $module, string $function) : Response
]);
}

#[Route('/documentation/dsl/{module}', name: 'documentation_dsl', priority: 100)]
#[Route('/documentation/dsl/{module}', name: 'documentation_dsl')]
public function dslModule(string $module = 'core') : Response
{
$modules = $this->dslDefinitions->modules();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(
) {
}

#[Route('/{topic}/{example}/', name: 'example')]
#[Route('/{topic}/{example}/', name: 'example', priority: -2)]
public function example(string $topic, string $example) : Response
{
$topics = $this->examples->topics();
Expand All @@ -36,7 +36,7 @@ public function example(string $topic, string $example) : Response
]);
}

#[Route('/{topic}/', name: 'topic', priority: 10)]
#[Route('/{topic}/', name: 'topic', priority: -1)]
public function topic(string $topic) : Response
{
$topics = $this->examples->topics();
Expand Down
2 changes: 1 addition & 1 deletion web/landing/src/Flow/Website/Controller/FlowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function __construct()
{
}

#[Route('/changelog', name: 'changelog', priority: 100)]
#[Route('/changelog', name: 'changelog', options: ['sitemap' => true])]
public function main() : Response
{
return $this->render('main/changelog.html.twig', [
Expand Down
2 changes: 1 addition & 1 deletion web/landing/src/Flow/Website/Controller/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(
) {
}

#[Route('/', name: 'home')]
#[Route('/', name: 'home', options: ['sitemap' => true])]
public function home() : Response
{
return $this->render('main/index.html.twig', [
Expand Down
124 changes: 124 additions & 0 deletions web/landing/src/Flow/Website/EventListener/SitemapSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

declare(strict_types=1);

namespace Flow\Website\EventListener;

use Flow\Website\Blog\Posts;
use Flow\Website\Service\Documentation\DSLDefinitions;
use Flow\Website\Service\Examples;
use Presta\SitemapBundle\Event\SitemapPopulateEvent;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final class SitemapSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly Examples $examples,
private readonly Posts $posts,
private readonly DSLDefinitions $dslDefinitions,
) {
}

public static function getSubscribedEvents()
{
return [
SitemapPopulateEvent::class => 'populate',
];
}

public function populate(SitemapPopulateEvent $event) : void
{
$this->populateExamples($event);
$this->populateBlogPosts($event);
$this->populateDocumentation($event);
}

private function populateBlogPosts(SitemapPopulateEvent $event) : void
{
$posts = $this->posts->all();

foreach ($posts as $post) {
$event->getUrlContainer()->addUrl(
new UrlConcrete(
$event->getUrlGenerator()->generate(
'blog_post',
['date' => $post->date->format('Y-m-d'), 'slug' => $post->slug],
UrlGeneratorInterface::ABSOLUTE_URL
),
changefreq: 'weekly'
),
'blog'
);
}
}

private function populateDocumentation(SitemapPopulateEvent $event) : void
{
$modules = $this->dslDefinitions->modules();

foreach ($modules as $module) {
$event->getUrlContainer()->addUrl(
new UrlConcrete(
$event->getUrlGenerator()->generate(
'documentation_dsl',
['module' => \mb_strtolower($module->name)],
UrlGeneratorInterface::ABSOLUTE_URL
),
changefreq: 'weekly'
),
'documentation'
);

foreach ($this->dslDefinitions->fromModule($module)->all() as $definition) {
$event->getUrlContainer()->addUrl(
new UrlConcrete(
$event->getUrlGenerator()->generate(
'documentation_dsl_function',
['module' => \mb_strtolower($module->name), 'function' => $definition->slug()],
UrlGeneratorInterface::ABSOLUTE_URL
),
changefreq: 'weekly'
),
'documentation'
);
}
}
}

private function populateExamples(SitemapPopulateEvent $event) : void
{
$topics = $this->examples->topics();

foreach ($topics as $topic) {
$event->getUrlContainer()->addUrl(
new UrlConcrete(
$event->getUrlGenerator()->generate(
'topic',
['topic' => $topic],
UrlGeneratorInterface::ABSOLUTE_URL
),
changefreq: 'weekly'
),
'examples'
);

$examples = $this->examples->examples($topic);

foreach ($examples as $example) {
$event->getUrlContainer()->addUrl(
new UrlConcrete(
$event->getUrlGenerator()->generate(
'example',
['topic' => $topic, 'example' => $example],
UrlGeneratorInterface::ABSOLUTE_URL
),
changefreq: 'weekly'
),
'examples'
);
}
}
}
}

0 comments on commit 69a6039

Please sign in to comment.