Skip to content

Commit

Permalink
Merge pull request #33 from HerveEmagma/master
Browse files Browse the repository at this point in the history
Fix custom routing
  • Loading branch information
bobdenotter authored Jun 15, 2017
2 parents 6a9b76f + c329933 commit 4464a91
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 46 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@ Apart from that, it's good pratice to also add the following line to your
Obviously, you should replace 'example.org' with the domain name of your website.

This extension adds a 'route' for `/sitemap.xml` and `/sitemap` by default, but it
has lower priority than user defined routes.
If you use the `pagebinding` in `routing.yml` (or anything similar like `/{slug}` ),
has lower priority than user defined routes.

If you want AnimalDesign/bolt-translate extension compatibily or
if you use the `pagebinding` in `routing.yml` (or anything similar like `/{slug}` ),
you need to add the following _above_ that route:

```
sitemapxml:
path: /sitemap.xml
defaults: { _controller: 'Bolt\Extension\Bolt\Sitemap\Extension::sitemapXml' }
sitemap:
path: /sitemap
defaults: { _controller: 'Bolt\Extension\Bolt\Sitemap\Extension::sitemap' }
defaults: { _controller: sitemap.controller:sitemap }
sitemapXml:
path: /sitemap.xml
defaults: { _controller: sitemap.controller:sitemapXml }
```


Note, if you have a ContentType with the property `searchable: false`, that content
type will be ignored.
73 changes: 73 additions & 0 deletions src/Controller/Sitemap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Bolt\Extension\Bolt\Sitemap\Controller;

use Silex\Application;
use Silex\ControllerCollection;
use Silex\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* The controller for Sitemap routes.
*
*/

class Sitemap implements ControllerProviderInterface
{
/** @var Application */
protected $app;

/**
* {@inheritdoc}
*/
public function connect(Application $app)
{
$this->app = $app;

/** @var ControllerCollection $ctr */
$ctr = $app['controllers_factory'];

// This matches both GET requests.
$ctr->match('sitemap', [$this, 'sitemap'])
->method('GET');

$ctr->match('sitemap.xml', [$this, 'sitemapXml'])
->method('GET');

return $ctr;
}

/**
* @param Application $app
* @param Request $request
*
* @return Response
*/
public function sitemap(Application $app, Request $request)
{
$config = $app['sitemap.config'];

$body = $app["twig"]->render($config['template'], ['entries' => $app['sitemap.links']]);

return new Response($body, Response::HTTP_OK);
}

/**
* @param Application $app
* @param Request $request
*
* @return Response
*/
public function sitemapXml(Application $app, Request $request)
{
$config = $app['sitemap.config'];

$body = $app["twig"]->render($config['xml_template'], ['entries' => $app['sitemap.links']]);

$response = new Response($body, Response::HTTP_OK);
$response->headers->set('Content-Type', 'application/xml; charset=utf-8');

return $response;
}
}
83 changes: 45 additions & 38 deletions src/SitemapExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Bolt\Extension\SimpleExtension;
use Bolt\Legacy\Content;
use Carbon\Carbon;
use Silex\Application;
use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Expand All @@ -21,45 +22,38 @@
class SitemapExtension extends SimpleExtension
{
/**
* Route for regular sitemap.
*
* @return Response
* {@inheritdoc}
*/
public function sitemap($xml = false)
protected function registerServices(Application $app)
{
$config = $this->getConfig();
$body = $this->renderTemplate($config['template'], ['entries' => $this->getLinks()]);

return new Response($body, Response::HTTP_OK);
$app['sitemap.config'] = $app->share(
function () {
return $this->getConfig();
}
);
$app['sitemap.links'] = $app->share(
function ($app) {
return $this->getLinks();
}
);
$app['sitemap.controller'] = $app->share(
function ($app) {
return new Controller\Sitemap();
}
);
}

/**
* Twig function returns sitemap.
*
* @return Response
* @return array
*/
protected function registerTwigFunctions(){
return [
'sitemapEntries' => 'twigGetLinks'
];
}

/**
* Route for XML based sitemap.
*
* @return Response
*/
public function sitemapXml()
{
$config = $this->getConfig();
$body = $this->renderTemplate($config['xml_template'], ['entries' => $this->getLinks()]);

$response = new Response($body, Response::HTTP_OK);
$response->headers->set('Content-Type', 'application/xml; charset=utf-8');

return $response;
}

/**
* {@inheritdoc}
*/
Expand All @@ -72,7 +66,7 @@ protected function registerAssets()
->setCallback(function () {
$app = $this->getContainer();
$snippet = sprintf(
'<link rel="sitemap" type="application/xml" title="Sitemap" href="%ssitemap.xml">',
'<link rel="sitemap" type="application/xml" title="Sitemap" href="%s/sitemap.xml">',
$app['url_generator']->generate('homepage', [], UrlGeneratorInterface::ABSOLUTE_URL)
);

Expand All @@ -85,17 +79,6 @@ protected function registerAssets()
];
}

/**
* {@inheritdoc}
*
* Set up the routes for the sitemap.
*/
protected function registerFrontendRoutes(ControllerCollection $collection)
{
$collection->match('sitemap', [$this, 'sitemap']);
$collection->match('sitemap.xml', [$this, 'sitemapXml']);
}

/**
* {@inheritdoc}
*/
Expand All @@ -113,6 +96,30 @@ public function twigGetLinks(){
return $this->getLinks();
}


/**
* {@inheritdoc}
*/

protected function registerTwigPaths()
{
return [
'templates',
];
}

/**
* {@inheritdoc}
*/
protected function registerFrontendControllers()
{
$app = $this->getContainer();

return [
'/' => $app['sitemap.controller'],
];
}

/**
* Get an array of links.
*
Expand Down Expand Up @@ -154,7 +161,7 @@ private function getLinks()
];
}else{
$links[] = [
'link' => $rootPath . $contentType['slug'],
'link' => $rootPath . '/' . $contentType['slug'],
'title' => $contentType['name'],
'depth' => 1,
];
Expand Down

0 comments on commit 4464a91

Please sign in to comment.