Magento 2 | How to create controller in magento 2 | "Full Practical" | Start to End

Channel:
Subscribers:
714
Published on ● Video Link: https://www.youtube.com/watch?v=BzErwqfY24Q



Duration: 15:21
1,808 views
0


Hi everybody,

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.
For example: http://example.com/route_name/controller/action

- 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 (Magento\Framework\App
\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 app\code.
- Create the module name ControllerTutorial in the path app\code\Techone.
- Create the file named registration.php in the path app\code\Techone\ControllerTutorial

- Create the file name module.xml in the path app\code\Techone\ControllerTutorial\etc


End of step #1, I have been completed the step to create new module called

Techone_ControllerTutorial

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

Techone_ControllerTutorial.
- Create the file name routes.xml in the path app\code\Techone\ControllerTutorial\etc\frontend\routes.xml

Step 3: Declare controller file
- Create new file named Index.php in the path app\code\Techone\ControllerTutorial\Controller\Index\Index.php

- As you see, all controllers must be extended \Magento\Framework\App\Action\Action 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 app\code\Techone\ControllerTutorial\view\frontend\layout\controllertutorial_index_index.xml


Step 5: Declare Block file
- Create new file named Index.php in the path app\code\Techone\ControllerTutorial\Block\Index.php

Step 6: Declare template file
- Create new file named index.phtml in the path app\code\Techone\ControllerTutorial\view\frontend\templates\index.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
http://domain.com/controllertutorial/index/index
or

http://domain.com/controllertutorial/

Get Source code : https://github.com/rajupndt06/magento2-controller

how to create controller in magento 2,
magento 2 controller,
magento 2 controller tutorial,
magento 2 tutorial,magento 2 controller development




Other Videos By Raju Pandit


2020-05-02How to Create Passport size photo in Photoshop | Step by Step | Easy Tutorial | Complete Guide
2020-05-02How to use or add custom border in ms word | step by step | Easy Tutorial | Complete Guide
2020-05-01How to create certificate design in ms word | step by step | Easy Tutorial | Complete Guide
2020-04-30How to create custom admin menu in magento 2 step by step | Easy Tutorial | Complete Guide
2020-04-29Magento 2 Create : View | Blocks, Template, Layout | Learn In Simpleway | Full Guide
2020-04-28Error Free | Installation Magento 2.3 | Start to End | Xampp | Localhost
2020-04-26How To Create The Matrix Rain In Command Prompt | step by step | Complete Tutorial
2020-04-26How to upload project or files to Github Repo with Git Bash | step by step | Complete Tutorial
2020-04-25How to import sql in phpmyadmin using command line | XAMPP |step by step | Complete Tutorial
2020-04-25How to export sql in phpmyadmin using command line | XAMPP | step by step | Complete Tutorial
2020-04-24Magento 2 | How to create controller in magento 2 | "Full Practical" | Start to End
2020-04-23Magento 2 | How to use AJAX in magento 2 | "Full Practical" | Start to End
2020-04-21Magento 2 | How to use plugin in magento 2 | "Full Practical" | Start to End
2020-04-20"READ More!" - Warren Buffett | How reading book helps in technical life | Reading books Benefits
2020-04-19PHP 7 | PHP String | Explained Simpleway | FULL PRACTICAL !!
2020-04-17PHP 7 | PHP Data types | Explained Simpleway | FULL PRACTICAL !!
2020-04-16PHP 7 | PHP Comment | PHP Variables | PHP echo & print | FULL PRACTICAL !! | Explained in Simpleway
2020-04-16PHP 7 | PHP Syntax | Everyone Should know !!| New release PHP
2020-04-15What's new in PHP 7 | What Can PHP 7 Do | Everyone Should know !!| New release PHP
2020-04-14Create Simple Login Page with PHP and MySql | Full Source Code | Complete Practical | Tutorial
2020-04-10How to add function in module step by step | Complete Tutorial



Tags:
how to create controller in magento 2
magento 2 controller
magento 2 controller tutorial
magento 2 tutorial
magento 2 controller development
techone solution