热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

markdown为Magento1.x编写模块

本文由编程笔记#小编为大家整理,主要介绍了markdown为Magento1.x编写模块相关的知识,希望对你有一定的参考价值。
本文由编程笔记#小编为大家整理,主要介绍了markdown 为Magento 1.x编写模块相关的知识,希望对你有一定的参考价值。




# Writing Magento Modules
All custom modules should have a **Namespace** and **Module Name**.
These are used below as `{Namespace}` and `{Module}`.
> **Caution:** The Magento autoloader is known to have problems with CamelCase namespaces and/or modules between Windows and *nix systems. If your module requires more than one word for either of these, it is best to just concatenate them to avoid any issues (Example: `{Namespace}_{Examplemodule}`).
## Index
* [Directory Structure](#directory_structure)
* [Setup](#setup)
* [Tell Magento how to load your module](#step1)
* [Create your module's configuration](#step2)
* [Create your module's administration settings](#step3) (optional)
* [Write your module classes](#classes)
* [Blocks](#blocks)
* [Controllers](#controllers)
* [Frontend](#frontend)
* [Backend (Admin)](#backend)
* [Helpers](#helpers)
* [Models](#models)
* [Data Models](#data)
* [Resource Models](#resource)
* [Observers](#observers)
* [Extending Magento classes](#extending)
* [Tips](#tips)

## Standard Directory Structure
> **Note:** Not all paths are required (`app/design`, `skin`), only implement the ones you need.
```python
+-app
| +-code
| | +-local
| | +-{Namespace}
| | +-{Module}
| | +-Block
| | | +-Adminhtml
| | +-controllers
| | | +-Adminhtml
| | | | +-{ControllerName}Controller.php # Backend controller
| | | +-{ControllerName}Controller.php # Frontend controller
| | +-etc
| | | +-adminhtml.xml # Admin ACL and other settings
| | | +-config.xml # Configuration settings for your module
| | | +-system.xml # Administration settings and form options
| | +-Helper
| | | +-Data.php
| | +-Model
| | | +-Resource
| | | | +-{EntityName}
| | | | | +-Collection.php # Collection model for {entity}
| | | | +-{EntityName}.php # Resource model for {entity}
| | | +-Observer.php # Used for subscribing to Magento events
| | | +-{EntityName}.php # Data model for {entity}
| | +-sql
| | | +-{namespace}_{module}_setup
| | | +-mysql4-install-{X}.{X}.{X}.php
| | | +-mysql4-upgrade-{X}.{X}.{X}-{X}.{X}.{X}.php
| | +-Test
| +-design
| | +-adminhtml
| | | +-default
| | | +-default
| | | +-layout
| | | | +-{namespace}
| | | | +-{module}.xml
| | | +-template
| | | +-{namespace}
| | | +-{module}
| | +-frontend
| | +-base
| | +-default
| | +-layout
| | | +-{namespace}
| | | +-{module}.xml
| | +-template
| | +-{namespace}
| | +-{module}
| +-etc
| +-modules
| +-{Namespace}_{Module}.xml
+-skin
+-adminhtml
| +-default
| +-default
| +-css
| | +-{namespace}
| | +-{module}
| +-images
| | +-{namespace}
| | +-{module}
| +-js
| +-{namespace}
| +-{module}
+-frontend
+-base
+-default
+-css
| +-{namespace}
| +-{module}
+-images
| +-{namespace}
| +-{module}
+-js
+-{namespace}
+-{module}
```

## Setup

### 1. Tell Magento how to load your module
Magento needs to know that it should load your module and where to find it. For this, create an XML file under `app/etc/modules/{Namespace}_{Module}.xml`:
```xml



<{Namespace}_{Example}>
true
local







```
The `` block above is for illustrative purposes only. If your module requires others to be loaded before it, list them under `` - otherwise you can remove it or replace it with an empty node (``).

### 2. Create your module&#39;s configuration
Create your module&#39;s `config.xml` under `app/code/local/{Namespace}/{Module}/etc`. Again, not all sections are required and it will greatly depend on your module and what it needs - however, this example covers the most common use cases:
```xml



<{Namespace}_{Example}>
{X}.{X}.{X}




<{namespace}_{module}>
{Namespace}_{Example}_Block



<{namespace}_{module}>
{Namespace}_{Example}_Helper



<{namespace}_{module}>
{Namespace}_{Example}_Model
{namespace}_{module}_resource

<{namespace}_{module}_resource>
{Namespace}_{Example}_Model_Resource


<{entity_name}}>

{namespace}_{module}_{table_name}





<{namespace}_{module}_setup>

{Namespace}_{Example}


core_setup


<{namespace}_{module}_write>

core_write


<{namespace}_{module}_read>

core_read





<{event_name}>

<{namespace}_{module}_{event_name}>
model
{namespace}_{module}/observer
{methodName}











<{namespace}_{module} before="Mage_Adminhtml">{Namespace}_{Example}_Adminhtml









<{namespace}_{module}>
{namespace}/{module}.xml







<{namespace}_{module}>
standard

{Namespace}_{Example}
{namespace}






<{namespace}_{module}>
{namespace}/{module}.xml






<{namespace}_{module}>
<{group_name}>
0
0






<{namespace}_{module}_{job_name}>
* * * * *
{namespace}_{module}/observer::{methodName}




```
The comments above should explain which sections can be removed if your module does not require/need them.
> **Note:** Be sure to replace `{X}.{X}.{X}` with your module&#39;s version number (Example: `1.0.0`). This number is stored in the database table `core_resource` and is also used for running module setup scripts under `app/code/local/{Namespace}/{Module}/sql/{namespace}_{module}_setup`.

### 3. Create your module&#39;s administration settings (optional)
Create the file `system.xml` under `app/code/local/{Namespace}/{Module}/etc`. Within this file you can define custom admin configuration tabs, admin settings, etc. Two example settings are included below (`enabled` and `debug`) which provide simple on/off settings in the Magento administration interface.
```xml



<{namespace}_{module} translate="label" module="{namespace}_{module}">

300



<{namespace}_{module} translate="label" module="{namespace}_{module}">

{namespace}_{module}
1
1
1
1

<{group_name} translate="label">

1
1
1
1



select
adminhtml/system_config_source_yesno
1
1
0
0


1

select
adminhtml/system_config_source_enabledisable
2
1
0
0







```
> **Note:** Each setting relates to the `` key in `config.xml` - which specifies that setting&#39;s default value.
In order for the settings to show up/work - you must also create ACL rules for your new admin config section. For that, we need to create `adminhtml.xml` under `app/code/local/{Namespace}/{Module}/etc`.
```xml










<{namespace}_{module] translate="title" module="{namespace}_{module]">

1










```
> **Note:** For older Magento versions, it is necessary to also specify the above `` area under the `` section of `config.xml`.

## Write your module classes

### Blocks
Blocks control the HTML output of a specific section/area of various pages within Magento. You can create your own blocks but the most common use is to extend another Magento block.
```php
class {Namespace}_{Module}_Block_{BlockName} extends Mage_Core_Block_Template // For Adminhtml, extend: Mage_Adminhtml_Block_Abstract
{
}
```

### Controllers
Controllers are broken into 2 types: [frontend](#frontend) and [backend](#backend) (admin). The difference requires extending different base Magento classes.

#### Frontend
```php
class {Namespace}_{Module}_{ControllerName}Controller extends Mage_Core_Controller_Front_Action
{
/**
* Maps to: /{namespace}/{controllerName}/{actionName}/
*/
public function {actionName}Action()
{
// Example of retrieving a query parameter
$queryParameter = $this->getRequest()->getParam(&#39;query_parameter&#39;, &#39;Default Value (if not present)&#39;);
// Example of getting a URL from the router
$url = Mage::app()->getStore()->getUrl(&#39;{module}/{controller}&#39;);
// Example of performing a redirect
$this->_redirectUrl($url);
}
}
```

#### Backend (Admin)
```php
class {Namespace}_{Module}_Adminhtml_{ControllerName}Controller extends Mage_Adminhtml_Controller_Action
{
protected function _initAction()
{
$this->loadLayout()
->_setActiveMenu(&#39;{menu_name}&#39;)
->_addBreadcrumb(
&#39;{Breadcrumb Text}&#39;,
&#39;{Breadcrumb Text}&#39;,
// ...
);
return $this;
}
public function {actionName}Action()
{
$this->_title(&#39;{Title Text}&#39;);
$this->_initAction()->renderLayout();
// Example of adding a success notice message
Mage::getSingleton(&#39;adminhtml/session&#39;)->addSuccess(&#39;{Success Message Text}&#39;);
// Example of adding an error notice message
Mage::getSingleton(&#39;adminhtml/session&#39;)->addError(&#39;{Error Message Text}&#39;);
}
}
```

### Helpers
You must create one Helper class, however - it can remain empty. These classes are used for general functions/methods that are required between different class types (Example: retrieving configuration values).
```php
class {Namespace}_{Example}_Helper_Data extends Mage_Core_Helper_Abstract
{
}
```
> **Note:** This class can then be retrieved with: `Mage::helper(&#39;{namespace}_{module}&#39;)`

### Models
Models can be broken into 2 types: [data models](#data) and [resource models](#resource). Resource models control the interaction with the database (MySQL) whereas data models can range in responsibilities but usually manipulate data between controllers and resource models.

#### Data Model
```php
class {Namespace}_{Module}_Model_{EntityName} extends Mage_Core_Model_Abstract
{
protected function _construct()
{
$this->_init(&#39;{namespace}_{module}/{entity_name}&#39;);
}
}
```
> **Note:** This class can then be retrieved with: `Mage::getModel(&#39;{namespace}_{module}/{entity_name}&#39;)`

#### Resource Model
```php
class {Namespace}_{Module}_Model_Resource_{EntityName} extends Mage_Core_Model_Mysql4_Abstract
{
protected function _construct()
{
$this->_init(&#39;{namespace}_{module}/{entity_name}&#39;, &#39;{primary_key}&#39;);
}
}
```
> **Note:** This class can then be retrieved with: `Mage::getResourceModel(&#39;{namespace}_{module}/{entity_name}&#39;)` However, it is common practice to retrieve it from the data model instance: `Mage::getModel(&#39;{namespace}_{module}/{entity_name}&#39;)->getResource()`
You can also (optionally) define the Collection class used. The Collection is used to handle many instances of your resource models (saving many rows, retrieving many rows, iterating, etc.).
```php
class {Namespace}_{Module}_Model_Resource_{EntityName}_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
protected function _construct()
{
$this->_init(&#39;{namespace}_{module}/{entity_name}&#39;);
}
}
```
> **Note:** This class can then be retrieved from the data modal instance with: `Mage::getModel(&#39;{namespace}_{module}/{entity_name}&#39;)->getCollection()`

#### Observers
The easiest and most flexible way of extending Magento features is by listening and responding to events with an observer class. Which events your module listens for is defined in your module&#39;s `config.xml`.
```php
class {Namespace}_{Module}_Model_Observer
{
public function {methodName}(Varien_Event_Observer $observer)
{
return $this;
}
}
```

## Extending Magento classes
Extending a Magento (or other module) class is relatively easy. For blocks, helpers, models, and resource models just use the following syntax in your module&#39;s `config.xml` (under ``, ``, etc.) to rewrite a Magento class to yours:
```xml
<{module_to_override}>

<{class_to_override}>{Namespace}_{Module}_Block_{ClassToOverride}


```
Here is an example of rewriting `Mage_Core_Model_Email_Template` with `Acme_Custom_Model_Email_Template`:
```xml






Acme_Custom_Model_Email_Message





```
> **Caution:** It is important to remember that core Magento modules (those namespaced with `Mage_`) do not require the use of `mage_`. So the example above uses just `core` rather than `mage_core`.

## Tips
#### `var_dump()` and Magento
Trying to `var_dump()` a Magento class instance will quickly lead to headaches because of circular references. Most Magento classes extend `Varien_Object` and thus expose a `debug()` method which is much better for dumping:
```php
var_dump($object->debug());
```
#### Logging
The easiest way to write to the system logger (or your own custom log file) is to use the `log()` method exposed by the `Mage` class:
```php
Mage::log(&#39;{Log Message Text}&#39;, (int) &#39;{(optional) Log Level}&#39;, &#39;{(optional) Log Filename}&#39;, (bool) &#39;{(optional) Force Writing}&#39;);
```


推荐阅读
  • 实验2:Open vSwitch虚拟交换机实践   实验3:OpenFlow协议分析实践
    实验2:OpenvSwitch虚拟交换机实践一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • 本文介绍了一个Magento模块,其主要功能是实现前台用户利用表单给管理员发送邮件。通过阅读该模块的代码,可以了解到一些有关Magento的细节,例如如何获取系统标签id、如何使用Magento默认的提示信息以及如何使用smtp服务等。文章还提到了安装SMTP Pro插件的方法,并给出了前台页面的代码示例。 ... [详细]
  • 本文详细介绍了在Linux虚拟化部署中进行VLAN配置的方法。首先要确认Linux系统内核是否已经支持VLAN功能,然后配置物理网卡、子网卡和虚拟VLAN网卡的关系。接着介绍了在Linux配置VLAN Trunk的步骤,包括将物理网卡添加到VLAN、检查添加的VLAN虚拟网卡信息以及重启网络服务等。最后,通过验证连通性来确认配置是否成功。 ... [详细]
  • 大坑|左上角_pycharm连接服务器同步写代码(图文详细过程)
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了pycharm连接服务器同步写代码(图文详细过程)相关的知识,希望对你有一定的参考价值。pycharm连接服务 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了markdown[软件代理设置]相关的知识,希望对你有一定的参考价值。 ... [详细]
  • charles3.11.1抓https包
    结论先行:用的是安卓测试机,没加固之前的生产环境的安装包,可以抓到https请求加固之后的包【也就是要上应用市场的包】,抓不到https请求电脑上的操作:1.安装证书【电脑上安装了 ... [详细]
  • vuepress是Vue驱动的静态站点生成工具本文仅介绍,搭建静态博客的过程,具体教程及文档请点击进入vuepress中文网点击查看项目代码vuepress初始化下面初始化#将github新创建的仓库克隆到本地 ... [详细]
  • 本文介绍了如何清除Eclipse中SVN用户的设置。首先需要查看使用的SVN接口,然后根据接口类型找到相应的目录并删除相关文件。最后使用SVN更新或提交来应用更改。 ... [详细]
  • 本文介绍了如何在使用emacs时去掉ubuntu的alt键默认功能,并提供了相应的操作步骤和注意事项。 ... [详细]
  • 使用eclipse创建一个Java项目的步骤
    本文介绍了使用eclipse创建一个Java项目的步骤,包括启动eclipse、选择New Project命令、在对话框中输入项目名称等。同时还介绍了Java Settings对话框中的一些选项,以及如何修改Java程序的输出目录。 ... [详细]
  • Summarize function is doing alignment without timezone ?
    Hi.Imtryingtogetsummarizefrom00:00otfirstdayofthismonthametric, ... [详细]
author-avatar
益清613
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有