【发布时间】:2012-11-28 19:33:58
【问题描述】:
我写了 2 个 Magento 观察者,他们都做了我想要的,除了他们在错误的页面上结束。换句话说,他们编写日志文件、修改数据库并与其他服务器通信,但他们修改页面到页面的路由。例如,我有一个在登录时使用的观察者,它修改数据库、写入 cookie 并写入日志,但它会将登录后页面更改为
http://www.my-web-site.com/index.php/customer/login/post/
然后给我一个 404 错误。如果我点击 "Ctrl" + 'r' 然后我登录在
http://www.my-web-site.com/index.php/customer/account/index/
这是正确的。如果我改变, app/code/local/my_module/my_model/etc/config.xml 到 app/code/local/my_module/my_model/etc/config.xml.1 (换句话说,取出观察者),然后 Magento 路由到正确的页面,
我认为我需要 config.xml 中的路由器信息。我当前的 config.xml 是:
<?xml version="1.0" encoding="UTF-8"?>
<!-- The root node for Magento module configuration -->
<config>
<!-- The module's node contains basic information about each Magento module -->
<modules>
<!-- This must exactly match the namespace and module's folder
names, with directory separators replaced by underscores -->
<MyCompany_LogIn>
<!-- The version of our module, starting at 0.0.0 -->
<version>0.0.0</version>
</MyCompany_LogIn>
</modules>
<!-- Configure our module's behavior in the global scope -->
<global>
<!-- Defining models -->
<models>
<!-- Unique identifier in the model's node.
By convention, we put the module's name in lowercase. -->
<mycompany_login>
<!-- The path to our models directory,
with directory separators replaced by underscores -->
<class>MyCompany_LogIn_Model</class>
</mycompany_login>
</models>
</global>
<frontend>
<!-- Defining an event observer -->
<events>
<!-- The code of the event we want to observe -->
<customer_login>
<!-- Defining an observer for this event -->
<observers>
<!-- Unique identifier within the catalog_product_save_after node.
By convention, we write the module's name in lowercase. -->
<mycompany_login>
<!-- The model to be instantiated -->
<class>mycompany_login/observer</class>
<!-- The method of the class to be called -->
<method>wrtLogInCookie</method>
<!-- The type of class to instantiate -->
<type>singleton</type>
</mycompany_login>
</observers>
</customer_login>
</events>
</frontend>
</config>
我猜测 Magento 内部的登录使用了观察者,我正在干扰它。
除了 ,我猜我也可以在 PHP Observer 代码中完成类似的事情。我的观察者是:
<?php
/**
* Our class name should follow the directory structure of
* our Observer.php model, starting from the namespace,
* replacing directory separators with underscores.
* i.e. /www/app/code/local/MyCompany/LogIn/Model/Observer.php
*/
class MyCompany_LogIn_Model_Observer extends Varien_Event_Observer
{
/**
* Magento passes a Varien_Event_Observer object as
* the first parameter of dispatched events.
*/
public function wrtLogInCookie(Varien_Event_Observer $observer)
{
// Retrieve the product being updated from the event observer
$customer = $observer->getEvent()->getCustomer();
$email = $customer->getEmail();
Mage::log('The E-mail is: ' . $email);
$ran_nmbr = rand();
Mage::log('The random number is: ' . $ran_nmbr);
$crnt_dat = date("m-d-Y::H:i:s");
Mage::log('The date is: ' . $crnt_dat);
return $this;
}
}
?>
我读过关于路由器的文章,但是文章讨论了在执行扩展之前登陆某个页面的方式。如您所见,我需要在扩展执行后登陆正确的页面。
在 PHP 观察者中,我也尝试了重定向。例如,
Mage::app()->getResponse()->setRedirect(Mage::getUrl('customer/account/login'));
也许我需要一个完整的 URL 地址或其他东西。我确信这很容易解决,但我的无知似乎在跟着我。如果您对此有所了解,请提供帮助。
【问题讨论】:
-
对于实验,您可以删除包含“extends Varien_Event_Observer”的代码部分吗?
-
感谢您的评论。我删除了“扩展 Varien_Event_Observer”,但没有任何区别。
-
我会留下这个,以防有人遇到类似问题。代码按照我提出的方式运行,只是在 Observer.php 中的结束标记后有空格。错误“无法重新生成会话 ID”是最终导致我遇到问题的原因。