我应该从 ajax 调用 url [Magento] 中查找文件

Where should i look for file from ajax call url [Magento]

在 ajax 调用中,我看到这个 url 是通过控制台调用的。

http://example.net/index.php/checkout/cart/add/uenc/random-key/product/527/form_key/random-key/

我的问题是我应该在哪里寻找这个?我在我的模板 app/design/frontend/default/mytheme/template/app/code/core/mage/.

中找到了 checkout/cart

这应该是app/code/core/Mage/Checkout/controllers/CartController.phpaddAction方法。

http://example.net/index.php/checkout/cart/add/uenc/random-key/product/527/form_key/random-key/

您可以在 Mage/Checkout/controllers/CartController 中找到它。php 在给定的文件中你会发现 addAction()

所以你的 url 就像 checkout - 模块名称 cart - 控制器名称 add - 函数名,其他为参数。

虽然你在这里得到了两个非常好的答案是为了对 Magento 的广泛了解 URL

注意:http://www.example.com/[index.php]/ 是您的 Magento url。根据您的 Magento 配置,您可能会或可能不会在 URL 中找到 index.php。 {text} 是可以根据上下文变化的文本。 [text] 是可以显示或不显示的文本,具体取决于上下文。

http://www.example.com/[index.php]/{module_frontname}/{[controller]}/{[action]}/{[parameter_name_1]}/{[value_of_parameter_name_1]}/{[parameter_name_2]}/{[value_of_parameter_name_2]}/ (...) /{[parameter_name_n]}/{[value_of_parameter_name_n]}

所以,除了我们的基础 Magento URL,这里是部件及其含义,以及在哪里可以找到它们。

第 1 部分:module_frontname:一个模块的“前面的名称”。该值可以在 app/code/{codePool}/{NamespaceOfModule}/{ModuleName}/etc/config.xml 中找到。 在那些文件中,您会发现一个或多个 <frontName> 节点,定义模块前面的名称。

在你的例子中,这是一个核心模块,'Mage_Checkout'所以它在codePool核心中。 (如果您想了解更多关于 codePool 的信息,我可以写更多关于它的内容,但让我们坚持在这里进行示例)。 如果您查看文件 app/code/core/Mage/Checkout/etc/config.xml,您确实会发现那些代码行

<frontend>
    <routers>
        <checkout>
            <use>standard</use>
            <args>
                <module>Mage_Checkout</module>
                <frontName>checkout</frontName>
            </args>
        </checkout>
    </routers>
</frontend>

它定义了使用 http://www.example.com/[index.php]/checkout/ 对这个 Magento 完成的所有请求将由模块 Mage_Checkout 及其控制器处理。

第 2 部分:控制器。在 Magento 上很容易找到控制器。它们位于app/code/{codePool}/{NamespaceOfModule}/{ModuleName}/controllers/的文件夹和子文件夹中,文件名和文件中的class对应URL的控制器部分。因此,如果您找到此 url http://www.example.com/[index.php]/module_frontname/controller/,控制器文件将被命名为 ControllerController.php,class 名称将被命名为 NamespaceOfModule_ModuleName_ControllerController。因此 url 的首字母大写,并以 Controller 为后缀。 http://www.example.com/[index.php]/module_frontname/something/ -> SomethingController.php / NamespaceOfModule_ModuleName_SomethingController http://www.example.com/[index.php]/module_frontname/else/ -> ElseController.php / NamespaceOfModule_ModuleName_ElseController.

请注意:默认情况下有一个控制器。这个控制器就是控制器IndexController。因此,如果您发现 url 类似于 http://www.example.com/[index.php]/module_frontname/,这意味着它隐式调用了控制器 IndexController,这就是为什么在我的第一个 url 示例中将此部分设置为可选的原因。

在你的情况下:你确实会在 app/code/core/Mage/Checkout/controller/CartController.php 中找到一个文件 class Mage_Core_CartController.

第 3 部分:动作。这个也很容易被发现。在给定的控制器 class 中,动作是一个以 "Action" 为后缀的函数。 这部分对大小写很敏感,所以请注意 aCtionAction 将处理 url http://www.example.com/[index.php]/module_frontname/controller/aCtion/(请注意 aAction 的大写 C。 所以这个URLhttp://www.example.com/[index.php]/module_frontname/controller/action/会调用控制器controllerController中名为actionAction的函数。 同样的方式:http://www.example.com/[index.php]/module_frontname/controller/some/ -> 将由函数 someAction.

处理

请注意:相同的默认行为适用于动作而不是控制器。然后 indexAction() 中的默认操作。所以实际上,当您调用 http://www.example.com/[index.php]/module_frontname/ 时,您正在访问控制器 IndexController 以及它的 indexAction。如果您调用 url http://www.example.com/[index.php]/module_frontname/controller,您将到达控制器 controllerController 的 indexAction。 另请注意:前 3 个部分的顺序和含义永远不会改变。 因此,如果您想调用默认控制器 IndexController 的特定操作,则必须在 url 中指定控制器,例如 http://www.example.com/[index.php]/module_frontname/index/some 因为如果您尝试以这种方式到达它 http://www.example.com/[index.php]/module_frontname/some您将获得控制器 SomeController 的动作 indexAction 而不是控制器 IndexController 的动作 someAction !

在你的例子中:如果你打开文件app/code/core/Mage/Checkout/controller/CartController.php,你确实会在该行周围找到一个函数addAction 191.

public function addAction()
{
  // (... lots of code come here which I'm not going to reproduce)
}

第 4 部分到 n:这些基本上是在 Zend Framework sauce 中获取参数。 http://www.example.com/[index.php]/module_frontname/controller/action/param1/value1 严格等于 http://www.example.com/[index.php]/module_frontname/controller/action?param1=value1。同样 http://www.example.com/[index.php]/module_frontname/controller/action/param1/value1/param2/value2 等于 http://www.example.com/[index.php]/module_frontname/controller/action?param1=value1&param2=value2。这将始终成对 parameter_name / parameter_value

请注意:第二条注释中关于默认操作/控制器的解释也适用于此。所以如果你想在控制器 IndexController 中的动作 indexAction 中有一个参数,你必须将 url 写成完整的 http://www.example.com/[index.php]/module_frontname/index/index/param1/value1 因为这个 url http://www.example.com/[index.php]/module_frontname/param1/value1 将匹配控制器 Param1Controller 和动作 value1Action 而不是控制器 IndexController 和带有请求参数的动作 indexAction !

你的情况 : uenc/random-key/product/527/form_key/random-key/ 意味着你有那个

$_GET['uenc'] == 'random-key'
$_GET['product'] == '527'
$_GET['form_key'] == 'random-key'

确实在上述文件中,您会在函数 cartAction() 中找到这些代码行:

$params = $this->getRequest()->getParams();
try {
    if (isset($params['qty'])) {
        // (... lots of code come here which I'm not going to reproduce)
    } 
} catch(Mage_Core_Exception $e) {
    // (... lots of code come here which I'm not going to reproduce)
}
// (... lots of code come here which I'm not going to reproduce)

在您的情况下(我保证这是最后一个):您的假设几乎是准确的,试图找到一个模板。但遗憾的是,这不是视觉操作,因此它不会显示任何内容,只是处理将产品添加到购物车的请求,然后重定向到另一个 url,就像第 251 行附近的 cartAction 状态的最后几行

$url = $this->_getSession()->getRedirectUrl(true);
if ($url) {
    $this->getResponse()->setRedirect($url);
} else {
    $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
}

希望这个完整的 Magento URL 解释对某人有所帮助 :)

http://example.net/index.php/checkout/cart/add/uenc/random-key/product/527/form_key/random-key/

结帐:模块名称(查找具有此名称的模块)

cart:购物车控制器(在目标模块中查找 Controllers 文件夹 > CartController.php 文件)

add : 添加操作方法(并在上面的控制器 class 中查找 addAction() 方法)