初探magento的MVC架构(Create new module “HelloWorld” – in Magento)

本文将指导您如何在Magento中创建新模块和页面,包括模块声明、配置、控制器类、块类、模板定义等步骤,最终实现显示“HelloWorld! I am a Magento Guy…”的页面。

 

Do you want to create a new page in Magento ? or Do you want to create a new module in Magento ? If yes, Then ok, just spend 10 minutes and follow below steps.

Objectives:  I will create a new page in Magento whose output will be “Hello World ! I am a Magento Guy..”.

Target:  Create a new module called “HelloWorld”

Step 1 : Module Declaration

Create app/etc/modules/M4U_HelloWorld.xml and write below code

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

Step 2: Module Configuration

a. Create a controller class app/code/local/M4U/HelloWorld/controllers/IndexController.php

class M4U_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout(array('default'));
$this->renderLayout();
}
}

b. Create a Block class app/code/local/M4U/HelloWorld/Block/HelloWorld.php

class M4U_HelloWorld_Block_HelloWorld extends Mage_Core_Block_Template
{
// necessary methods
}

c. create configuration xml in app/code/local/M4U/HelloWorld/etc/config.xml

<?xml version="1.0"?>
<config>
<global>
<modules>
<m4u_helloworld>
<version>0.1.0</version>
</m4u_helloworld>
</modules>
<blocks>
<helloworld>
<rewrite>
<helloworld>M4U_HelloWorld_Block_HelloWorld</helloworld>
</rewrite>
</helloworld>
</blocks>

</global>
<frontend>
<routers>
<helloworld>
<use>standard</use>
<args>
<module>M4U_HelloWorld</module>
<frontName>helloworld</frontName>
</args>
</helloworld>
</routers>
<layout>
<updates>
<helloworld>
<file>helloworld.xml</file>
</helloworld>
</updates>
</layout>
</frontend>
</config>

Define Frontend Template :

1. Define page layout in app/design/frontend/M4U /default/layout/helloworld.xml

N.B:  Use default  instead of M4U  as template location if you use default design packages. Means create file in app/design/frontend/default/default/layout/helloworld.xml

<?xml version="1.0"?>

<layout version="0.1.0">

<helloworld_index_index>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
<reference name="content">
<block type="helloworld/helloworld" name="hello" template="helloworld/helloworld.phtml"/>
</reference>
</helloworld_index_index>

</layout>

2. Create template file app/design/frontend/M4U /default/template/helloworld/helloworld.phtml and write down

N.B:  Use default  instead of M4U  as template location if you use default design packages. Means create file in app/design/frontend/default /default/template/helloworld/helloworld.phtml

Hello World ! I am a Magento Guy..

Hey, new module is ready to run and hit browser with url http://127.0.0.1/projectname/index.php/helloworld/

and see result.

That’s it……..

Is it easy or not ? what you think ?

Coder, Please at first try yourself.

Ohh.., Still are you facing problem ? Lets download full module source code from left panel “BOX” section or let me know.

Note: I have upgraded HelloWorld module source code of BOX widget. So it will resolve the problem which was faced by some guys.

Tagged: Block Override, Create module, create new, create new module, create new page in magento, Custom module, Magento Module, Module override

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值