Le répertoire Controller dans Magento 2
What is the "Controller" Directory in Magento 2?
Dans Magento 2, the "Controller" répertoire plays an important role in handling requests and rendering responses for the application. Vous pouvez think of it as a bridge between the requests utilisateurs make and the behind-the-scenes logic that processes those requests. It helps determine how the application responds to different types of HTTP requests.
Dans cet article, nous'll break down the structure and fonction of the "Controller" répertoire, with exemples to show how it works in practice.
What Does the Controller Directory Contain?
The Controller répertoire is located in Magento 2 modules at app/code/{Vendor}/{Module}/Controller. It holds PHP classes that act as contrôleurs. These contrôleurs are responsible for processing incoming HTTP requests and deciding how the application should respond.
Generally, contrôleurs handle two types of requests:
- Frontend Requests – These are requests coming from the public-facing side of your Magento store, where clients blignese products.
- Adminhtml Requests – These are requests from the back-office, where store administrators manage products, commandes, clients, etc.
Frontend vs. Adminhtml Controllers
Magento 2 divides contrôleurs into two main types basé sur where they are used:
- Frontend Controllers: These contrôleurs handle requests lié à the client-facing pages. Vous pouvez find them in
app/code/{Vendor}/{Module}/Controller/Index/.
namespace Vendor\Module\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; class Example extends Action { public function __construct(Context $context) { parent::__construct($context); } public function execute() { // Logic here echo 'This is a front-end controller!'; } } - Adminhtml Controllers: These handle admin-side requests and peut être found in
app/code/{Vendor}/{Module}/Controller/Adminhtml/.
namespace Vendor\Module\Controller\Adminhtml\Index; use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; class Example extends Action { public function __construct(Context $context) { parent::__construct($context); } public function execute() { // Admin-side logic echo 'This is an admin controller!'; } }
Both frontend and adminhtml contrôleurs extend different base classes:
- Frontend contrôleurs extend
\Magento\Framework\App\Action\Action, while - Adminhtml contrôleurs extend
\Magento\Backend\App\Action.
How Do Controllers Work?
Each contrôleur class has an execute() méthode that defines the logic to be performed when a request hits that contrôleur. The flow typically works like this:
- A utilisateur requests a URL.
- Magento routes the request to the appropriate module and contrôleur.
- The contrôleur's
execute()méthode is triggered. - The response is generated and displayed to the utilisateur.
Par exemple, if a utilisateur visits the URL http://yourstore.com/yourmodule/index/example, Magento will:
- Use the routeur configuration (defined in the module’s
routes.xmlfichier) to locate theIndex/Examplecontrôleur. - Call the
execute()méthode in that contrôleur. - Return the result (a rendered page, a redirect, JSON data, etc.).
Adding Logic to Your Controller
Vous pouvez add any custom logic inside the execute() méthode, tel que fetching data from the database, modifying session variables, or rendering custom views.
Here’s an exemple of a contrôleur that redirects to a custom URL:
public function execute() {
return $this->_redirect('custom/url');
}
Or, here’s one that returns a JSON response:
use Magento\Framework\Controller\Result\JsonFactory;
class Example extends \Magento\Framework\App\Action\Action
{
protected $resultJsonFactory;
public function __construct(Context $context, JsonFactory $resultJsonFactory) {
$this->resultJsonFactory = $resultJsonFactory;
parent::__construct($context);
}
public function execute() {
$data = ['message' => 'Hello, World!'];
$result = $this->resultJsonFactory->create();
return $result->setData($data);
}
}
Controller URL Structure
Magento 2 uses a convention-based URL structure to route requests to the appropriate contrôleur. The general URL structure is:
{base_url}/{frontname}/{controller}/{action}/{params}
Par exemple, http://yourstore.com/yourmodule/index/example maps to the Index/Example.php contrôleur in the Vendor_Module module.
Conclusion
Controllers are a clé part of developing modules in Magento 2. By managing how HTTP requests are handled, they give développeurs a flexible and powerful way to create custom fonctionality on both the frontend and back-office of Magento stores. Understanding the Controller répertoire is crucial for creating robust and scalable Magento 2 extensions.