How to Create New Pages in Magento 2: Admin and Programmatical Methods

Jan 10, 2019220Pavel Polyanovskiy
How to Create New Pages in Magento 2: Admin and Programmatical Methods

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 this two ways in more detail.

How to create a page in admin panel of Magento 2
How to create a new page programmatically in Magento 2
Admin vs programmical way of creating Magento 2 new pages

How to create a page in 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,

content magento 2


  • Press Add new page button,

Magento 2 add new page

  • 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

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:

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.

Admin vs programmical way of creating Magento 2 new pages

Admin panel method


  • user-friendly interface,
  • don’t need the knowledge of a programming language,
  • don’t need access to initial code,
  • don’t need console commands implementation.


  • when you deploy website copy, you need to migrate the database or create the page anew,
  • need access to admin panel.

Programmical + module method


  • no need to migrate the database when you deploy the website copy,
  • code is saved in version control system,
  • actions automation.


  • need the knowledge of programming language,
  • need access to 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.


  1. Hi, Lars! Sorry to hear that. Could you be more specific about the issue you’re having?

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *