Controllers are an important part of an extension if you want to create custom pages, either in backend or frontend. In this tutorial, we will create an admin controller and a frontend controller that will only return a text on the screen.
Magento 2 came with some changes regarding this part. Their structure has changed. Magento 1 developers know that a controller can have multiple actions/methods, but this has changed in Magento 2, and now every action needs a controller file, and a controller will have only one method called execute that is responsible for returning the response.
For a better understanding, I will take the CMS Page CRUD from Magento 1 and Magento 2 as an example:
1. In Magento 1 we have PageController.php that contains indexAction, newAction, editAction, saveAction and deleteAction like in the following screenshot:
2. In Magento 2 we have a controller for all those actions: Index.php, NewAction.php, Edit.php, Save.php and Delete.php
Assuming that you already followed our tutorial about how to create a basic extension in Magento 2, I will jump over that part and continue with creating a controller for admin panel and another one for the frontend in our Testimonials extension.
URLs will have the following format:
siteurl/<frontName>/<actionPath>/<actionClass>
In the same way as in Magento 1, in frontend both <actionPath> and <actionClass> will default to Index if they are not present in the URL, so siteurl/testimonials/ will check for Controller/Index/Index.php
Like for any extension with controllers, we need to define a frontname. We can do this by creating the file etc/adminhtml/routes.xml in our extension with the following content:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="testimonial" frontName="testimonial">
<module name="DEVLINE_Testimonial" />
</route>
</router>
</config>
app/code/DEVLINE/Testimonial/etc/adminhtml/routes.xml The parent route for admin routes needs to be "admin".
At frontName we defined the first segment in the URL right after admin frontName defined in app/etc/env.php
In my case the admin frontName is "admin123" so my URL, based on what I set in routes.xml, will look like: siteurl/admin123/testimonial/...
The next step is to create the controller file. All admin controllers need to be placed in "Adminhtml" folder and have to extend \Magento\Backend\App\Action.
I will create Controller/Adminhtml/Test/Index.php with the content:
<?php
namespace DEVLINE\Testimonial\Controller\Adminhtml\Test;
class Index extends \Magento\Backend\App\Action
{
public function execute()
{
echo 'This will be the grid page of testimonials in the admin panel.';
die();
}
}
app/code/DEVLINE/Testimonial/Controller/Adminhtml/Test/Index.php
Now that we have the necessary files for a controller in admin, all we need to do is to open the terminal and run this command:
php bin/magento setup:upgrade
At this stage we have a controller that we can access by the following URL:
siteurl/<admin-frontName>/<frontName>/<actionPath>/<actionClass>
In my case the URL is:
siteurl/admin123/testimonial/test/index
This will show the message from the execute method:
This is pretty much the same as the admin controller, except the folder structure and that the controller has to extend \Magento\Framework\App\Action\Action
We will need to create a routes.xml, however now in etc/frontend/ of your extension. This time the parent route will be "standard":
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="testimonials" frontName="testimonials">
<module name="DEVLINE_Testimonial" />
</route>
</router>
</config>
app/code/DEVLINE/Testimonial/etc/frontend/routes.xml The frontName has the same role, however, in this case it will be the first segment of the URL, without any admin frontName, so we will be able to access our URL like:
siteurl/<frontName>/...
We will also create the controller file Controller/Test/Index.php:
<?php
namespace DEVLINE\Testimonial\Controller\Test;
class Index extends \Magento\Framework\App\Action\Action
{
public function execute()
{
echo 'This page will contain a list of testimonials in frontend.';
die();
}
}
app/code/DEVLINE/Testimonial/Controller/Test/Index.php
Again, run the following command in terminal:
php bin/magento setup:upgrade
Now we will be able to access our frontend page by the URL:
siteurl/<frontName>/<actionPath>/<actionClass>
In my case the URL is:
siteurl/testimonials/test/index
Congrats! You've learned how to create both admin and frontend controllers in Magento 2.
Comments
No comments