gpt4 book ai didi

events - 如何从外部脚本触发 config.xml 中 中的 的无 Controller 核心 Magento 模块

转载 作者:行者123 更新时间:2023-12-04 05:16:27 25 4
gpt4 key购买 nike

背景引用
见:Magento: How do I get observers to work in an external script?

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

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

<?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 事件的 Controller 将永远触发。

Mage_Log 是这种情况的一个完美示例,它监视 customer_login 并触发 bindCustomerLogin() 函数(因为我使用了上面 Alan Storm 的建议)但 Controller 调度没有触发,导致模块无法正常工作。

这些其他模块怎么可能被外部脚本(或观察 controller_front_init_routers 事件的全局观察者)触发?

编辑:解决方案
这是上面 benmarks 建议的最终结果......我正在模拟 Mage_Customer Controller 。下面的脚本演示了如何执行一个完整的 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();

}

最佳答案

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

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

Mage_Core_Controller_Varien_Action::preDispatch() (link)了解更多信息。请注意,pre-dispatch 和 post-dispatch 方法基于 routename 参数调度动态事件,这可能是也可能不是问题。

关于events - 如何从外部脚本触发 config.xml 中 <frontend> 中的 <controller_action_predispatch> 的无 Controller 核心 Magento 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14201677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com