Controller là một phần quan trọng trong module nói riêng và mô hình MVC nói chung. Nhiệm vụ của controller là nhận request, xử lý và render page. Có thể có một hoặc nhiều file trong thư mục controller của module. Có hai loại controller là frontend controller và backend controller. Về bản chất thì 2 loại này giống nhau chỉ khác là ở backend controller sẽ có thêm một method để kiểm tra quyền truy cập. Trong bài viết này chúng ta sẽ cùng nhau tìm hiểu cách để tạo Controller trong Magento 2:
Bước 1: Tạo file routes.xml
Bước đầu tiên trước khi tạo controller, các bạn phải tạo file routes.xml nằm trong module ViMagento_HelloWorld đã tạo ở bài trước
- Đối với frontend controller
Bạn tạo một file ViMagento/HelloWorld/etc/frontend/routes.xml và thêm nội dung bên dưới:
1 2 3 4 5 6 7 8 |
<?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 frontName="helloworld" id="helloworld"> <module name="ViMagento_HelloWorld"/> </route> </router> </config> |
- Đối với backend controller
Bạn tạo file ViMagento/HelloWorld/etc/adminhtml/routes.xml và thêm nội dung bên dưới:
1 2 3 4 5 6 7 8 |
<?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="helloworld" frontName="helloworld"> <module name="ViMagento_HelloWorld"/> </route> </router> </config> |
Bước 2: Tạo Controller
- Tạo Controller frontend
Bạn tạo file ViMagento/HelloWorld/Controller/Post/Index.php và thêm nội dung sau:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php namespace ViMagento\HelloWorld\Controller\Post; class Index extends \Magento\Framework\App\Action\Action { protected $_pageFactory; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $pageFactory) { $this->_pageFactory = $pageFactory; return parent::__construct($context); } public function execute() { return $this->_pageFactory->create(); } } |
- Đối với backend controller
Bạn tạo file ViMagento/HelloWorld/Controller/Adminhtml/Post/Index.php và thêm nội dung sau:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php namespace ViMagento\HelloWorld\Controller\Adminhtml\Post; use Magento\Framework\View\Result\PageFactory; use Magento\Backend\App\Action; class Index extends \Magento\Backend\App\Action { protected $_pageFactory; public function __construct(Action\Context $context, PageFactory $pageFactory) { $this->_pageFactory = $pageFactory; parent::__construct($context); } public function execute() { return $this->_pageFactory->create(); } } |
Như ở trên các bạn có thể thấy frontend controller extends class Magento\Framework\App\Action\Action và nằm trực tiếp ở trong thư mục Controller. Còn backend controller thì extends class Magento\Backend\App\Action và bắt buộc phải nằm trong thư mục Controller/Adminhtml nhé. Ở đây thì cả hai class để sử dụng Magento\Framework\View\Result\PageFactory để render ra trang cơ bản mà magento đã xây dựng sẵn. Và tất cả logic sẽ được thực hiện trong execute() method.
Sau khi thực hiện xong hai bước trên các bạn clear cache để Magento cập nhật những thay đổi của bạn và triển khai chúng.
php bin/magento cache:clean
Bước 3: Kiểm tra xem đã tạo Controller thành công chưa
Đầu tiên các bạn phải xác định URL của controller của bạn. URL trong magento 2 được chia làm ba phần: http://domain.com/frontName/ControllerName/ActionName trong đó:
- frontName: được xác định trong file routes.xml
- ControllerName: là thư mục phía trong thư mục Controller
- ActionName: chính là class chứa method execute() bạn vừa viết.
Trong bài viết này tôi sử dụng:
- https://example.com/helloworld/post/index
- https://example.com/admin/helloworld/post/index
Bây giờ hãy mở trình duyệt của bạn và truy cập theo URL đã xác định ở trên nếu thành công sẽ hiển thị trang web mặc định của Magento như bên dưới:


Kết luận
Vừa rồi tôi đã hướng dẫn các bạn tạo controller trong Magento 2. Trong bài viết tiếp theo chúng ta sẽ tìm hiểu cách thêm một layout cho từng controller bạn muốn nhé.
Các bạn có thể tham khảo thêm trên devdocs của magento 2: https://devdocs.magento.com/guides/v2.4/extension-dev-guide/routing.html
controller admin sai rồi khởi tạo contruc lại ko khai báo lại, vãi thánh
Ok bạn. Mình đã update lại rồi nhé
Video hướng dẫn trong bài không xem đươc nữa
t xài magento docker bitnami có lệnh magento setup:di:compile không bị lỗi
ReflectionException: Class Magento\Framework\App\Http\Interceptor does not exist in /bitnami/magento/vendor/magento/framework/Code/Reader/ClassReader.php:24 nữa
path admin của em không được đẹp như của anh là sao vậy ạ
thêm phần module admin layout block template view của admin luôn đi