Magento 重写 Block, Model, Helper, controllers

2016年3月14日 Edited 2022年12月6日 浏览量 32 3 min read
Magento 重写 Block, Model, Helper, controllers

在使用magento的过程中,几乎没有人会一成不变的使用magento框架所自带的功能,无论什么项目几乎都会有多多少少在功能上的变化需求,如添加一个新的功能,或者重写现有的功能模块。但是如果我们直接修改magento的核心代码结构,这是一个非常不好的习惯,而且我们也不推荐开发者这样来做。因此magento为大家提供了一系列非常好的方式方法来重写或者覆盖现有的方法。

我们在上一篇文章中已经提到了如何来重写一个model,但是如果我们有需求来重写helpers,blocks或者controllers(包括前台controller和后台controller),怎么办?下面我们来介绍一下如何来重写magento的blocks, models, helpers ,controllers。

1,重写magento blocks。 我们从重写magento核心block标签开始,例如重写Mage_Tag_Block_Product_List 类,我们想在这里添加我们自己的类,并且拓展当前的核心类目以及方法。首先我们要做的就是在当前新建的模块中的配置文件添加如下代码:



<config>
    <global>
        <blocks>	
            <tag>		
                <rewrite>			
                    <product_list>Magease_Tag_Block_Product_List</product_list>			
                </rewrite>		
            </tag>	
        </blocks>
    </global>
</config>


之后,在当前模块中新建block文件,app/code/community/Magease/Tag/Block/Product/List.php



class Magease_Tag_Block_Product_List extends Mage_Tag_Block_Product_List{
/**
/*
/* 代码
/*
/**
}

同理,我们可以用相同的规则来实现对Mage_Adminhtml_Block_Tag_Edit 类的重写。



<config>
    <global>	
        <blocks>		
            <adminhtml>			
                <rewrite>	
                    <tag_edit>Magease_Tag_Block_Adminhtml_Tag_Edit</tag_edit>	
                </rewrite>	
            </adminhtml>	
        </blocks>	
    </global>
</config>


按照如上配置文件中的类名,新建app/code/community/Magease/Tag/Block/Adminhtml/Tag/Edit.php文件,



class Magease_Tag_Block_Adminhtml_Tag_Edit extends Mage_Adminhtml_Block_Tag_Edit{
/**
/*
/* 代码
/*
/**
}

2.重写magento helpers 按照相同的规则我们来重写magento的helper类,模块配置代码如下:


<config>
    <global>	
        <helpers>
            <tag>
                <rewrite>	
                    <data>Magease_Tag_Helper_Data</data>	
                </rewrite>	
            </tag>	
        </helpers>
    </global>
</config>

接下来我们新建helper类文件,app/code/community/Magease/Tag/Helper/Data.php


class Magease_Tag_Helper_Data extends Mage_Tag_Helper_Data{
/**
/*
/* 代码
/*
/**
}

3.重写magento models 我们在上一篇文章中讲到了如何来重写magento models,但是resource类和collection 类如何来从写呢?与重写model类同理,resource类和collection类也可以很容易的重写掉,我们来拓展以下三个类:



Mage_Tag_Model_Tag
Mage_Tag_Model_Resource_Tag
Mage_Tag_Model_Resource_Tag_Collection


配置文件写法如下:



<config>
    <global>
        <models>		
            <tag>	
                <rewrite>	
                    <!-- Model -->		
                    <tag>Magease_Tag_Model_Tag</tag>	
                </rewrite>	
            </tag>	
            <tag_resource>	
                <rewrite>			
                    <!-- Resource -->	
                    <tag>Magease_Tag_Model_Resource_Tag</tag>	
                    <!-- Collection -->			
                    <tag_collection>Magease_Tag_Model_Resource_Tag_Collection</tag_collection>	
                </rewrite>	
            </tag_resource>	
        </models>	
    </global>
</config>


下面到模块中新建如下文件:

app/code/community/Magease/Tag/Model/Tag.php

app/code/community/Magease/Tag/Model/Resource/Tag.php

app/code/community/Magease/Tag/Model/Resource/Tag/Collection.php.

需要注意的是,在这里我们重写这三个类,只是为了演示给开发者看,具体到现实环境中,还需要根据自身的项目需求来重写你需要的类即可。

4.重写magento controllers

重写magneto controller跟我们之前看的这些重写models,重写blocks,重写helpers都有很大得区别,那么如何来重写magneto的控制器呢,以Mage_Tag模块为例,下面我们来看一下如何重写magento controller,首先先看config.xml文件。



<config>	
    <frontend>	
        <routers>	
            <tag>	
                <args>		
                    <modules>				
                        <magease_tag before="Mage_Tag">Magease_Tag</magease_tag>	
                    </modules>			
                </args>		
            </tag>		
        </routers>	
    </frontend>
</config>


你是否注意到了,我们在这里并没有定义一些文件,我们在这里只是定义了一个路径,这个路径可以通过名字来找到controllers的类文件。所以如果你想重写app/code/core/Mage/Tag/controllers/TagController.php这个控制器,我们应该在app/code/community/Magease/Tag/controllers/TagController.php下创建一个一模一样名字的控制器文件。 这里在文件中的写法也是跟其他文件的重写方法不一样,如:


require_once(Mage::getModuleDir('controllers','Mage_Tag').DS.'TagController.php'); 
class Magease_Tag_TagController extends Mage_Tag_TagController{
/**
/*
/* 代码
/*
/**
}

重写adminhtml的控制器也是同样的方法,在config.xml中按照如下格式:


<config>	
    <admin>	
        <routers>
            <adminhtml>		
                <args>				
                    <modules>			
                        <magease_tag before="Mage_Adminhtml">Magease_Tag_Adminhtml</magease_tag>	
                    </modules>	
                </args>		
            </adminhtml>	
        </routers>	
    </admin>
</config>

新建文件app/code/local/community/Magease/controllers/Adminhtml/TagController.php


require_once(Mage::getModuleDir('controllers','Mage_Adminhtml').DS.'TagController.php');
 class Magease_Tag_Adminhtml_TagController extends Mage_Adminhtml_TagController{
/**
/*
/* 代码
/*
/**
}

到这里,magento的模块重写的内容方法基本已经全都在这里了,剩下的就是通过你自己的方法和技术,来写下你自己想要的模块功能或拓展吧。

Previous article:
Next article:
Comments
发表评论,留下你的足迹
我们不会公开你的邮箱地址

是否允许我们在发布新内容或者进行促销活动向您发送消息?

Remind me later

Thank you! Please check your email inbox to confirm.

Oops! Notifications are disabled.

© 2014-2023 www.magease.com. All Rights Reserved. 寰云网络 版权所有    鲁ICP备 14014975号-1