首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Magento核心覆盖在第三个请求之前不注册?

为什么Magento核心覆盖在第三个请求之前不注册?
EN

Stack Overflow用户
提问于 2015-06-12 23:19:42
回答 1查看 185关注 0票数 0

我正在重写模块中的Mage_Core_Model_Encryption以重写哈希、getHash和validateHash方法。

由于管理端的urls也使用散列,因此我覆盖了Mage_Adminhtml_Model_Url getSecretKey方法,以便它在urls中使用(更快的) md5散列。

我注意到的问题是,当我清除我的缓存(只启用了config)并加载admin/index时,我的加密器没有注册。直到第三次请求才能看到它。

我在Mycompany_Encryption_Adminhtml_Url中有调试语句,这些语句显示它立即与模块一起加载。清除缓存后,我可以看到我的调试语句正在工作。

当我使用以下语句时:

var_dump('Class name: '.get_class(Mage::helper('core')->getEncryptor() ) );

我看到返回到admin/index (刷新页面时)的类名是Mage_Core_Model_Encryption,然后在第三次刷新时,它显示了我的类Mycompany_Encryption_Model_Encryption

为什么这需要三个请求?我已经到处找了,还没找到问题所在。从表面上看,我已经正确地进行了配置(并且已经尝试了一系列替代方案)。

有人能帮我解决这个问题吗?

(下面是配置、Url类和加密类片段)。非常感谢你的帮助。

更新,我正在使用modman,这也是我的modman:

代码语言:javascript
复制
# Modman file allows Modman to generate links in Magento.
code                        app/code/local/Mycompany/Encryption/
Mycompany_Encryption.xml   app/etc/modules/Mycompany_Encryption.xml

下面是我的配置:

代码语言:javascript
复制
<config>
  <modules>
    <Mycompany_Encryption>
      <version>0.1</version>
      <depends>
        <Mage_Core />
      </depends>
    </Mycompany_Encryption>
  </modules>
  <phpunit>
    <suite>
      <modules>
        <Mycompany_Encryption />
      </modules>
    </suite>
  </phpunit>
  <global>
    <helpers>
      <core>
        <encryption_model>
          <class>Mycompany_Encryption_Model_Encryption</class>
        </encryption_model>
      </core>
    </helpers>
    <models>
      <mycompany_encryption>
        <class>Mycompany_Encryption_Model</class>
      </mycompany_encryption>
      <core>
        <rewrite>
          <encryption>Mycompany_Encryption_Model_Encryption</encryption>
        </rewrite>
      </core>
      <adminhtml>
        <rewrite>
          <url>Mycompany_Encryption_Model_Adminhtml_Url</url>
        </rewrite>
      </adminhtml>
    </models>
  </global>
</config>

Url类:

代码语言:javascript
复制
class Mycompany_Encryption_Model_Adminhtml_Url extends Mage_Adminhtml_Model_Url
{
    /**
     * Generate secret key for controller and action based on form key
     *
     * @param string $controller Controller name
     * @param string $action Action name
     * @return string
     */
    public function getSecretKey($controller = null, $action = null)
    {
        $salt = Mage::getSingleton('core/session')->getFormKey();

        $p = explode('/', trim($this->getRequest()->getOriginalPathInfo(), '/'));

        if (!$controller) {
            var_dump('Not Controller');
            $controller = !empty($p[1]) ? $p[1] : $this->getRequest()->getControllerName();
        }

        if (!$action) {
            var_dump('Not Action');
            $action = !empty($p[2]) ? $p[2] : $this->getRequest()->getActionName();
        }
        $secret = $controller . $action . $salt;

        // Here are my debug statements showing when class/method become available.
        var_dump('Method exists: '.method_exists(Mage::helper('core')->getEncryptor(), 'urlHash')); 
        var_dump('Class name: '.get_class(Mage::helper('core')->getEncryptor() ) );

        /* This is what I want to return - but sends error 500 until instantiated. */
        //return Mage::helper('core')->getEncryptor()->urlHash($secret);
        return false;
    }
}

加密类:

代码语言:javascript
复制
class Mycompany_Encryption_Model_Encryption extends Mage_Core_Model_Encryption
{

    public function hash($data)
    {
        return password_hash($data, PASSWORD_BCRYPT);
    }

    public function validateHash($password, $hash)
    {

        return password_verify($password, $hash);
    }

    public function getHash($value)
    {
        return $this->hash($value);
    }

    public function urlHash($value)
    {
        return md5($value);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2015-06-13 01:57:43

使用n98-magerun工具,我能够配置:转储并搜索global/helpers/core节点。

使用上面的配置,我得到了这个格式错误的配置转储:

代码语言:javascript
复制
 $ n98-magerun config:dump |cat |grep -A1 -B2 encryption_model
<helpers>
  <core>
    <encryption_model>Mage_Core_Model_Encryption<class>Mycompany_Encryption_Model_Encryption</class></encryption_model>
  </core>

如果我删除我的config.xml中的<class>标记,它看起来是清晰的:

代码语言:javascript
复制
<helpers>
  ...
  <core>
    <!-- Class tags were surrounding my encryption_model value before -->       
    <encryption_model>Mycompany_Encryption_Model_Encryption</encryption_model>
  </core>
  ...
</helpers>

通过此更改,我清除了缓存并获得了更好的配置转储:

代码语言:javascript
复制
$ n98-magerun cache:clean config
config cache cleaned

$ n98-magerun config:dump |cat |grep -A1 -B2 encryption_model
<helpers>
  <core>
    <encryption_model>Mycompany_Encryption_Model_Encryption</encryption_model>
  </core>

如果您知道发生这种情况的原因,请使用文档链接进行评论或提供信息!谢谢!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30806669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档