如何从外部脚本</frontend> </controller_action_predispatch>触发在config.xml中的<frontend>中观察<controller_action_predispatch>的无控制器核心Magento模块>

时间:2013-01-07 18:18:03

标签: events magento controller external observers

供背景参考 见:Magento: How do I get observers to work in an external script?

我想问一下从外部脚本“复制”前端控制器操作的首选方法是什么。我正在为Magento EE 1.12创建一个外部SSO登录。

我的代码在php文件中如下所示。您可以通过创建test.php并用我的用户ID替换我的用户(185)来测试它。导航到该页面,然后再次。您会注意到您已登录和注销,但在管理员中它不会显示您在线。请继续阅读...

<?php

    umask(0);
    require_once 'app/Mage.php';

    Mage::app("default");

    // Set the session to frontend according to many suggestions
    Mage::getSingleton("core/session", array("name" => "frontend"));

    // Alan Storm suggestion to load specific area in Magento (frontend)
    Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);

    // Load My Specific User
    $user = Mage::getModel('customer/customer')->load(185)->setWebsiteId(Mage::app()->getStore()->getWebsiteId());

    // Get the session object
    $session = Mage::getSingleton('customer/session');

    // Make it look good for debugging
    echo "<PRE>";

    // Toggle the customer logged in / logged out upon page load
    if ($session->isLoggedIn())
    {
        try
        {
            $session->session->setCustomer($user);
            echo "LOGGED IN<br>";
            var_dump($session->getCustomer()->getIsJustConfirmed());
        } catch (Mage_Core_Exception $e) {
            $message = $e->getMessage();
            var_dump($message);
        }

    } else {
        $session->logout();
    }

    var_dump($session->getCustomer());
    echo $session->isLoggedIn() ? $user->getName().' is online!' : 'not logged in';

?>

此代码以用户身份登录,但Mage_Log,Mage_Persistent或任何其他模块没有依赖于附加到config.xml中前端区域的controller_action_predispatch和controller_action_postdispatch事件的控制器都不会永远火了。

Mage_Log是这种情况的一个很好的例子,它监视customer_login并触发bindCustomerLogin()函数(因为我正在使用Alan Storm的建议)但是控制器调度没有触发,导致模块无法正常工作

如何从外部脚本(或观看controller_front_init_routers事件的全局观察者)触发这些其他模块?

编辑:解决方案 以下是上述benmarks建议的最终结果......我正在模仿Mage_Customer控制器。下面的脚本演示了如何执行COMPLETE magento登录。它没有经过广泛测试,但它确实显示用户登录后端。这是迄今为止我见过的最完整的解决方案。

public function autoMageLogin() {
                // Include the controller itself so the object can be used
                include ('/var/www/app/code/core/Mage/Customer/controllers/AccountController.php');

                // Configure Magento to think its using the frontend
                Mage::getSingleton("core/session", array("name" => "frontend"));
                Mage::getConfig()->init();
                Mage::getConfig()->loadEventObservers('frontend');
                Mage::app()->addEventArea('frontend');
                Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);

                // Prepare the request as if it were coming from the specific
                // controller (I've chosed Mage_Customer as my chosen controller
                // to 'emulate' in php code
                $request = Mage::app()->getRequest();
                $request->setRouteName('customer');
                $request->setControllerModule('Mage_Customer');
                $request->setRoutingInfo('');
                $request->setModuleName('customer');
                $request->setControllerName('account');
                $request->setModuleKey('module');
                $request->setControllerKey('account');
                $request->setActionName('loginPost');
                $request->setActionKey('action');


                $response = Mage::app()->getResponse();

                // Instantiate a new AccountController object using the modified request
                // and the modified response (see the include() above)
                $accountControl = new Mage_Customer_AccountController($request, $response);

                // Dispatch events associated to the controller
                Mage::dispatchEvent('controller_action_predispatch', array('controller_action' => $accountControl));
                Mage::dispatchEvent('controller_action_predispatch_customer', array('controller_action' => $accountControl));
                Mage::dispatchEvent('controller_action_predispatch_customer_account_loginPost', array('controller_action' => $accountControl));


                // Load the current user
                $user = Mage::getModel('customer/customer')->load(185)->setWebsiteId(Mage::app()->getStore()->getWebsiteId());

                // Grab the current session
                $session = Mage::getSingleton('customer/session');

                // From this point forward, emulate the controller actions    
                if (!$session->isLoggedIn()){
                        try{

                                $session->setCustomerAsLoggedIn($user);
                                $session->renewSession();
                                echo "LOGGED IN<br>";
                        } catch (Mage_Core_Exception $e) {
                                $message = $e->getMessage();
                                var_dump($message);
                        }

                } else {
                        echo "LOGGED OUT<br>";

                        $session->logout();
                }


                    // Now fire the post controller action events
                    Mage::dispatchEvent('controller_action_postdispatch_customer_account_loginPost', array('controller_action'=>$accountControl));
                    Mage::dispatchEvent('controller_action_postdispatch_customer', array('controller_action'=>$accountControl));
                    Mage::dispatchEvent('controller_action_postdispatch', array('controller_action'=>$accountControl));


                // log to the screen and be done
                var_dump($session->getCustomer());
                echo $session->isLoggedIn() ? $session->getCustomer()->getName().' is online!' : 'not logged in';

                die();

        }

2 个答案:

答案 0 :(得分:3)

您需要使用原始参数手动调度事件,例如

Mage::dispatchEvent(
    'controller_action_predispatch',
    array('controller_action',Mage::app()->getRequest()
);

有关详细信息,请参阅Mage_Core_Controller_Varien_Action::preDispatch() (link)。请注意,调度前和调度后方法根据routename参数调度动态事件,这可能是也可能不是一个问题。

答案 1 :(得分:0)

如果您将外部脚本重写为自定义控制器和操作,则所有事件都将自然触发。但是,您必须有理由将其置于外部。