Skip to content

Commit 4464a91

Browse files
authored
Merge pull request #33 from HerveEmagma/master
Fix custom routing
2 parents 6a9b76f + c329933 commit 4464a91

File tree

3 files changed

+128
-46
lines changed

3 files changed

+128
-46
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,22 @@ Apart from that, it's good pratice to also add the following line to your
1919
Obviously, you should replace 'example.org' with the domain name of your website.
2020

2121
This extension adds a 'route' for `/sitemap.xml` and `/sitemap` by default, but it
22-
has lower priority than user defined routes.
23-
If you use the `pagebinding` in `routing.yml` (or anything similar like `/{slug}` ),
22+
has lower priority than user defined routes.
23+
24+
If you want AnimalDesign/bolt-translate extension compatibily or
25+
if you use the `pagebinding` in `routing.yml` (or anything similar like `/{slug}` ),
2426
you need to add the following _above_ that route:
2527

2628
```
27-
sitemapxml:
28-
path: /sitemap.xml
29-
defaults: { _controller: 'Bolt\Extension\Bolt\Sitemap\Extension::sitemapXml' }
30-
31-
3229
sitemap:
3330
path: /sitemap
34-
defaults: { _controller: 'Bolt\Extension\Bolt\Sitemap\Extension::sitemap' }
31+
defaults: { _controller: sitemap.controller:sitemap }
32+
33+
sitemapXml:
34+
path: /sitemap.xml
35+
defaults: { _controller: sitemap.controller:sitemapXml }
3536
```
3637

38+
3739
Note, if you have a ContentType with the property `searchable: false`, that content
3840
type will be ignored.

src/Controller/Sitemap.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Bolt\Extension\Bolt\Sitemap\Controller;
4+
5+
use Silex\Application;
6+
use Silex\ControllerCollection;
7+
use Silex\ControllerProviderInterface;
8+
use Symfony\Component\HttpFoundation\Request;
9+
use Symfony\Component\HttpFoundation\Response;
10+
11+
/**
12+
* The controller for Sitemap routes.
13+
*
14+
*/
15+
16+
class Sitemap implements ControllerProviderInterface
17+
{
18+
/** @var Application */
19+
protected $app;
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function connect(Application $app)
25+
{
26+
$this->app = $app;
27+
28+
/** @var ControllerCollection $ctr */
29+
$ctr = $app['controllers_factory'];
30+
31+
// This matches both GET requests.
32+
$ctr->match('sitemap', [$this, 'sitemap'])
33+
->method('GET');
34+
35+
$ctr->match('sitemap.xml', [$this, 'sitemapXml'])
36+
->method('GET');
37+
38+
return $ctr;
39+
}
40+
41+
/**
42+
* @param Application $app
43+
* @param Request $request
44+
*
45+
* @return Response
46+
*/
47+
public function sitemap(Application $app, Request $request)
48+
{
49+
$config = $app['sitemap.config'];
50+
51+
$body = $app["twig"]->render($config['template'], ['entries' => $app['sitemap.links']]);
52+
53+
return new Response($body, Response::HTTP_OK);
54+
}
55+
56+
/**
57+
* @param Application $app
58+
* @param Request $request
59+
*
60+
* @return Response
61+
*/
62+
public function sitemapXml(Application $app, Request $request)
63+
{
64+
$config = $app['sitemap.config'];
65+
66+
$body = $app["twig"]->render($config['xml_template'], ['entries' => $app['sitemap.links']]);
67+
68+
$response = new Response($body, Response::HTTP_OK);
69+
$response->headers->set('Content-Type', 'application/xml; charset=utf-8');
70+
71+
return $response;
72+
}
73+
}

src/SitemapExtension.php

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Bolt\Extension\SimpleExtension;
99
use Bolt\Legacy\Content;
1010
use Carbon\Carbon;
11+
use Silex\Application;
1112
use Silex\ControllerCollection;
1213
use Symfony\Component\HttpFoundation\Response;
1314
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@@ -21,45 +22,38 @@
2122
class SitemapExtension extends SimpleExtension
2223
{
2324
/**
24-
* Route for regular sitemap.
25-
*
26-
* @return Response
25+
* {@inheritdoc}
2726
*/
28-
public function sitemap($xml = false)
27+
protected function registerServices(Application $app)
2928
{
30-
$config = $this->getConfig();
31-
$body = $this->renderTemplate($config['template'], ['entries' => $this->getLinks()]);
32-
33-
return new Response($body, Response::HTTP_OK);
29+
$app['sitemap.config'] = $app->share(
30+
function () {
31+
return $this->getConfig();
32+
}
33+
);
34+
$app['sitemap.links'] = $app->share(
35+
function ($app) {
36+
return $this->getLinks();
37+
}
38+
);
39+
$app['sitemap.controller'] = $app->share(
40+
function ($app) {
41+
return new Controller\Sitemap();
42+
}
43+
);
3444
}
3545

3646
/**
3747
* Twig function returns sitemap.
3848
*
39-
* @return Response
49+
* @return array
4050
*/
4151
protected function registerTwigFunctions(){
4252
return [
4353
'sitemapEntries' => 'twigGetLinks'
4454
];
4555
}
4656

47-
/**
48-
* Route for XML based sitemap.
49-
*
50-
* @return Response
51-
*/
52-
public function sitemapXml()
53-
{
54-
$config = $this->getConfig();
55-
$body = $this->renderTemplate($config['xml_template'], ['entries' => $this->getLinks()]);
56-
57-
$response = new Response($body, Response::HTTP_OK);
58-
$response->headers->set('Content-Type', 'application/xml; charset=utf-8');
59-
60-
return $response;
61-
}
62-
6357
/**
6458
* {@inheritdoc}
6559
*/
@@ -72,7 +66,7 @@ protected function registerAssets()
7266
->setCallback(function () {
7367
$app = $this->getContainer();
7468
$snippet = sprintf(
75-
'<link rel="sitemap" type="application/xml" title="Sitemap" href="%ssitemap.xml">',
69+
'<link rel="sitemap" type="application/xml" title="Sitemap" href="%s/sitemap.xml">',
7670
$app['url_generator']->generate('homepage', [], UrlGeneratorInterface::ABSOLUTE_URL)
7771
);
7872

@@ -85,17 +79,6 @@ protected function registerAssets()
8579
];
8680
}
8781

88-
/**
89-
* {@inheritdoc}
90-
*
91-
* Set up the routes for the sitemap.
92-
*/
93-
protected function registerFrontendRoutes(ControllerCollection $collection)
94-
{
95-
$collection->match('sitemap', [$this, 'sitemap']);
96-
$collection->match('sitemap.xml', [$this, 'sitemapXml']);
97-
}
98-
9982
/**
10083
* {@inheritdoc}
10184
*/
@@ -113,6 +96,30 @@ public function twigGetLinks(){
11396
return $this->getLinks();
11497
}
11598

99+
100+
/**
101+
* {@inheritdoc}
102+
*/
103+
104+
protected function registerTwigPaths()
105+
{
106+
return [
107+
'templates',
108+
];
109+
}
110+
111+
/**
112+
* {@inheritdoc}
113+
*/
114+
protected function registerFrontendControllers()
115+
{
116+
$app = $this->getContainer();
117+
118+
return [
119+
'/' => $app['sitemap.controller'],
120+
];
121+
}
122+
116123
/**
117124
* Get an array of links.
118125
*
@@ -154,7 +161,7 @@ private function getLinks()
154161
];
155162
}else{
156163
$links[] = [
157-
'link' => $rootPath . $contentType['slug'],
164+
'link' => $rootPath . '/' . $contentType['slug'],
158165
'title' => $contentType['name'],
159166
'depth' => 1,
160167
];

0 commit comments

Comments
 (0)