Magento 2 | How to create controller in magento 2 | "Full Practical" | Start to End
I am raju, Welcome back to my Magento 2 tutorial video series.
Today, I am going to show everybody a best practice, How to Create Controller in Magento 2
In Magento 2 Controller has one or more files in Controller folder of module, it includes actions of
class which contain execute() method. There are 2 different controllers, they are frontend controller
and backend controller. They are generally similar of workflow, but admin controller is a little
different. There is a checking permission method in admin controller, it calls form key.
How controller work?
It receive an request from end-user.
– route_name : It is a unique name which is set in routes.xml.
– controller : It is the folder inside Controller folder.
– action : It is a class with execute method to process request.
One of the important in Magento system is frontController (MagentoFrameworkApp
FrontController), it alway receives request then route controller, action by route_name Let’s take
an example of routing an request:
If there is an action of controller class found, execute() method will be run.
To understand more about regarding how to Create Controller in Magento 2 we will do a practice.
Lets do this practically, you need to follow step by step
Step 1: Create a new module called Techone_ControllerTutorial
– Create the namespace Techone in the path appcode.
– Create the module name ControllerTutorial in the path appcodeTechone.
– Create the file named registration.php in the path appcodeTechoneControllerTutorial
– Create the file name module.xml in the path appcodeTechoneControllerTutorialetc
End of step #1, I have been completed the step to create new module called
Step 2: Declar routes.xml file.
– To create a controller, we need to create a folder inside Controller folder of module and declare an
action class inside it. For example, we create a index controller and a index action for module
– Create the file name routes.xml in the path appcodeTechoneControllerTutorialetcfrontendroutes.xml
Step 3: Declare controller file
– Create new file named Index.php in the path appcodeTechoneControllerTutorialControllerIndexIndex.php
– As you see, all controllers must be extended MagentoFrameworkAppActionAction class which
has dispatch method which will call execute method in action class. In this execute() method, we
will write all of our controller logic and will return response for the request.
Step 4 : Declare Layout file
– Create new file named controllertutorial_index_index.xml in the path appcodeTechoneControllerTutorialviewfrontendlayoutcontrollertutorial_index_index.xml
Step 5: Declare Block file
– Create new file named Index.php in the path appcodeTechoneControllerTutorialBlockIndex.php
Step 6: Declare template file
– Create new file named index.phtml in the path appcodeTechoneControllerTutorialviewfrontendtemplatesindex.phtml
Step 7: Test and see the results
Run the command lines following:
– php bin/magento setup:upgrade
– php bin/magento setup:static-content:deploy -f
– php bin/magento cache:flush
Let’s open browser and navigate to
Get Source code :
how to create controller in magento 2,
magento 2 controller,
magento 2 controller tutorial,
magento 2 tutorial,magento 2 controller development