1、创建模块
需要声明模块的名称和版本信息,并进行激活
需要配置两个配置文件:
a、模块配置文件config.xml
b、激活配置文件 <package_name>_<module_name>.xml
如果包里面有多个模块,可以将多个模块激活配置文件放置在一个xml文件中,取名:<package_name>_All.xml

示例:
创建一个包名:Aosom,创建一个模块名:Testhello
则创建的配置文件如下
a、app/code/local/Aosom/Testhello/etc/config.xml

<config>
    <modules>
        <Aosom_Testhello>
            <version>0.1.0</version>
        </Aosom_Testhello>
    </modules>
</config>

b、app/etc/modules/Aosom_Testhello.xml

<config>
    <modules>
        <Aosom_Testhello>
            <active>true</active>
            <codePool>local</codePool>                
        </Aosom_Testhello>
    </modules>
</config>

配置文件说明:
<config>为配置文件的根节点
<modules>为配置文件的二级节点—指明某个模块的基本信息
最基本的定义一个模块的名字,版本和是否依赖于其他模块
模块名字一般是<packagename_modulename>构成
所以如果是app/code/local/Aosom/Testhello模块,
则模块名称为Aosom_Testhello

modules配置结构:

  <modules>
     <(NameSpace_ModuleName)>
       <active>[true|false]</active>
       <codePool>[core|community|local]</codePool>
       <depends>
         <(AnotherNameSpace_ModuleName) />
       </depends>
       <version>(version_number)</version>
     </(NameSpace_ModuleName>
  </modules>

2、前端控制器
如果创建了前端控制器,并且需要进行路由访问,则需要对config.xml进行路由配置
示例:
创建app/code/local/Aosom/Testhello/IndexController.php
路由配置:

<frontend>
    <routers>
        <testhello>
            <use>standard</use>
            <args>
                <module>Aosom_Testhello</module>
                <frontName>testhello</frontName>
            </args>
        <testhello>
    </routers>
</frontend>

配置说明:
<frontend>节点也是一个二级节点,他包含以下子节点:

<frontend>
    <routers></routers>
    <events></events>
    <translate></translate>
    <layout></layout>
</frontend>

我们这里只进行路由配置,所以使用了<routers>节点
<routers>节点的基本结构如下:

<routers>
    <(modulename)>
        <use>[standard|admin|default]</use>
        <args>
            <module>(NameSpace_ModuleName)</module>
            <frontName>(frontname)</frontName>
        </args>
    </(modulename)>
</routers>

一般<{modulename}>节点的名字和<frontName>节点中的值一致
<use>节点指定使用权限,一般后台用admin

进行了以上配置以后,我们就可以通过浏览器来访问了
假设入口地址为:http://magetest.cn/magento/index.php
则上面定义的路由访问路径为:
http://magetest.cn/magento/index.php/testhello/index/index

访问的是<frontName>为testhello对应的模块底下的IndexController控制器中的indexAction方法

3、布局、块、模板文件
以上讲解了关于路由配置文件的编写,接下去,如果在控制器中要显示模板文件的HTML内容,则需要对布局xml文件进行配置
示例:
在控制器indexController的indexAction方法中,输出布局

$this->loadLayout();
$this->renderLayout();

当我们没有进行任何布局配置时,系统会输出默认的页面,接下去我们来配置布局xml文件
a、布局xml配置文件位置:
自定义的配置文件,需要放置在后台定义的默认设计主题下,假设我们后台定义的主题是rwd
则放置位置为:app/design/frontend/rwd/default/layout/下
我们将文件名设置为:local.xml

<layout version="0.1.0">
    <testhello_index_index>
        <block type="page/html" name="root" output="toHtml" template="testhello/test.phtml"/>
    </testhello_index_index>
</layout>

配置文件说明:
<layout>节点是布局配置文件的根节点
节点:testhello_index_index是frontName+控制器名称+方法名称组成,如果该节点写成default,则所有页面都会输出test.phtml模板内容
block节点表示block对应的类和对应的模板名称。
block中的type,这个属性其实是一个URI

<block type="page/html" ...
<block type="page/template_links" ...

Magento就是通过这个URI用来查找block对应的类名。
这个URI分为两部分:
第一部分“page”是用来在全局配置中查找一个基本类名
第二部分“html”或者“template_link”将被添加到基本类名后面生成一个具体的将被实例化的类名。

如果需要编写Block类,并且在布局xml中会使用,则在编写完毕以后,要在模块配置文件config.xml中进行声明
示例:
在app/code/local/Aosom/Testhello/Blocks/Test.php
对应的Block类名为:Aosom_Testhello_Blocks_Test
所有自定义的前台Block都继承于Mage_Core_Block_Template类。
所有自定义的后台Block都继承于Mage_Adminhtml_Block_Template类。
Block中编写一个简单的方法:

class Aosom_Testhello_Block_Test extends Mage_Core_Block_Template
{
    public function getList(){
        $arr = [1,2,3,4];
        return $arr;
    }
}

接下来,可以在布局xml定义一个简单的布局输出

<layout version="0.1.0">
    <testhello_index_index>
        <block type="testhello/test" name="root" output="toHtml" template="testhello/test.phtml"/>
    </testhello_index_index>
</layout>

这里面唯一要注意的就是type=”testhello/test”,这个声明如上所述是一个URI,用来确定对应的Block类的。
其中testhello是Block类的前半部分(组名),这个组名我们目前没有定义,所以需要在模块配置文件config.xml中进行定义
在config.xml文件中增加:

<global>
    <blocks>
        <testhello>
            <class>Aosom_Testhello_Block</class>
        </testhello>
    </blocks>
</global>

配置说明:
<global>节点是二级节点,和<modules><frontend>节点同级,都是属于<config>下面的子节点
<global>节点包含以下子节点:
 

 <global>
    <models></models>
    <resources></resources>
    <blocks></blocks>
    <helpers></helpers>
    <fieldsets></fieldsets>
    <template></template>
    <events></events>
    <eav_attributes></eav_attributes>
    <(modulename)>!-- custom config variables --></(modulename)>
  </global>

在节点<blocks>定义的<testhello>节点,就是我们在layout布局xml中写type时的URI前半部分,
magento系统会根据布局xml中的testhello到全局配置文件中查找blocks中的testhello,找到以后
会根据里面指定的class,查找到具体要使用的Block的全类名。
上面定义会查找到的类名是Aosom_Testhello_Block_Test。

一般一个Block会对应一个phtml文件,上面定义的Block对应的phtml文件时test.phtml,这时我们可以在phtml文件中
通过$this来引用Block对应的方法。
如果在phtml文件中要使用定义的getList方法,则可以如下使用:

<body>
    测试Block方法输出
<?php
    $list = $this->getList();
    foreach($list as $v){
        echo $v . '<br/>';
    }
?>
</body>

接下去访问对应的控制器,可以看到相应输出:
http://magetest.cn/magento/index.php/testhello/index/index

获取某项配置的值:
store>getConfig( <script type="math/tex" id="MathJax-Element-1">store->getConfig(</script>path) 或者 Mage::getStoreConfig( path[, <script type="math/tex" id="MathJax-Element-2">path [, </script>store]);
$store 这个变量可以是 store 的 code或ID

获取某项配置是否不为空,返回值为 true / false:
Mage::getStoreConfigFlag( path[, <script type="math/tex" id="MathJax-Element-3">path [, </script>store]);
$store 这个变量可以是 store 的 code或ID

获取某个配置项的整个节点(Simple_Xml):
Mage::getConfig()->getNode( path[, <script type="math/tex" id="MathJax-Element-4">path [, </script>scope]);
$scope 通常是指一个范围,website或store

Logo

小龙虾开发者社区是 CSDN 旗下专注 OpenClaw 生态的官方阵地,聚焦技能开发、插件实践与部署教程,为开发者提供可直接落地的方案、工具与交流平台,助力高效构建与落地 AI 应用

更多推荐