Điều này đơn giản vì lớp cơ sở Mage_Core_Controller_Varien_Action cung cấp các sự kiện gửi trước/sau.
Nếu bạn mở các lớp Mage_Core_Controller_Varien_Action bạn tìm thấy hai phương pháp: preDispatch() và postDispatch()
Những phương pháp thực hiện một vài nhiệm vụ và quan trọng nhất là bắn ra ba sự kiện.
controller_action_(pre|post)dispatch
controller_action_(pre|post)dispatch_{{routeName}}
controller_action_(pre|post)dispatch_{{fullActionName}}
FullActionName là tên tuyến đường, tên bộ điều khiển và tên hành động được phân tách bằng dấu '_' và tất cả chữ thường. (Xem Mage_Core_Controller_Varien_Action::getFullActionName để tham khảo)
/app/code/local/FilFact/Test/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<FilFact_Test>
<version>1.0.0</version>
<FilFact_Test>
</modules>
<global>
<models>
<FilFact_Test>
<class>FilFact_Test_Model</class>
</FilFact_Test>
</models>
</global>
<frontend>
<routers>
<filfact>
<use>standard</use>
<args>
<module>FilFact_Test</module>
<frontName>filfact</frontName>
</args>
</filfact>
</routers>
<events>
<controller_action_predispatch_filfact_index_index>
<observers>
<FilFact_Test>
<class>FilFact_Test/Observer</class>
<method>indexPreDispatch</method>
</FilFact_Test>
</observers>
</controller_action_predispatch_filfact_index_index>
<controller_action_postdispatch_filfact_index_index>
<observers>
<FilFact_Test>
<class>FilFact_Test/Observer</class>
<method>indexPostDispatch</method>
</FilFact_Test>
</observers>
</controller_action_postdispatch_filfact_index_index>
</events>
</frontend>
</config>
/app/code/local/FilFact/Test/Model/Observer.php
<?php
class FilFact_Test_Model_Observer
{
public function indexPreDispatch(Varien_Event_Observer $observer)
{
// TODO: Your code
}
public function indexPostDispatch(Varien_Event_Observer $observer)
{
// TODO: Your code
}
}
Nguồn
2011-09-22 06:42:33
Bạn muốn cung cấp hai sự kiện để các mô-đun khác có thể nối vào chúng, đúng không? – Simon
Có, Và tôi sử dụng một cách như: Thêm câu lệnh: 'Mage :: dispatchEvent ('test_index_index_before_action', $ data);' bên trong hành động chỉ mục. Nhưng nó không tốt. Câu trả lời dưới đây là tốt hơn. – vietean