This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

Create a Custom Frontend View in Magento 2

Updated on  17th October

4 Min Read
Reading Time: 4 minutes

Project structure is one of the most important differences between Magento 1 and Magento 2. To create a custom frontend view in Magento, the developer needs to understand module development, controllers, layouts, blocks, and templates. The process of creating a custom frontend view in Magento 2 is quite similar to the Magento 1.x. The major difference is the very different file structure in Magento 2.

Custom Frontend Magento 2

In this blog, I will create a custom frontend view in Magento 2 and test it on a storefront. For the purpose of this blog, I assume that you have successfully installed Magento 2, either locally or on a web server.

Once Magento 2 is up and running, follow this step-by-step procedure:

  1. Create and Register a New Magento 2 Module
  2. Setup Routing
  3. Controller Action
  4. Layout File
  5. The Block file
  6. Create the Template file
  7. Activate the Module

Create and Register a New Magento 2 Module

Create a new module with Namespace Cloudways and Module name Newmodule. Go to app/code/Cloudways/Newmodule and create a new registration.php file. Add the following code snippet to the file.

Next, the module needs to be declared.

Cloudways Magento Hosting For Developers

For this, create a module.xml file in the app/code/Cloudways/Newmodule/etc and add the following code in it.

Setup Routing

The module’s frontend routing information will be reported in routes.xml file. This file should be located in  app/code/Cloudways/Newmodule/etc/frontend and have the following code.

Controller Action

Create the file Index.php file in app/code/Cloudways/Newmodule/Controller/Index. This will be the controller action and the execute() function in index.php will be invoked when the action is called.

Layout File

Layout file in Magento 2 is different from Magento 1’s layout file. Magento 2 uses the layout file inside the module folder, and it is named after the structure of module. A common naming style is  RouterName_ControllerName_ActionName.xml. So in this case, create the file newmodule_index_index.xml in app/code/Cloudways/Newmodule/view/frontend/layout directory. Add the following code to it

The Block file

The next important step is the creation of the block file for the module. Block file includes several logical function(s) which will be used in the template file. Create a block file in app/code/Cloudways/Newmodule/Block and name it Newmodule.php. Add the following code to it

Create the Template file

The template file will be named newmodule.phtml and will be located in app/code/Cloudways/Newmodule/view/frontend/templates. Once done, add the following code.

Activate the Module

Finally, the module needs to be activated. Open up the SSH Terminal and in the root folder of Magento 2 (i.e. public_html), run the following commands.

Congratulations! The Magento 2 custom frontend view has been successfully created. To test it out, run the URL in the following format.

http://yourdomain.com/RouterName/ControllerName/ActionName

In my case, the URL is http://magento-14846-54048-160729.cloudwaysapps.com/newmodule/index/index, and the result is following.

Create a Custom Frontend View Magento 2

Conclusion

The above procedure is the shortest way of creating a custom frontend view in Magento 2. As you could see, Magento 2 offers a much easy way of creating a custom view. If you have a question or would like to contribute to the discussion, leave a comment below.

Share your opinion in the comment section. COMMENT NOW

Fayyaz Khattak

Fayyaz is a Magento Community Manager at Cloudways - A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. Fayyaz is a food lover and enjoys driving. You can email him at m.fayyaz@cloudways.com

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.