Get Free Audit

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

Jan 10, 2019

2720 Andrey Litvin

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 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,

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

Igor Dragun
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Igor


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.

magento custom development

Magento Custom Development

Take your online store to the next level with BelVG Magento Custom Development

Visit the page

Admin 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

Magento Webdesign

Take your online store to the next level with BelVG Magento Webdesign

Visit the page
Igor Dragun
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Igor

4 Comments

  1. 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.

  2. 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.

  3. 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 *