Magento覆盖控制器 [英] Magento override controller

查看:152
本文介绍了Magento覆盖控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做上述。
Ive overridden many files in the past ... block,model,helper .... but this one eludes me。



任何人都可以看到错误在这里:
(ive编辑此代码...以包括一些推荐现在...)



剩下我的文件夹结构a test):

  /Idigital/Idgeneral/etc/config.xml 
/ Idigital / Idgeneral / controllers / Checkout /CartController.php
/Idigital/Idgeneral/controllers/CartController.php

.xml:

 <?xml version =1.0?> 
< config>
< modules>
< idigital_idgeneral>
< version> 0.1.0< / version>
< / idigital_idgeneral>
< / modules>
< global>
< blocks>
< idgeneral>< class> Idigital_Idgeneral_Block< / class>< / idgeneral>
< / blocks>
< / global>

< frontend>
< routers>
< checkout>
< use> standard< / use>
< args>
< modules>
< Idigital_Idgeneral before =Mage_Checkout> Idigital_Idgeneral_Checkout< / Idigital_Idgeneral>
< / modules>
< / args>
< / checkout>
< / routers>
< layout>
< updates>
< idgeneral>
< file> idigital.xml< / file>
< / idgeneral>
< / updates>
< / layout>
< / frontend>
< / config>

我把控制器文件放在2个位置进行测试。
和我的FIRST控制器文件的顶部

  require_once'Mage / Checkout / controllers / CartController.php' ; 
class Idigital_Idgeneral_Checkout_CartController extends Mage_Checkout_CartController
{


public function testAction()
{
var_dump('inside checkout / cart / test' ;出口;
}

/ **
*将产品添加到购物车action
* /
public function addAction()
{
blah ...
}

回答我的第二个控制器:

  require_once'Mage / Checkout / controllers / CartController.php'; 
class Idigital_Idgeneral_CartController extends Mage_Checkout_CartController
{


public function testAction()
{
var_dump('inside cart / test'); exit ;
}

/ **
*将产品添加到购物车action
* /
public function addAction()
{
blah ...
}

当我访问:/ checkout / cart / add
Im指向法师控制器...不是我的本地。



当我访问/ checkout / cart /测试 - 我得到一个404 $ b $(我有var_dump stmts在每个...我可以看到哪个是) b当我访问/购物车/添加或购物车/测试 - 我得到一个404
当我访问idgeneral / cart /测试或idgeneral /购物车/添加 - 我得到一个404

 

code> app / code / local / MyNameSpace / MyModule / etc / config.xml
app / code / local / MyNameSpace / MyModule / controllers / Checkout / CartController.php
app / etc / modules /MyNameSpace_All.xml


  • 编辑/etc/config.xml并建立应用程式/ /MyNameSpace/MyModule/etc/config.xml包含以下内容:

     <?xml version =1.0? ; 
    < config>
    < modules>
    < MyNameSpace_MyModule>
    < version> 0.1.0< / version>
    < / MyNameSpace_MyModule>
    < / modules>
    < global>
    <! - 可以将此重写规则添加到数据库 - >
    < rewrite>
    <! - 这是您的重写的标识符,应该是唯一的 - >
    <! - 这是您自己的控制器中的类别名称>
    < mynamespace_mymodule_checkout_cart>
    < from><![CDATA [#^ / checkout / cart /#]]>< / from>
    <! -
    - mymodule匹配路由器名称下面
    - checkout_cart匹配控制器的路径

    考虑到下面的路由器,/ mymodule / checkout_cart /将
    translated更改为/MyNameSpace/MyModule/controllers/Checkout/CartController.php(?)
    - >
    < to> / mymodule / checkout_cart /< / to>
    < / mynamespace_mymodule_checkout_cart>
    < / rewrite>
    < / global>
    <! -
    如果你想重载一个管理控制器,这个标签应该是< admin>而是
    或< adminhtml>如果你超载这样的东西(?)
    - >
    < frontend>
    < routers>
    <! - 在重载管理员内容时应设置为admin(?) - >
    < use> standard< / use>
    < args>
    < module> MyNameSpace_MyModule< / module>
    <! - 当捕获上面的重写 - >
    < frontName> mymodule< / frontName>
    < / args>
    < / mynamespace_mymodule>
    < / routers>
    < / frontend>



    注意:当我覆盖目录/产品控制器时,为我工作。我必须使用:

     < from><![CDATA [#^ catalog / product /#]]& < / from> 
    < to> mymodule / mycontroller< / to>

    (注意缺少的前导斜杠)



    自从Magento 1.3,你可以简单地添加你的模块到前端路由器。重写不再是必要的:

     <?xml version =1.0encoding =UTF-8?& 
    < config>
    < modules>
    < MyNameSpace_MyModule>
    < version> 0.1.0< / version>
    < / MyNameSpace_MyModule>
    < / modules>

    < frontend>
    < routers>
    < checkout>
    < args>
    < modules>
    < MyNameSpace_MyModule before =Mage_Checkout> MyNameSpace_MyModule< / MyNameSpace_MyModule>
    < / modules>
    < / args>
    < / checkout>
    < / routers>
    < / frontend>


    请注意 before =Mage_Checkout将首先加载您的控制器(如果可用),并返回Magento的



    如果控制器位于另一个文件夹中,则必须修改代码:

    app / code / local / MyNameSpace / MyModule / controllers / Checkout / CartController.php。


  • 替换



    < MyNameSpace_MyModule before = Mage_Checkout> MyNameSpace_MyModule< / MyNameSpace_MyModule>



    with

     < MyNameSpace_MyModule before =Mage_Checkout> MyNameSpace_MyModule_Checkout< / MyNameSpace_MyModule> 




    1. 编辑 /CartController.php'



      创建 app / code / local / MyNameSpace / MyModule / controllers / Checkout / CartController。具有以下内容的php (对indexAction()的唯一更改是添加一个error_log()调用):

       <?php 
      #控制器不是自动加载的,所以我们必须手动执行:
      require_once'Mage / Checkout / controllers / CartController.php';
      类MyNameSpace_MyModule_Checkout_CartController extends
      Mage_Checkout_CartController
      {
      #重载的indexAction
      public function indexAction(){
      #只是为了确保
      error_log我做到了!');
      parent :: indexAction();
      }
      }




      1. 'app / etc / modules / MyNameSpace_All.xml'
        (这是为了激活您的模块)







        true
        local




    2. 编辑'app / design / frontend / [myinterface] / [mytheme] /layout/checkout.xml'并添加以下内容以使用与之前相同的更新句柄:

       < mynamespace_mymodule_checkout_cart_index> 
      < update handle =checkout_cart_index/>
      < / mynamespace_mymodule_checkout_cart_index>

      (请注意,这些标签看起来是区分大小写的。对于你)



      [通过Hendy:当我使用本Wiki或这里描述的方法覆盖目录/产品/视图时,我没有做上述。但是,当使用'cms方式',我不得不手动更新手柄。]



      上面的项目没有为我工作M Carvalho)



      我发现要更改的文件是app / design / frontend / [myinterface] / [mytheme] /layout/mymodule.xml



      添加以下行:







      1. 将您的浏览器指向/ checkout / cart /
        在您的PHP错误日志中,您会发现是的,我做到了!。

      您需要使用重写正则表达式更加精确,因为错误很难找到。





    I would like to do the above. Ive overridden many files in the past...block, model, helper....but this one eludes me.

    Can anyone see what im doing wrong here: (ive edited this code...to include some of the recomendations now...)

    Heres my folder structure (2 controller locations as a test):

    /Idigital/Idgeneral/etc/config.xml
    /Idigital/Idgeneral/controllers/Checkout/CartController.php
    /Idigital/Idgeneral/controllers/CartController.php
    

    Heres my config.xml:

    <?xml version="1.0"?>
    <config>
    <modules>
        <idigital_idgeneral>
        <version>0.1.0</version>
        </idigital_idgeneral>
    </modules>
    <global>
    <blocks>
            <idgeneral><class>Idigital_Idgeneral_Block</class></idgeneral>
        </blocks>
    </global>   
    
    <frontend>
        <routers>
                    <checkout>
                        <use>standard</use>
                        <args>
                            <modules>
                                <Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral_Checkout</Idigital_Idgeneral>
                            </modules>
                        </args>
                    </checkout>
               </routers>
           <layout>   
            <updates>   
                <idgeneral>   
                    <file>idigital.xml</file>   
                </idgeneral>   
            </updates>   
        </layout>
    </frontend>
    </config>
    

    Ihave placed my controller file in 2 locations to test. And heres the top of my FIRST controller file:

    require_once 'Mage/Checkout/controllers/CartController.php';
    class Idigital_Idgeneral_Checkout_CartController extends Mage_Checkout_CartController
    {
    
    
    public function testAction()
    {  
        var_dump('inside checkout/cart/test');exit; 
    }
    
    /**
     * Add product to shopping cart action
     */
    public function addAction()
    {
        blah...
    }
    

    Ans my second controller:

    require_once 'Mage/Checkout/controllers/CartController.php';
    class Idigital_Idgeneral_CartController extends Mage_Checkout_CartController
    {
    
    
    public function testAction()
    {  
        var_dump('inside cart/test');exit; 
    }
    
    /**
     * Add product to shopping cart action
     */
    public function addAction()
    {
        blah...
    }
    

    When i visit: /checkout/cart/add Im directed to the mage controller...not my local. (i have var_dump stmts in each..so i can see which is ran).

    When i visit /checkout/cart/test - i get a 404 When i visit /cart/add or cart/test - i get a 404 when i visit idgeneral/cart/test or idgeneral/cart/add - i get a 404

    解决方案

    1. Create your module folders and files

      app/code/local/MyNameSpace/MyModule/etc/config.xml
      app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php
      app/etc/modules/MyNameSpace_All.xml
      

    2. Edit /etc/config.xml and Create app/code/local/MyNameSpace/MyModule/etc/config.xml with the following content:

      <?xml version="1.0"?>
       <config>
      <modules>
          <MyNameSpace_MyModule>
              <version>0.1.0</version>
          </MyNameSpace_MyModule>
      </modules>
      <global>
          <!-- This rewrite rule could be added to the database instead -->
          <rewrite>
              <!-- This is an identifier for your rewrite that should be unique -->
              <!-- THIS IS THE CLASSNAME IN YOUR OWN CONTROLLER -->
              <mynamespace_mymodule_checkout_cart>
                  <from><![CDATA[#^/checkout/cart/#]]></from>
                  <!--
                      - mymodule matches the router frontname below
                      - checkout_cart matches the path to your controller
      
                      Considering the router below, "/mymodule/checkout_cart/" will be
                      "translated" to "/MyNameSpace/MyModule/controllers/Checkout/CartController.php" (?)
                  -->
                  <to>/mymodule/checkout_cart/</to>
              </mynamespace_mymodule_checkout_cart>
          </rewrite>
      </global>
      <!--
      If you want to overload an admin controller this tag should be <admin> instead,
      or <adminhtml> if youre overloading such stuff (?)
      -->
      <frontend>
          <routers>
              <mynamespace_mymodule>
                  <!-- should be set to "admin" when overloading admin stuff (?) -->
                  <use>standard</use>
                  <args>
                      <module>MyNameSpace_MyModule</module>
                      <!-- This is used when "catching" the rewrite above -->
                      <frontName>mymodule</frontName>
                  </args>
              </mynamespace_mymodule>
          </routers>
      </frontend>
      

      Note: The above didn’t work for me when I override catalog/product controller. I had to use:

                  <from><![CDATA[#^catalog/product/#]]></from>
                  <to>mymodule/mycontroller</to>
      

      (notice the missing leading slash)

      Since Magento 1.3 you can simply add your module to the frontend router. Rewrites are not neccessary any more:

        <?xml version="1.0" encoding="UTF-8"?>
       <config>
      <modules>
          <MyNameSpace_MyModule>
              <version>0.1.0</version>
          </MyNameSpace_MyModule>
      </modules>
      
      <frontend>
          <routers>
              <checkout>
                  <args>
                      <modules>
                          <MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule</MyNameSpace_MyModule>
                      </modules>
                  </args>
              </checkout>
          </routers>
      </frontend>
      

      Please note that before="Mage_Checkout" will load your controller first if available and fall back to Magento’s if not.

      If the controller is in another folder, you’ll have to modify the code:
      app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php.

    Replace

    <MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule</MyNameSpace_MyModule>

    with

        <MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule_Checkout</MyNameSpace_MyModule>
    

    1. Edit 'controllers/Checkout/CartController.php'

      Create app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php with the following content: (the only change to indexAction() is adding an error_log() call):

          <?php
             # Controllers are not autoloaded so we will have to do it manually:
             require_once 'Mage/Checkout/controllers/CartController.php';
             class MyNameSpace_MyModule_Checkout_CartController extends
                             Mage_Checkout_CartController
          {
          # Overloaded indexAction
           public function indexAction() {
          # Just to make sure
          error_log('Yes, I did it!');
          parent::indexAction();
          }
           }
      

      1. Edit 'app/etc/modules/MyNameSpace_All.xml' (This is to activate your module)

        true local

    2. Edit 'app/design/frontend/[myinterface]/[mytheme]/layout/checkout.xml' and add the following to use the same update handle as before:

       <mynamespace_mymodule_checkout_cart_index>
          <update handle="checkout_cart_index"/>
        </mynamespace_mymodule_checkout_cart_index>
      

      (Note that these tags seem to be case sensitive. Try using all lowercase if this isn’t working for you)

      [by Hendy: When I override catalog/product/view using the method described in this Wiki or here, I didn’t have to do the above. However, when using the 'cms way', I had to update the handle manually.]

      The above item did not work for me (2009-02-19 by Jonathan M Carvalho)

      I discovered that the file to change is app/design/frontend/[myinterface]/[mytheme]/layout/mymodule.xml

      Add the following lines:


      1. Point your browser to /checkout/cart/ In your PHP error log, you should find ‘Yes, I did it!’.

      You need to be extra precise with the rewrite regular expression because errors are very hard to find.

    这篇关于Magento覆盖控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆