magento基础xml配置详解

来源:互联网 发布:淘宝店铺优惠券名称 编辑:程序博客网 时间:2024/05/21 22:39

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(path) 或者 Mage::getStoreConfig(path[,store]);
$store 这个变量可以是 store 的 code或ID

获取某项配置是否不为空,返回值为 true / false:
Mage::getStoreConfigFlag(path[,store]);
$store 这个变量可以是 store 的 code或ID

获取某个配置项的整个节点(Simple_Xml):
Mage::getConfig()->getNode(path[,scope]);
$scope 通常是指一个范围,website或store

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 嫁到北京农村怎么办居住证 2020年没脱贫的农民怎么办 2020年农民的土地怎么办 车停在停车场被划怎么办 专升本差两分怎么办 入职需要学士学位证怎么办 不喜欢写科研项目又没编制怎么办 易学堂密码忘了怎么办 易班手机号换了怎么办 易班登录不上怎么办 易到手机号换了怎么办 海外留学没有教育部认证怎么办 七过月宝宝便秘怎么办 6个月孩子便秘怎么办 6个月婴儿便秘怎么办 一个多月宝宝两天没拉大便怎么办 7个月宝宝便秘怎么办 9个月宝宝便秘怎么办 六个月宝宝严重便秘怎么办 公司调岗员工不同意怎么办 acca注册一直在审核怎么办 ieee ap二审被拒怎么办 文章投到假期刊怎么办 论文投了假网站怎么办 网上传了虚假的怎么办? 网上做兼职被骗了钱怎么办 通过支付宝扫码被骗了怎么办 支付宝扫二维码被骗怎么办 在is上被骗了怎么办 微信兼职被骗怎么办天涯论坛 公众号不给稿费怎么办 围绕服务改善民生提出怎么办 翰墨香林苑怎么办全产权 西城高铁联名卡怎么办 英语教学系统做题时超时了怎么办 sci被要求撤稿怎么办 论文投稿后初审通过想撤稿怎么办 河南大学读写译挂了怎么办 在职mba双证硕士怎么办户口 电脑开机了出现一些英文单词怎么办 不懂法语想读法语书怎么办