
建立magento子產品
由于我在做我自己的magento項目,我将使用我自己的項目名“app”。 然後,我們要建立以下目錄結構
app/code/local/app/shopping/block
app/code/local/app/shopping/controller //controllers基類
app/code/local/app/shopping/controllers
app/code/local/app/shopping/etc
app/code/local/app/shopping/helper
app/code/local/app/shopping/model
app/code/local/app/shopping/sql
class app_shopping_controller_action extends mage_core_controller_front_action{
}
你的插件并不一定需要包含以上所有的目錄,但是為了以後開發友善,我們還是在一開始就把目錄建立好。接下來我們要建立兩個檔案,一個是config.xml,放在etc目錄下面
app/code/local/app/shopping/etc/config.xml
檔案内容如下
<config>
<modules>
<app_shopping>
<version>0.1.0</version>
</app_shopping>
</modules>
</config>
第二個檔案需要在如下位置建立
app/etc/modules/app_shopping.xml
第二個檔案應該遵循如下命名規則“packagename_modulename.xml”,檔案内容如下
<modules>
<app_shopping>
<active>true</active>
<codepool>local</codepool>
</app_shopping>
</modules>
該檔案的目的是讓magento系統載入該子產品。<active>标簽為true表示使該子產品生效。
也可以是packagename_all.xml,裡面配置所有 module。
<?xml version="1.0"?>
<app_catalog>
<active>true</active>
<codepool>local</codepool>
</app_catalog>
</config>
我們先不管這些檔案是幹什麼的,以後會解釋。建立好這兩個檔案以後,你的子產品的骨架就已經完成了。magento已經知道你的子產品存在,但是現在你的子產品不會做任何事情。我們來确認一下magento确實裝載了你的子產品
清空magento緩存
在背景管理界面,進入 system->configuration->advanced
展開“disable modules output”
确認“app_shopping ”顯示出來了
如果你看到“app_ shopping ”,那麼恭喜你,你已經成功建立了你第一個magento子產品!
2建立的子產品不會做任何事情,下面我們來為這個子產品加入邏輯
首先在app下建立新的子產品,依次建立如下檔案:
/app/code/local/app/shopping/controllers/cartcontroller.php
<?php
class app_shopping_cartcontroller extends mage_core_controller_front_action {
public function indexaction() {
echo 'hello magento';
//顯示layout中配置的block shopping_cart_index
$this->loadlayout();
$this->renderlayout();
}
編輯/app/code/local/app/shopping/etc/config.xml檔案,加入如下代碼:
<frontend>
<routers>
<app_shopping>
<use>standard</use>
<args>
<module>app_shopping</module>
<!-- this is used when "catching" the rewrite above -->
<frontname>shopping</frontname>
</args>
</app_shopping>
</routers>
<layout> <!-- 不配置layout标簽預設讀customer.xml-->
<updates>
<app_shopping>
<file>shopping.xml</file>
</app_shopping>
</updates>
</layout>
</frontend>
frontend/routers/用來設定使該子產品從前端顯示的入口。frontname稍後将出現在url中 /shopping/cart
修改視圖檔案app/design/frontend/[myinterface]/[mytheme]/layout/shopping.xml在layout标簽中,添加下面内容:
<ticket_index_index><!-- frontname_controller_action -->
<update handle="customer_account"/>
<reference name="my.account.wrapper">
<block type="ticket/ticket" name="ticket" template="ticket/index.phtml"/><!-- 使用block檔案 -->
</reference>
</ticket_index_index>
注意,xml的大小寫敏感。 背景管理 system->configuration->設計->主題 預設。 來生效設定
添加背景的layout xml需要在config.xml添加adminhtml節點
<adminhtml>
<layout>
<updates>
<sintax>
<file>sintax.xml</file>
</sintax>
</updates>
</layout>
</adminhtml>
檔案: app/design/adminhtml/default/default/layout/sintax.xml
<sintax_adminhtml_myform_index>
<reference name="content">
<block type="adminhtml/template" name="myform" template="sintax/myform.phtml"/>
</reference>
</sintax_adminhtml_myform_index>
form 模闆頁
檔案: app/design/adminhtml/default/default/template/sintax/myform.phtml
<div class="content-header">
<table cellspacing="0" class="grid-header">
<tr>
<td><h3><?php echo $this->__('my form title')?></h3></td>
<td class="a-right">
<button onclick="editform.submit()" class="scalable save" type="button"><span>submit my form</span></button>
</td>
</tr>
</table>
</div>
<div class="entry-edit">
<form id="edit_form" name="edit_form" method="post" action="<?php echo $this->geturl('*/*/post')?>">
<h4 class="icon-head head-edit-form fieldset-legend"><?php echo $this->__('this fieldset name')?></h4>
<fieldset id="my-fieldset">
<table cellspacing="0" class="form-list">
<tr>
<td class="label"><?php echo $this->__('field label')?> <span class="required">*</span></td>
<td class="input-ele"><input class="input-text required-entry" name="myform[myfield]" /></td>
</tr>
</table>
</fieldset>
</form>
<script type="text/javascript">
var editform = new varienform('edit_form');
</script>