Magento的从adminthtml页面重定向到一个前端动作(Magento redirect f

2019-09-22 07:50发布

下面这篇文章 ,我终于设法通过扩展捕获事件Magento_Adminhtml_Controller_Action和之前进行一些操作。 但现在我想改善它,所以我可以捕获另一事件触发的管理面板上,并从那里通过传递一个数组request变量对前端另一个事件。 我有这些做题/答案, 在这里和这里 ,但没有办法,我可以实现我需要什么。 我测试过的observer code使用die()以确保执行线程进入正确的呼叫,它是确定。 我使用的是从AlanStorm的CommerceBug,在情况下,它可以用来获取有关此问题的一些情况。

这是我的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> 

Answer 1:

这是发生了什么。 您的管理员控制器的配置是错误的。 包括您在管理部分控制器正确的方法是这样的(你需要改变了模块名称,以符合你的):

<?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 ,因为它只会在后台触发(非常非常小的性能差异)。

此外,作为一个小的阿里纳斯,我在你看到config.xml ,你被指定的菜单项/ ACL那里。 你可能不知道,但已过时的功能,并且这些项目应该把它adminhtml.xml



文章来源: Magento redirect from adminthtml page to a frontend action