【问题标题】:Magento 1.8 Custom Module 404Magento 1.8 自定义模块 404
【发布时间】:2013-12-19 06:37:20
【问题描述】:

希望有人可以帮助我测试 Magento 自定义模块,但我无法输出。该模块显示在“配置”>“高级”中,所以我认为 Helloworld_Mystuff.xml 已正确完成,但我可能错了!我禁用了本地 Magento 管理实例上的所有缓存。多次刷新缓存,登录/退出管理员,不同的浏览器。如果重要的话,我在使用 apache 的 Mac 上。

/etc/modules/Helloworld_Mystuff.xml(不包括 xml 行)

<config>
<modules>
    <Helloworld_Mystuff>
        <active>true</active>
        <codePool>local</codePool>
    </Helloworld_Mystuff>
</modules>
</config>

/app/code/local/Helloworld/Mystuff/controllers/IndexController.php

class Helloworld_Mystuff_IndexController extends Mage_Core_Controller_Front_Action{
  public function indexAction(){
    echo "test";
    //$this->loadLayout()->renderLayout();
  }
}

/app/code/local/Helloworld/Mystuff/etc/config.xml

<config> 
    <modules>
        <Helloworld_Mystuff>
            <version>0.0.1</version>
        </Helloworld_Mystuff>
    </modules>
     <frontend>
                <routers>
                    <helloworld>
                        <use>standard</use>
                        <args>
                              <module>Helloworld_Mystuff</module>
                              <frontName>helloworld</frontName>
                        </args>
                    </helloworld>
                </routers>
     </frontend>
</config>

试过了
- /helloworld/index
- /helloworld/index/index

它导致了 404 错误。

我尝试在 /varien/Router/Standard.php 中放置一些调试代码,以查看当我调用测试 URL 时它在寻找哪个类,并且它正在寻找 Mage_Cms_IndexController。因此,在某些地方,我的自定义模块没有加载,因为 Magento 甚至没有尝试加载文件。

编辑: 我在函数内的 Varien/Router/Standard.php 中添加了一些额外的调试代码:

public function match(Zend_Controller_Request_Http $request)
{
//checking before even try to find out that current module
//should use this router
if (!$this->_beforeModuleMatch()) {
    return false;
}

$this->fetchDefault();

$front = $this->getFront();
$path = trim($request->getPathInfo(), '/');

if ($path) {
    $p = explode('/', $path);
} else {
    $p = explode('/', $this->_getDefaultPath());
}
echo "<br />front = " . ($front);
echo "<br />path = " . ($path);
echo "<br />request->getModuleName = ".$request->getModuleName();

输出

front =
path = helloworld/index
request->getModuleName =
front =
path = helloworld/index
request->getModuleName =
front =
path = helloworld/index
request->getModuleName = cms
front =
path = helloworld/index
request->getModuleName = cms 

【问题讨论】:

    标签: magento


    【解决方案1】:

    在以下方法中删除一些调试代码(var_dumps)

    #File: app/code/core/Mage/Core/Controller/Varien/Router/Standard.php
    protected function _validateControllerClassName($realModule, $controller)
    {
        $controllerFileName = $this->getControllerFileName($realModule, $controller);
        var_dump($controllerFileName);
        if (!$this->validateControllerFileName($controllerFileName)) {
            return false;
        }
    
        $controllerClassName = $this->getControllerClassName($realModule, $controller);
        var_dump($controllerClassName);
        if (!$controllerClassName) {
            return false;
        }
    
        // include controller file if needed
        if (!$this->_includeControllerClass($controllerFileName, $controllerClassName)) {
            return false;
        }
    
        return $controllerClassName;
    }
    

    这将告诉您 Magento 在尝试路由您的 URL 时正在寻找哪些类/文件,并且通常足以找出您的配置和/或类名和/或文件名中的拼写错误。

    【讨论】:

    • 嗨艾伦,我实际上遇到了一些你建议的答案,我也尝试过:/app/code/core/Mage/Cms/controllers/IndexController.php 和 Mage_Cms_IndexController
    • @james -- 你确定这些是唯一列出的吗?在前端搜索之前,Magento 至少还会在管理员中搜索控制器。
    • 是的,100%。当我使用调试代码进入管理面板时,我会看到完整的文件列表,但是当我进入另一个前端页面(例如产品)时,我也只看到一个:/app/code/core/Mage/Catalog /controllers/ProductController.php / Mage_Catalog_ProductController
    • @james 啊,看起来最新版本的 Magento 路由更智能了(前端没有管理员检查)。尽管如此,听起来 Magento 的匹配方法看不到您的路由器配置。我会从$modules = $this-&gt;getModuleByFrontName($module); 开始,祝你好运!
    • 感谢您的宝贵时间。没错,Magento 没有看到我的路由器配置。它最终变得非常愚蠢。 configure.xml != config.xml.
    【解决方案2】:

    尝试在 /app/code/local/Helloworld/Mystuff/etc/config.xml 中使用它

    <config> 
      <modules>
        <Helloworld_Mystuff>
          <version>0.0.1</version>
        </Helloworld_Mystuff>
      </modules>
      <frontend>
        <routers>
          <mystuff>
            <use>standard</use>
            <args>
              <module>Helloworld_Mystuff</module>
              <frontName>helloworld</frontName>
            </args>
          </mystuff>
        </routers>
      </frontend>
    </config>
    

    【讨论】:

      【解决方案3】:

      对不起各位,这实在是太愚蠢了,我的配置文件命名不正确。 Alan 的提示帮助我意识到路由器没有被拾取,所以我从头开始。

      我打印出了 Magento 获取的所有路由器名称,发现我的模块不在其中。

      foreach ($routers as $routerName=>$routerConfig) {
      echo $routerName;
      

      【讨论】:

        【解决方案4】:

        我有同样的问题,在我的情况下,这都是关于区分大小写的单词,我想你也是:
        试试这个

        <frontend>
             <routers>
                   <Mystuff>
                        <use>standard</use>
                        <args>
                             <module>Helloworld_Mystuff</module>
                             <frontName>helloworld</frontName>
                        </args>
                   </Mystuff>
             </routers>
        </frontend>
        

        这里是my question

        【讨论】:

          猜你喜欢
          • 2014-02-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-06
          • 1970-01-01
          • 2015-10-03
          • 2012-08-28
          • 2014-05-22
          相关资源
          最近更新 更多