Deep understanding of magento-Nineth-Modify, Extend, rewrite Magento code

Source: Internet
Author: User
Tags cdata extend php class php file regular expression zend zend framework

As a developer you are sure to modify the Magento code to fit your business needs, but in many cases we don't want to modify the core code of Magento, there are many reasons, such as the future want to upgrade Magento, but also want to use more Magento code. This article will be a good tutorial if you are looking for the best way to modify the Magento code.

Suitable for: Advanced developers

Fit to target: Developers want custom modifications Magento

Current version: Magento versions:1.4.0.1

Author: Jing Dong

Last modified: May 30, 2010

Version: V 0.3.0 rewrite Magento module

The first step, you need to create your own code namespace, such as Wemvc,app, in order to facilitate the sharing of code with you, I name the space as an app.

app/code/core/community/local/app/

If you are going to modify the mage/catalog/block/breadcrumbs.php file now, you can add a new module "Catalog" to your namespace and app. Next, create a block directory and copy the breadcrumbs.php into your new directory. You will also need to create a config configuration file.

app/code/core/community/local/app/catalog/block/breadcrumbs.php Etc/config.xml

Modify the Breadcrumbs.php class name to App_catalog_block_breadcrumbs and inherit the original class name Mage_catalog_block_breadcrumbs.
Now you need to activate your new module so that Magento can know your new module.

To create the file App/etc/modules/app_all.xml, add the following code.

< XML version= "1.0"?> <config> <modules> <App_Catalog> <active>true</active> <codePool>local</codePool> </App_Catalog> </modules> </config>

Below we need a special tag to write off breadcrumbs, below we are through the module configuration file to implement. rewrite Magento chunks (Blocks)

Edit File "App/code/local/app/catalog/etc/config.xml"

<?xml version= "1.0" encoding= "UTF-8"?> <config> <modules> <App_Catalog> <version>0.1.0 </version> </App_Catalog> </modules> <global> <blocks> <catalog> <rewrite> <breadcrumbs>App_Catalog_Block_Breadcrumbs</breadcrumbs> </rewrite> </catalog> </ Blocks> </global> </config>

We need to add a "blocks" tab, or add content to the "blocks" tab that already exists. Then add the rewrite tag after the module name, in this case the module name is "Catalog". Then we look at the "Breadcrumbs" tab, which helps Magento find the block we want to modify. In our sake, Breadcrumbs is the class name in the Magento core code: app/code/core/mage/catalog/block/breadcrumbs.php. If you have more directory levels, you can use a glide line to separate them. For example

<blocks> <catalog> <rewrite> <category_view>app_catalog_block_category_view</category_ view> </rewrite> </catalog> </blocks>

In this example, we rewrite the app/code/core/mage/catalog/block/category/view.php.

The value in the breadcrumbs tag is your class name, so Magento can get your class, because the class name matches your directory name. People who have used the Zend framework know that automatic loading of auto Loader this thing, it will go with your class name in the sliding line in your directory needs the corresponding class file. Remember, the slide line represents the next level of folder, and if your class name does not match your file directory name, then Magento will not ignore you at all.
For example:

app_catalog_block_breadcrumbs→/app/code/local/app/catalog/block/breadcrumbs.php App_Catalog_Block_Category_View →/app/code/local/app/catalog/block/category/view.php

override Magento Controller-regular expression matching

Rewrite the Magento controller we take the example of rewriting the shopping cart.

1, first create a new module under the app, and then create the following files in turn:

/app/code/local/app/shopping/app/code/local/app/shopping/etc/app/code/local/app/shopping/etc/config.xml/app/ code/local/app/shopping/controllers/app/code/local/app/shopping/controllers/cartcontroller.php

2, edit the/app/code/local/app/shopping/etc/config.xml file, add the following code:

<?xml version= "1.0"?> <config> <modules> <App_Shopping> <version>0.1.0</version > </App_Shopping> </modules> <global> <!--This rewrite rule could is added to the database Instea D--<rewrite> <!--This is a identifier for your rewrite it should be unique---!--This is the C Lassname in YOUR OWN CONTROLLER--<App_Shopping_cart> <from><!--[cdata[#^/checkout/cart/#]]--> </from> <!---shopping module matches the router Frontname Below-checkout_cart matches the path to your contro Ller considering the router below, "/shopping/cart/" would be "translated" to "/app/shopping/controllers/ Cartcontroller.php "(?)-<to>/shopping/cart/</to> </App_Shopping_cart> </rewrite> < /global> <!--If you want to overload a admin-controller this tag should be <admin> instead, or <adminhtml > If youre overloading such stuff (?)--<frontend> <routers> <App_Shopping> <!--should is set to ' admin ' when overloading admin stuff (?)--<use>s Tandard</use> <args> <module>App_Shopping</module> <!--this was used when "catching" the Rewrite above-<frontName>shopping</frontName> </args> </App_Shopping> </routers > </frontend> </config> 

3. Rewrite your own controller
/app/code/local/app/shopping/controllers/cartcontroller.php
Add the following code to your controller, and the only place we will modify it is to add a error_log () to the index action;

< PHP # Controller does not load automatically, so we need to include the file, which is not the same as the Block (block) require_once ' mage/checkout/controllers/cartcontroller.php '; Class App_shopping_cartcontroller extends Mage_checkout_cartcontroller {#覆写indexAction方法 public function indexaction ( {# Just to make sure Error_log (' Yes ~ successfully rewritten shopping cart. '); Parent::indexaction (); } }

In this code, the first is the class name, as in the previous block (block), our own class name is App_shopping_cartcontroller inherit the original Mage_checkout_ Cartcontroller. We recorded a piece of information in Indexaction.

4, modify the App_all.xml, activate our new shopping module

<?xml version= "1.0"?> <config> <modules> <App_Catalog> <active>true</active> <codePool>local</codePool> </App_Catalog> <App_Shopping> <active>true</active > <codePool>local</codePool> </App_Shopping> </modules> </config>

Here, after clearing the cache, you can already see Error_log successfully recorded our information, open the page http://www.wemvc.dev/checkout/cart/, show the shopping cart page, everything is OK, but if you visit HTTP// Www.wemvc.dev/shopping/cart/, you will find the homepage .... We expect the shopping cart view has not yet appeared, how to solve it. Let's look at the next step down.

5. Modify the View file App/design/frontend/[myinterface]/[mytheme]/layout/checkout.xml
In the Layout tab, add the following:

<app_shopping_cart_index> <update handle= "Checkout_cart_index"/> </app_shopping_cart_index>

Note that the case is sensitive here.

I'm basically done here, but I suggest you learn the regular expressions, because there's just one paragraph in the code:

<from><! [cdata[#^/checkout/cart/#]]></from>

This is used to match the regular expression.

Also, after trying, it is possible to support the same module name overlay, such as the Magento code in the Product Details page is mage_catalog_productcontroller::viewaction (), if we want to rewrite this controller, We can do this:
A. CV New catalogue/app/code/local/app/catalog/controllers/productcontroller.php
The code is as follows:

Require_once ' mage/catalog/controllers/productcontroller.php '; /** * PRODUCT Controller * * @category Mage * @package Mage_catalog */class App_catalog_productcontroller extends Mage_ca Talog_productcontroller {/** * View product Action */Public Function viewaction () {echo ' covered ... '; Parent::viewaction ( ); } }

B. Edit the/app/code/local/app/catalog/etc/config.xml with the following code:

<?xml version= "1.0" encoding= "UTF-8"?> <config> <modules> <App_Catalog> <version>0.1.0 </version> </App_Catalog> </modules> <global> <!--This rewrite rule could is added to the data Base instead-<rewrite> <!--This is a identifier for your rewrite, should be unique---<!--TH Is are the CLASSNAME in YOUR OWN CONTROLLER--<App_Shopping_cart> <from><!--[cdata[#^/catalog/product /#]]--></from> <!---shopping module matches the router Frontname Below-checkout_cart matches the path to Y Our controller considering the router below, "/shopping/cart/" would be "translated" to "/app/shopping/controllers/cartcon Troller.php "(?)-<to>/catalog/product/</to> </App_Shopping_cart> </rewrite> < blocks> <catalog> <rewrite> <breadcrumbs>App_Catalog_Block_Breadcrumbs</breadcrumbs> </rewrite> </catalog> </blocks> </global> <frontend> <routers> <catalog> <use>standard</use> <args> <module >App_Catalog</module> <frontName>catalog</frontName> </args> </catalog> </ Routers> </frontend> </config> 

Empty the cache, refresh your product Details page to see if it has changed, hehe. But this method has a drawback, you need to copy all the controllers of this module, or you will encounter a lot of trouble. Speaking of which, I'll introduce a rewrite method.
Look closely at the wording of the configuration file:

<?xml version= "1.0"?> <config> <modules> <App_Mycms> <version>0.1.0</version> </App_Mycms> </modules> <frontend> <routers> <mycms> <use>standard</use> <args> <module>App_Mycms</module> <frontName>mycms</frontName> </args> </ mycms> </routers> </frontend> <global> <routers> <cms> <rewrite> <index> <to>App_Mycms/index</to> <override_actions>true</override_actions> <actions> < noroute><to>app_mycms/index/noroute</to></noroute> </actions> </index> </ rewrite> </cms> </routers> </global> </config>

overriding Magento Model and Action Helper (Model&helper)

We are rewriting the process of magento, in order to achieve their business logic, it is inevitable to change its business model, here first introduced a simplest approach. Before we have to, Magento is using the Zend Framework frame:

The value in the breadcrumbs tag is your class name, so Magento can get your class, because the class name matches your directory name. People who have used the Zend framework know that automatic loading of auto Loader this thing, it will go with your class name in the sliding line in your directory needs the corresponding class file. Remember, the slide line represents the next level of folder, and if your class name does not match your file directory name, then Magento will not ignore you at all.
For example:

app_catalog_block_breadcrumbs→/app/code/local/app/catalog/block/breadcrumbs.php App_Catalog_Block_Category_View →/app/code/local/app/catalog/block/category/view.php

Let's look at the Magento launcher, a snippet of code in the Mage class:

if (defined (' Compiler_include_path ')) {$app _path = Compiler_include_path; Set_include_path ($app _path. Ps. Mage::registry (' Original_include_path ')); Include_once "mage_core_functions.php"; Include_once "varien_autoload.php"; } else {/** * Set include path */$paths [] = BP. Ds. ' App '. Ds. ' Code '. Ds. ' Local '; $paths [] = BP. Ds. ' App '. Ds. ' Code '. Ds. ' Community '; $paths [] = BP. Ds. ' App '. Ds. ' Code '. Ds. ' Core '; $paths [] = BP. Ds. ' Lib '; $app _path = Implode (PS, $paths); Set_include_path ($app _path. Ps. Mage::registry (' Original_include_path '));

Magento added its own several include paths (include path) to the original inclusion path, namely the Local,community,core code Pool directory and then the Varien Library directory, which determines the order in which PHP queries PHP classes. Is Local,community,core, because Magento does not explicitly load any libraries, but takes advantage of PHP's __autoload features.

If you create a mage namespace (directory) under the Local Code pool (directory), you can almost rewrite any of the Magento classes. For example, if we want to rewrite the Mage_catalog_model_category class, then you can create the following file directory structure:
app/code/local/mage/catalog/model/category.php
So you can copy the original file directly, and then you can make any changes, without worrying about the issue of the version update.

Here I have to tell you that the method of overriding the class is not the best solution, because you have to copy a class every time, this way with the direct modification of the Magento code is very different. You can try to configure your own class with the configuration file under the module, inherit the model or helper you want to rewrite, and then call your own class. Now let's take a look at the user model for an in-depth explanation.

A. Create your own module folder first

App/code/local/app/customer App/code/local/app/customer/etc/config.xml App/code/local/app/customer/model App/code /local/app/customer/model/customer.php

B. Modify App/etc/modules/app_all.xml

<App_Customer> <active>true</active> <codePool>local</codePool> </app_customer >

C. Modify your own module configuration file App/code/local/app/customer/etc/config.xml

<?xml version= "1.0" encoding= "UTF-8"?> <config> <modules> <App_Customer> <version> 0.1.0</version> </App_Customer> </modules> <global> <models> <customer> < rewrite> <customer>App_Customer_Model_Customer</customer> </rewrite> </customer> </ Models> </global> </config>

D. Now write your new Model and create a new class in file app/code/local/app/customer/model/customer.php App_customer_model_cutomer

Class App_customer_model_customer extends Mage_customer_model_customer {//Override existing method public function validate () {//Defin e new validate rules. From now Magento call this validate method instead of existing method//return $errors; return true; }//You can also create a new method public function Newmethod () {//function logic}}

E. We rewrite a class to deepen our understanding. Next we rewrite the customer Address Model. As with the rewrite Customer model, we first edit the module's configuration file, App/code/local/app/customer/etc/config.xml.

<?xml version= "1.0" encoding= "UTF-8"?> <config> <modules> <App_Customer> <version> 0.1.0</version> </App_Customer> </modules> <global> <models> <customer> < Rewrite> <customer>App_Customer_Model_Customer</customer> <address>app_customer_model_ address</address> </rewrite> </customer> </models> </global> </config>

Can you see that the customer and address in the rewrite tag are actually the Magento model you are going to overwrite?
Next create the Model class App_customer_model_address and write the method you want to overwrite and add

Class App_customer_model_address extends Mage_customer_model_address {//Override existing method public function validate () {//Define New validate rules. From now Magento call this validate method instead of existing method//return $errors; return true; }//You can also create a new method public function Newmethod () {//function logic}}

F. I'll talk about how to cover Magento's model resources, here, as an example of the replication address Entity model class, we'll first modify the module's configuration file App/code/local/app/customer/etc/config.xml.

<?xml version= "1.0" encoding= "UTF-8"?> <config> <modules> <App_Customer> <version> 0.1.0</version> </App_Customer> </modules> <global> <models> <customer> < Rewrite> <customer>App_Customer_Model_Customer</customer> <address>app_customer_model_ address</address> </rewrite> </customer> <customer_entity> <rewrite> <address> app_customer_model_entity_address</address> </rewrite> </customer_entity> </models> </ Global> </config>

Next, create the class file.

Class App_customer_model_entity_address extends Mage_customer_model_entity_address {protected function _aftersave ( Varien_object $address) {//Write your code}}

Summary

In this chapter we learned how to rewrite modules, rewrite controllers, rewrite chunks, and rewrite models and helpers, and basically rewrite the Magento code is no longer a difficult task for you. At this point, to congratulate you, you have mastered most of the skills to modify Magento. In the following article we will conduct a more in-depth study. Finally thanks to all Sasacake Team Member, it is their passion and sense of responsibility to work to motivate me to write these tutorials.

From: Fine Blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.