2012-09-11 58 views
2

正在關注this post我終於設法通過擴展Magento_Adminhtml_Controller_Action並在它之前執行一些操作來捕獲事件。但是現在我正在嘗試改進它,以便捕獲管理面板上觸發的另一個事件,並從那裏將數組通過request變量傳遞給前端上的另一個事件。我有這些問題/答案,herehere但我沒辦法達到我所需要的。我已經使用die()測試了observer code以確保執行線程進入正確的調用並且沒問題。我正在使用AlanStorm的CommerceBug,以防它可以用來解決這個問題。Magento從adminthtml頁面重定向到前端動作

這是我的Observer.php代碼。

<?php 
class Dts_Videotestimonials_Model_Observer { 

    public function hookToAdminhtmlControllerActionPreDispatch($observer) 
    { 
     if($observer->getEvent()->getControllerAction()->getFullActionName() == 'videotestimonials_adminhtml_videotestimonialsbackend_post') 
     { 
      // dispatching our own event before action upload video is run and sending parameters we need 
      Mage::dispatchEvent("upload_video_before", array('request' => $observer->getControllerAction()->getRequest())); 
     } 
    } 

    public function hookToUploadVideoBefore($observer) 
    { 
     //Hooking to our own event 
     $request = $observer->getEvent()->getRequest()->getParams(); 
     // do something with product 

     $user = Mage::getSingleton('admin/session'); 
     $userName = $user->getUser()->getFirstname(); 
     $userEmail = $user->getUser()->getEmail(); 

     $request['product_id'] = "16"; #$_product->getId(), 
     $request['author_email'] = $userEmail; 
     $request['author_name'] = $userName; 
     $request['video_link'] = "http://www.youtube.com/watch?v=y435u6kfExA&feature=youtube_gdata_player"; 
     $request['video_type'] = "link"; 
     $request['title'] = "AT&T Phone Nokia 2610"; 
     $request['comment'] = "this is a comment"; 
     Mage::dispatchEvent("vidtest_youtube_post", $request); 
    } 
} 

編輯:

以下是完整的config.xml中

<?xml version="1.0"?> 
<config> 
    <modules> 
     <Dts_Videotestimonials> 
      <version>0.1.0</version> 
     </Dts_Videotestimonials> 
    </modules> 
    <global> 
     <models> 
      <videotestimonials> 
      <class>Dts_Videotestimonials_Model</class> 
      <resourceModel>videotestimonials_mysql4</resourceModel> 
      </videotestimonials> 
     </models> 
     <events> 
      <controller_action_predispatch> 
       <observers> 
        <controller_action_before> 
         <type>singleton</type> 
         <class>videotestimonials/observer</class> 
         <method>hookToAdminhtmlControllerActionPreDispatch</method> 
        </controller_action_before> 
       </observers> 
      </controller_action_predispatch> 
      <upload_video_before> 
       <observers> 
        <upload_video_before> 
         <type>singleton</type> 
         <class>videotestimonials/observer</class> 
         <method>hookToUploadVideoBefore</method> 
        </upload_video_before> 
       </observers> 
      </upload_video_before> 
     </events> 
     <helpers> 
      <videotestimonials> 
       <class>Dts_Videotestimonials_Helper</class> 
      </videotestimonials> 
     </helpers> 
     <blocks> 
      <videotestimonials> 
      <class>Dts_Videotestimonials_Block</class> 
      </videotestimonials> 
     </blocks> 
    </global> 
    <admin> 
     <routers> 
      <videotestimonials> 
      <use>admin</use> 
      <args> 
       <module>Dts_Videotestimonials</module> 
       <frontName>videotestimonials</frontName> 
      </args> 
      </videotestimonials> 
     </routers> 
    </admin> 
    <adminhtml> 
     <menu> 
      <videotestimonials module="videotestimonials"> 
      <title>Videotestimonials</title> 
      <sort_order>100</sort_order> 
      <children> 
       <videotestimonialsbackend module="videotestimonials"> 
       <title>VideoTestimonials_Admin</title> 
       <sort_order>0</sort_order> 
       <action>videotestimonials/adminhtml_videotestimonialsbackend</action> 
       </videotestimonialsbackend> 
       <pending_video translate="title"> 
        <title>Videos pendientes</title> 
        <sort_order>20</sort_order> 
        <action>videotestimonials/adminhtml_pendingvideos/pending</action> 
       </pending_video>   
      </children> 
      </videotestimonials> 
     </menu> 
     <acl> 
      <resources> 
      <all> 
       <title>Allow Everything</title> 
      </all> 
      <admin> 
       <children> 
       <videotestimonials translate="title" module="videotestimonials"> 
        <title>Videotestimonials</title> 
        <sort_order>1000</sort_order> 
        <children> 
        <videotestimonialsbackend translate="title"> 
         <title>VideoTestimonials_Admin</title> 
        </videotestimonialsbackend> 
        <pending_video translate="title"> 
         <title>Videos pendientes</title> 
         <sort_order>20</sort_order> 
        </pending_video> 
        </children> 
       </videotestimonials> 
       </children> 
      </admin> 
      </resources> 
     </acl> 
     <layout> 
      <updates> 
      <videotestimonials> 
       <file>videotestimonials.xml</file> 
      </videotestimonials> 
      </updates> 
     </layout> 
    </adminhtml> 
    <crontab> 
     <jobs>    
      <videotestimonials_videotestimonialscron> 
       <schedule><cron_expr>59 0 * */1 0</cron_expr></schedule> 
       <run><model>videotestimonials/cron::VideoTestimonialscron</model></run> 
      </videotestimonials_videotestimonialscron> 
     </jobs> 
    </crontab> 
</config> 
+0

我不認爲這是一個問題,但它可能是: $ observer-> getControllerAction()應該是$ observer-> getEvent() - > getControllerAction() – Andrew

+0

什麼問題,發生錯誤?如果有的話? – Andrew

+0

@Andrew,我正在進行另一個模塊開發,也是爲Magento開發的,當我完成之後,我會回過頭來看看這個問題,然後檢查你的建議,預先感謝 – Yaroslav

回答

2

這裏是發生了什麼。您的管理控制器配置錯誤。包括您在管理部分控制器正確的方法是這樣的(你需要改變了模塊名稱,以符合你的):

<?xml version="1.0" ?> 

<config> 
    <modules> 
     <Video_Awesome> 
      <version>0.0.1</version> 
     </Video_Awesome> 
    </modules> 
    <global> 
     <models> 
      <Video_Awesome> 
       <class>Video_Awesome_Model</class> 
       <!-- No resource model used currently --> 
      </Video_Awesome> 
     </models> 
     <events> 
      <controller_action_predispatch> 
       <observers> 
        <controller_action_before> 
         <type>singleton</type> 
         <class>Video_Awesome/Observer</class> 
         <method>controllerActionPredispatch</method> 
        </controller_action_before> 
       </observers> 
      </controller_action_predispatch> 
      <upload_video_before> 
       <observers> 
        <Video_Awesome> 
         <type>singleton</type> 
         <class>Video_Awesome/Observer</class> 
         <method>uploadVideoBefore</method> 
        </Video_Awesome> 
       </observers> 
      </upload_video_before> 
     </events> 
    </global> 

    <admin> 
     <routers> 
      <adminhtml> 
      <!-- we are not creating our own router, but tapping into the adminhtml router --> 
       <args> 
        <modules> 
         <!-- Your module name, and then 
         the path to the controller (minus 
         the controllers folder name). So, 
         in this instance, I put the router 
         in a "Adminhtml" folder inside of 
         the controllers folder, like thus: 
     Video/Awesome/controllers/Adminhtml/videotestimonialsController.php --> 
         <Video_Awesome before="Mage_Adminhtml">Video_Awesome_Adminhtml</Video_Awesome> 
        </modules> 
       </args> 
      </adminhtml> 
     </routers> 
    </admin> 
</config> 

,然後,在你的路由器(你不需要越來越控制器動作之前調用getEvent):

public function controllerActionPredispatch ($observer) 
{ 
    if($observer->getControllerAction()->getFullActionName() == 'adminhtml_videotestimonials_post') { 
     // dispatching our own event before action upload video is run and sending parameters we need 
    Mage::dispatchEvent("upload_video_before", array('request' => $observer->getControllerAction()->getRequest())); 
    } 
} 

最後,它不聽起來就像如果你有一個調試設置爲你的Magento發展。我會強烈推薦一個。我使用PHPStorm(我沒有任何公司股份 - 這不是一個廣告:),它的工作很棒。在那裏設置一個斷點,看看變量是什麼。

我還建議使用adminhtml_controller_action_predispatch_start而不是全局controller_action_predispatch,因爲它只會在後端觸發(性能差別非常非常小)。

另外,作爲一個小小的註腳,我在您的​​3210中看到,您在那裏指定了菜單項/ acl。你可能不知道,但這是不推薦使用的功能,這些項目應該放在adminhtml.xml

+0

只需詳細解答,您就可以獲得+1。我正處在另一個Magento相關問題的中間,我會盡量儘快測試您的建議。 PS:保持警惕,我會在幾分鐘後發佈一些新問題,也許你可以提供幫助 – Yaroslav