2

I have a sitemap that is generated the same way as regular webpage(index, contacts, etc..). Now I want to just run cron once a day/week and save sitemap.xml to /web directory.

Is it possible to save .twig template output to /web directory of the website?

Thanks

PS: I'm planning to create symfony command to generate sitemap.xml to /web directory. But I'm not sure if it's gonna work.

7
  • Have you seen this so link? Then you could create a xml twig file... Commented Jan 17, 2017 at 5:56
  • Yes I saw it, this will save xml file to MY computer, I want to save this file to /web directory. That is my question. Commented Jan 17, 2017 at 5:59
  • Are you planning to use a cron job to call the template? Commented Jan 17, 2017 at 6:02
  • Yes I'm planning to use cronjob to generate sitemap file. I have been thinking about creating symfony command. But I'm not sure if it's gonna work. Commented Jan 17, 2017 at 6:04
  • I withdrew my answer because @PatrikKarisch 's answer seems like the best solution for you to use. Commented Jan 17, 2017 at 6:56

1 Answer 1

1

Yes. Simply create a command which generates your sitemap.xml. It doesn't matter if you render the sitemap with twig or php xml functions. You simply save it with php. So this may be in your command:

$sitemap = // however you collect the sites

$renderedSitemap = $this->getContainer()->get('templating')
    ->render('sitemap/sitemap.xml.twig', [
        'sitemap' => $sitemap,
    ]);

file_put_contents(
    dirname($this->getContainer()->getParameter('kernel.root_dir')).'/web/sitemap.xml',
    $renderedSitemap
);

That's all. Your command must extend ContainerAwareCommand to have access to the container. And the constructed path expects the standard symfony structure. If you have changed that, you must adapt the path.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'll try. I think you've forgotten to put $renderedSitemap to file_put_contents as $data parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.