We can create CMS pages in Magento 2 using setup scripts. So, to do this, follow the next steps:
Create Setup/UpgradeData.php
Create Setup/UpgradeData.php file inside your module (how to create a module you can read here) with the next code:
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | <?php // app/code/[VendorName]/[ModuleName]/Setup/UpgradeData.php namespace MageDirect\ModuleExample\Setup; use Magento\Framework\Setup\UpgradeDataInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Cms\Model\PageFactory; class UpgradeData implements UpgradeDataInterface { /** * @var PageFactory */ protected $pageFactory; /** * @param PageFactory $pageFactory */ public function __construct(PageFactory $pageFactory) { $this->pageFactory = $pageFactory; } /** * @param ModuleDataSetupInterface $setup * @param ModuleContextInterface $context */ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); if (version_compare($context->getVersion(), '0.1.1') < 0) { $page = $this->pageFactory->create(); $page->setTitle('New CMS page') ->setIdentifier('new-cms-page') ->setIsActive(1) ->setPageLayout('2columns-right') ->setStores([0]) ->setContent('Lorem ipsum dolor sit amet, consectetur adipi' . 'scing elit, sed do eiusmod tempor incididunt ut l' . 'abore et dolore magna aliqua.') ->setContentHeading('New cms page') ->setMetaKeywords('Page keywords') ->setMetaDescription('Page description') ->save(); } $setup->endSetup(); } } |
Change version
Change version of your module to 0.1.1:
PHP
1 2 3 4 5 6 7 8 | <!-- app/code/[VendorName]/[ModuleName]/etc/module.xml --> <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="MageDirect_ModuleExample" setup_version="0.1.1"/> </config> |
Run the database upgrade script
Run the database upgrade script from the root directory of your Magento 2 store:
PHP
1 | php bin/magento setup:upgrade |
Navigate to Content > Pages
Navigate to Content > Pages in the admin panel and check the result:
Lilya GogolevaMagento Developer
Rate us
Please wait...
Related articles
- Follow this tutorial to clear Magento cache with no efforts. Also, you will...
- This easy and effortless tutorial on how to create the new admin account in...
- If you don’t know how to create a custom cache type in Magento, this article...
- SSL is very important for every online store as it initiates a secure session...
- The sitemap is highly important in case you want to optimize your website for...
- Want to manage email templates in your Magento 2 store? Check our article and...
- Do you know how to create custom product type? We know! And would be glad to...
- Want to have a Magento 2 Multistore? Read how our team setups...