Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Generate.php
Go to the documentation of this file.
1 <?php
8 
12 
14 {
16  private $appEmulation;
17 
23  public function __construct(
24  Action\Context $context,
25  Emulation $appEmulation = null
26  ) {
27  parent::__construct($context);
28  $this->appEmulation = $appEmulation ?: ObjectManager::getInstance()
29  ->get(\Magento\Store\Model\App\Emulation::class);
30  }
31 
37  public function execute()
38  {
39  // init and load sitemap model
40  $id = $this->getRequest()->getParam('sitemap_id');
41  $sitemap = $this->_objectManager->create(\Magento\Sitemap\Model\Sitemap::class);
42  /* @var $sitemap \Magento\Sitemap\Model\Sitemap */
43  $sitemap->load($id);
44  // if sitemap record exists
45  if ($sitemap->getId()) {
46  try {
47  //We need to emulate to get the correct frontend URL for the product images
48  $this->appEmulation->startEnvironmentEmulation(
49  $sitemap->getStoreId(),
51  true
52  );
53  $sitemap->generateXml();
54 
55  $this->messageManager->addSuccessMessage(
56  __('The sitemap "%1" has been generated.', $sitemap->getSitemapFilename())
57  );
58  } catch (\Magento\Framework\Exception\LocalizedException $e) {
59  $this->messageManager->addErrorMessage($e->getMessage());
60  } catch (\Exception $e) {
61  $this->messageManager->addExceptionMessage($e, __('We can\'t generate the sitemap right now.'));
62  } finally {
63  $this->appEmulation->stopEnvironmentEmulation();
64  }
65  } else {
66  $this->messageManager->addErrorMessage(__('We can\'t find a sitemap to generate.'));
67  }
68 
69  // go to grid
70  $this->_redirect('adminhtml/*/');
71  }
72 }
$id
Definition: fieldset.phtml:14
__construct(Action\Context $context, Emulation $appEmulation=null)
Definition: Generate.php:23
__()
Definition: __.php:13