简体   繁体   中英

Symfony2 Save .twig output to web directory

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM