PHP code example of jeroendesloovere / sitemap-bundle
1. Go to this page and download the library: Download jeroendesloovere/sitemap-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jeroendesloovere / sitemap-bundle example snippets
namespace App\SitemapProviders;
use JeroenDesloovere\SitemapBundle\Item\ChangeFrequency;
use JeroenDesloovere\SitemapBundle\Provider\SitemapProvider;
use JeroenDesloovere\SitemapBundle\Provider\SitemapProviderInterface;
class NewsArticleSitemapProvider extends SitemapProvider implements SitemapProviderInterface
{
/** @var NewsArticleRepository */
private $articleRepository;
public function __construct(NewsArticleRepository $articleRepository)
{
$this->articleRepository = $articleRepository;
// `NewsArticle::class` would even be better then just `NewsArticle`
// because you can then use it with doctrine events.
parent::__construct('NewsArticle');
}
public function createItems(): void
{
/** @var Article[] $articles */
$articles = $this->articleRepository->findAll();
foreach ($articles as $article) {
$this->createItem('/nl/xxx/url-to-article', $article->getEditedOn(), ChangeFrequency::monthly());
}
}
}
namespace App\Sitemap;
use App\Sitemap\DependencyInjection\Compiler\SitemapProviderPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class SitemapBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new SitemapProviderPass());
}
}
namespace App\Sitemap\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
class SitemapCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (!$container->has('sitemap.generator')) {
return;
}
$container->setParameter('router.request_context.host', 'www.mywebsite.com');
$container->setParameter('router.request_context.scheme', 'http');
$container->setParameter('router.request_context.base_url', '');
$container->setParameter('asset.request_context.base_path', $container->getParameter('router.request_context.base_url'));
$container->setParameter('asset.request_context.secure', true);
}
}
namespace JeroenDesloovere\SitemapBundle\EventListener;
use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Events;
use JeroenDesloovere\SitemapBundle\Generator\SitemapGenerator;
use JeroenDesloovere\SitemapBundle\Provider\SitemapProviders;
class SitemapSubscriber implements EventSubscriber
{
/** @var SitemapGenerator */
private $sitemapGenerator;
/** @var SitemapProviders */
private $sitemapProviders;
public function __construct(SitemapProviders $sitemapProviders, SitemapGenerator $sitemapGenerator)
{
$this->sitemapProviders = $sitemapProviders;
$this->sitemapGenerator = $sitemapGenerator;
}
public function getSubscribedEvents(): array
{
return [
Events::postPersist,
Events::postUpdate,
Events::postRemove,
];
}
/**
* @param LifecycleEventArgs $eventArgs
* @throws \Exception
*/
public function regenerate(LifecycleEventArgs $eventArgs): void
{
$entityClass = get_class($eventArgs->getObject());
if ($this->sitemapProviders->exists($entityClass)) {
$this->sitemapGenerator->regenerateForSitemapProvider($this->sitemapProviders->get($entityClass));
}
}
public function postPersist(LifecycleEventArgs $eventArgs): void
{
$this->regenerate($eventArgs);
}
public function postRemove(LifecycleEventArgs $eventArgs): void
{
$this->regenerate($eventArgs);
}
public function postUpdate(LifecycleEventArgs $eventArgs): void
{
$this->regenerate($eventArgs);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.