As you begin working on a new website on Magento 2, you will sooner or later need to create some pages. The common way to do it is to interact with the system through the backend office built into Magento 2. However, this is not the only method – we can also do it programmatically using a special module. Now let’s examine these two ways in more detail.
How to create a page in the admin panel of Magento 2
How to create a new page programmatically in Magento 2
Admin vs programmatical way of creating Magento 2 new pages
How to create a page in the admin panel of Magento 2
First I will briefly analyze the classic approach – creation of a page from the admin panel. This is done the following way:
- Log into the admin panel,
- Go to Content -> Pages,
- Press Add new page button,
- fill in the Page Title field, as well as all other required fields,
- select in which store view this page should be available,
- click Save Page button.
How to create a new page programmatically in Magento 2
We now turn to creating a new page with software; in particular, we need to create a module which will automatically create a new page when installed.
I won’t explain the process of creating main module files, because I’ve already described it in my previous article. Instead, I will proceed directly to the realization of my idea.
- Create a Setup directory in the module.
- Create a file Setup / UpgradeData.php
- Add the main framework class to the file Setup / UpgradeData.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 |
<?php namespace BelVG\PageCreator\Setup; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\UpgradeDataInterface; class UpgradeData implements UpgradeDataInterface { /** * @var \Magento\Cms\Model\PageFactory */ private $pageFactory; /** * @var \Magento\Store\Model\StoreFactory */ private $storeFactory; public function __construct( \Magento\Cms\Model\PageFactory $pageFactory, \Magento\Store\Model\StoreFactory $storeFactory ) { $this->pageFactory = $pageFactory; $this->storeFactory = $storeFactory; } public function upgrade( ModuleDataSetupInterface $setup, ModuleContextInterface $context ) { $installer = $setup; $installer->startSetup(); if (version_compare($context->getVersion(), '1.0.2', '<=')) { $this->createContactPage(); } $installer->endSetup(); } private function createContactPage() { // Here’s page creation code } } |
Let me comment on the code. We used the UpgradeData class, which is applied to update the data in the database when the module is updated, and the page refers specifically to the data. Since data update will be called each time the module is updated, we check the version with the function version_compare. Also, with the help of dependency injection, we received instances of the $pageFactory and $storeFactory classes, since we will need them later. We have left room for the code that will create the new page.
Now let’s proceed to the implementation of the method for creating the page. Replace the createContactPage method with the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
private function createContactPage() { $storeId = $this->storeFactory->create()->load('EN')->getId(); $page = $this->pageFactory->create(); $page->setTitle('Contacts') ->setIdentifier('contacts') ->setPageLayout('1column') ->setIsActive(true) ->setStores([$storeId]) ->setContent('<p>Contact page content</p>') ->save(); } |
In this method, we get the store view identifier from its code (EN), create a page class, assign fields to it and save. The page will be created when you will make the console command. If we want to create a page for all store views, we do not need to call the setStores method – instead, we can also list several identifiers.
The setIdentifier method sets the URL key which makes the page available for the specified store views. Using the setPageLayout method, a Custom layout is set, which should be specified in layouts.xml.
When you call setContent method, we set the page content, which can be changed through the visual editor in the administration panel. With the help of special tags you can add {block} here. Also, blocks and widgets can be added to the page using XML in the same way as to a page created manually from the administration panel.
php bin / magento setup: upgrade
Let us examine the pros and cons of the considered data of page creation approaches in Magento 2.
Magento Custom Development
Take your online store to the next level with BelVG Magento Custom Development
Visit the pageAdmin vs programmatical way of creating Magento 2 new pages
Admin panel method
Advantages:
- user-friendly interface,
- don’t need the knowledge of a programming language,
- don’t need access to the initial code,
- don’t need console commands implementation.
Disadvantages:
- when you deploy website copy, you need to migrate the database or create the page anew,
- need access to admin panel.
Programmatical + module method
Advantages:
- no need to migrate the database when you deploy the website copy,
- the code is saved in the version control system,
- actions automation.
Disadvantages:
- need the knowledge of programming language,
- need access to the initial code,
- need to create a new module or configure the existing one,
- page version conflicts can arise.
Based on this comparative analysis, we can conclude that the use of one or another approach depends on the number of developers working on the project, the number of deployed copies of the site and other aspects of work. Therefore, it’s up to you to make this decision; my task was to show you the alternatives, and I hope I managed to do it.
If you have any questions or comments left, you’re welcome to drop them down below.
Magento Webdesign
Take your online store to the next level with BelVG Magento Webdesign
Visit the page
Hello, Syed!
Thank you for the comment, I really appreciated it.
I also agree that for a person with programmatical knowledge the second method is more suitable, yet it’s useful to the admin method as well.
Hello,
Thanks for this detailed post for creating pages in Magento 2. Yes, both methods have advantages and disadvantages.
Well, I think if someone has programmatical knowledge, then Method 2 will be a good choice.
Hi, Lars! Sorry to hear that. Could you be more specific about the issue you’re having?
For Magento 2.3 this solution will not work.