Magento版本的MVC

Magento Alan 10年前 (2014-06-25) 4270次浏览 0个评论 扫描二维码

如果您熟悉传统的CakePHP或Symfony这些MVC模型框架的话,可能会了解到通用的MVC称为基于惯例(convention-based)的MVC。在这种MVC框架下,添加现代战争模型或控制器等只需按框架标准创建文件(类),系统可以自动识别。而Magento使用的则是基于配置(configuration-based)的MVC,对于这种情况,除了创建文件(类)外,还需要告诉Magento我们添加了一个新类。
每个Magento模块在etc目录下都有一个config.xml文件,该文件中包含所有的模块配置内容。例如,假设我们想象添加一个包含新模型的新模块,我们需要在配置文件中定义一个节点来告诉Magento到哪里去查找我们的模型,举例如下:

<global>

	…

	<models>

		<group_classname>

			<class>Namespace_Modulename_Model</class>

		<group_classname>

	</models>

	...

</global>

尽管这看起来有些画蛇添足,但这也留给我们更多的灵活性和更大的操作空间。比如我们想要通过rewrite节点来重写另一个类:

<global>

	…

	<models>

		<group_classname>

			<rewrite>

				<modulename>Namespace_Modulename_Model</modulename>

			</rewrite>

		<group_classname>

	</models>

	...

</global>

Magento会加载所有的config.xml文件并在运行时合并这些文件,创建一个单独的配置树(configuration tree)。
此外,模块还可带有一个system.xml文件,用于指定Magento后台的配置选项,让终端用户可以用来配置模块的功能,system.xml文件示例如下:

<config>

	<sections>

		<section_name translate="label">

			<label>Section Description</label>

			<tab>general</tab>

			<frontend_type>text</frontend_type>

			<sort_order>1000</sort_order>

			<show_in_default>1</show_in_default>

			<show_in_website>1</show_in_website>

			<show_in_store>1</show_in_store>

			<groups>

				<group_name translate="label">

					<label>Demo Of Config Fields</label>

					<frontend_type>text</frontend_type>

					<sort_order>1</sort_order>

					<show_in_default>1</show_in_default>

					<show_in_website>1</show_in_website>

					<show_in_store>1</show_in_store>

					<fields>

						<field_name translate="label comment">

							<label>Enabled</label>

							<comment>

								<![CDATA[Comments can contain

								<strong>HTML</strong>]]>

							</comment>

							<frontend_type>select</frontend_type>

							<source_model>adminhtml/system_config_source_yesno</

							source_model>

							<sort_order>10</sort_order>

							<show_in_default>1</show_in_default>

							<show_in_website>1</show_in_website>

							<show_in_store>1</show_in_store>

						</field_name>

					</fields>

				</group_name>

			</groups>

		</section_name>

	</sections>

</config>

下面让我们来了解各个节点的作用:

  • section_name: 这个随机名称用于确定配置区块(section),在这一节点内我们需指定该配置区块所有的字段(field)和组(group)。
  • group: 顾名思义,group用于对配置选项进行分组,并在一个折叠区块内进行显示。
  • label: 用于定义字段、区块或组所使用的标题或标签。
  • tab: 用于定义在哪个区块显示该标签。
  • frontend_type: 这个节点允许我们指定在自定义选项中使用哪些选项:
    • button
    • checkboxes
    • checkbox
    • date
    • file
    • hidden
    • image
    • label
    • link
    • multiline
    • multiselect
    • password
    • radio
    • radios
    • select
    • submit
    • textarea
    • text
    • time
  • sort_order: 指定字段、组或者区块的位置。
  • source_model: 某些字段如select字段可以从源模型中获取选项,Magento已经提供了在Mage/Adminhtml/Model/System/Config/Source下提供了几个类供使用,例如:
    • YesNo
    • Country
    • Currency
    • AllRegions
    • Category
    • Language

通过使用XML文件,我们可以在Magento后台中为我们的模块创建复杂的配置选项,而无需担心要在模板中添加字段或验证数据。Magento还提供了一个全面的表单字段验证模型,只需使用<validate>标签,有如下验证字段:

  • validate-email
  • validate-length
  • validate-url
  • validate-select
  • validate-password

Magento的所有其它部分我们可以继承source_model, frontend_type和validator方法,甚至创建一个新的方法。后面我们会讲解。但现在我们还是先来了解模型、视图、文件布局和控制器的相关概念吧。

喜欢 (0)
[]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址