Creating a Block in Mage5

Creating a block in Mage5 is very eazy, please note that blocks should be developed in outer directory. However, this tutorial focuses only on creating a new block. By creating a block, webmasters can easily add and remove it from anywhere on a site as well as make changes to it.

Making the Folder Structure

Start by creating a new folder within your project root’s blocks folder. The name should match the name of the block, so if you wish to implement a contact form, then an appropriate block name would be "contact". This is the example we will be using in this tutorial.

Secondly, we have to add a controller class for the block. If you are familiar with the MVC pattern, then the purpose of a controller is nothing new to you. Put very simply, a controller is controlling the flow of an application; it takes requests, processes them and forwards the results to the view, which is often referred to as the presentation layer. Within the newly created directory, create a file named controller.phpand put in the following content for now. Remember to include the PHP start tag. An explanation will follow.