Learn how to use controllers and display "hello world" in the browser.
- [Instructor] To add a new page in Magento 2, you need to create a new controller. In Magento 2, a controller is a file located at a specific place, which responds to a specific route. A route in Magento 2 are a standard URL that consists of three parts: front-name, controller-name, and action-name. We will be discussing them further in our route.xml. Now, first, we will go to Magento's code module. Here you can see Magento's code module.
Inside this, we'll have etc folder, and inside etc folder, you can see adminhtml, frontend, webapi_rest, and webapi_soap. These are the areas for Magento. Now, if you want to display something in the website's front end, we have to create a front end folder. Inside frontend folder, you'll find a routes.xml. In route.xml, you will have a route ID, which will be defining the unique identifier for this particular route, the frontName will be the name that can be identified into browser's URL.
For us, it will be hplussport.com, slash, our front name, slash, action name. Module name will our module's name. If you want to create something into Magento's back end, we have to go adminhtml, and create the same route.xml. The same thing we have to do for the rest or soap APIs. So let's create the front end folder inside our module. In etc folder, we'll be creating the front end folder.
Inside our front end folder, we'll create routes.xml. In this route.xml, you can just copy the content from your core module. Make sure you change your module's name here. Your route ID is the unique identifier.
And the front name will be the part of URI scheme that will be displayed in your browser. We'll use sports for this. Now to make sure that this router comes in action, we'll create the controller. Let's create the controller folder in our module. Make sure you keep C as capital in your controller folder. Inside controller folder, we'll be creating one more folder named as Page.
Inside this page folder, our controller file will exist. We'll create a controller named as View.php. First of all, we'll define the namespace here. This will be Hplussport\Chapter2\Controller\Page.
After defining this, we'll create this as a class. Our class name is view. It should extend Magento's Framework App\Action\Action class. Now inside this we'll define a protected property named as $resultJsonFactory.
And then we'll define a constructor. Inside this constructor, we'll use Magento\Framework\Controller\Result\JsonFactory.
Then we'll assign this to our property. Then define a public function named as execute. This will be the default function executed by Magento controller. Inside this function, we'll define a variable named as result, and we'll pass the instance of $this, resultJsonFactory, create.
We'll define on array named as data, and we'll pass message, "Hello World." We'll return $result, and we'll set data onto this. This should be good to go. Go to your browser: hplussport.com.
Then your front name, which is sports, then your page, and then your view. So in your browser you see, "Hello World," which is the expected output. This sports word is your front name, this page is your folder's name, this view is your controller's name. Let's go back to our route.xml file. In route.xml, we have defined front name as sports, so that's the second URI segment. In our controller, we defined page as the folder.
So that's our page we have in our URL, and then our controller's name. If you define a different folder, you can just change that, or you can define index folder and index controller just to run the default file.
- Setting up your development environment
- Installing and upgrading the schema
- Creating modules
- Magento ORM
- Creating models and collections
- Customizing Magento
- Defining plugins