在这一节中我们将要写一个空的组件,要求这个组件能够通过后台安装程序来进行安装。要完成这一任务并不很难,只需要编写很少的代码就行了。
在joomla开发中,命名是一个很重要的部分。将组件起名为guestBook.
文件夹结构
下面是组件的文件夹结构:
guestbook
guestbook.xml
guestbook.php
(注:红色为文件夹)
一个最简单的组件,只需要这两个文件就可以了。
guestbook.xml的内容:
<?xml version="1.0" encoding="utf-8"?> <extension type="component" version="2.5.0" method="upgrade"> <name>guestbook</name> <!-- The following elements are optional and free of formatting constraints --> <creationDate>2013-9-26</creationDate> <author>min.zhang</author> <authorEmail>zhang19min88AT163.com</authorEmail> <authorUrl>http://www.zmax99.com</authorUrl> <copyright>All right @zmax</copyright> <license>License</license> <!-- The version string is recorded in the components table --> <version>0.0.1</version> <!-- The description is optional and defaults to the name --> <description>这是一个留言板组件</description> <files> <filename>guestbook.php</filename> </files> <administration> <menu>留言本</menu> </administration> </extension>
guestbook.php的内容为空。
这样一个空的组件就完成了。我们将他打包,注意要.zip格式的。然后在后台安装。下面是2.5下的安装截图:
说明我们刚刚写的组件成功安装了。并且还显示了一句话:“这是一个留言板组件”。
在 整个过程中我们只写了两个文件,一个是guestbook.php,一个是guestbook.xml.其中guestbook.php是一个空的文件。 (更准确的说,这个guestbook.php文件我们也可以不要,只是等下要用到这个文件,所以才加上)。我们重点要研究的就是 guestbook.xml文件。
首先谈一下这个文件的名称。在开始的时候就已经提到了joomla中,命名是十分重要的。guestbook.xml这个名称是不能变的。我们将这个文件称为自述文件。它的名字是固定的,一定要是组件的名称加上.xml。
其次,就是来看看这个文件干了写什么?在joomla中这个文件的格式是固定的。他主要是告诉安装程序有关这个组件的一些信息。比如说是谁开发的,要包含哪些文件等等。我们现在需要关注的地方有三点:
1,<name>guestbook</name> 这个地方说明了这个组件的名称
2,<description>这是一个留言板组件</description> 这个地方就是安装成功后显示的那一句话
3 , <filename>guestbook.php</filename> 这个地方就是告诉安装程序需要将 guestbook.php这个文件拷贝到组件的更目录下。如果要拷贝一个文件夹 用<folder>folderName</folder>来指定。
一个组件安装成功,系统到底做了写什么呢?(这个问题我们在模块开发中讨论过)
- 在components目录下新建一个文件夹叫做com_guestbook.并且拷贝我们要求的文件到该文件夹下。
- 在数据库表#__e
xtensions中插入一条记录并且初始化一些值。比如说是否发布。
到此,这一节就结束了。
小结
我们在这一节中成功新建了一个组件,并且成功安装。
- 记住,在joomla中文件的命名是十分重要的。自述文件的名称必须是组件的名称加上.xml。
- 一个空的组件最简单只需要一个文件就行了。
- 自述文件告诉安装程序需要拷贝什么文件到组建的根目录
- 一个组件的成功安装,意味着系统会默默的完成两件事。
于2014-02-23 更正:
XML文件中的<name>guestbook</name> 中,的确这个字段说明了组件的名称,但是这个名称是不能随便起的,应为joomla系统将会用这个名称来建立组件的文件夹。joomla安装程序会首先获 得xml文件中name属性的值,然后将这个名称全部变为小写,再去掉不被识别的字符串(去掉空格,去掉!@#¥等等字符),以此来构成组件的目录名。所 以,如果你在这个地方用一个中文名称,那么你的组件执行后肯定会提示找不到组件。
于2014-03-01添加:
XML文件有许多的作用,上面只是一个最简单的XML. 当需要参数的时候,你可以在XML中进行添加,下面摘录一个比较复杂的XML文件:在这个XML文件中我们给菜菜单增加了一个自定义的图片
<?xml version="1.0" encoding="utf-8"?> <extension type="component" version="2.5.0" method="upgrade"> <name>Hello World!</name> <!-- The following elements are optional and free formating constrains--> <creationDate>2014-02-20</creationDate> <author>min.zhang</author> <authorEmail>zhang19min88AT163.com</authorEmail> <authorUrl>http://www.zmax99.com</authorUrl> <copyright>All right reserved!</copyright> <license>ZMAX99</license> <!-- The version string is recorded in the components table--> <version>0.1.3</version> <!--The description is optional and defaults to the name --> <description>COM_HELLOWORLD_DESCRIPTION</description> <install><!-- Runs on install --> <sql> <file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file> </sql> </install> <uninstall> <sql> <file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file> </sql> </uninstall> <update><!-- Runs on update; new in 2.5--> <schemas> <schemapath type="mysql">sql/updates/mysql</schemapath> </schemas> </update> <!-- Site Main File Copy Section--> <!-- Note the folder attribute: This attribute describes the folder to copy FROM in the package to install therfore files copied in this section are copied from /site/in the package --> <files folder="site"> <filename>index.html</filename> <filename>helloworld.php</filename> <filename>controller.php</filename> <folder>views</folder> <folder>models</folder> <folder>language</folder> </files> <media destination="com_helloworld" folder="media"> <filename>index.html</filename> <folder>images</folder> </media> <administration> <!-- Administration Menu Section--> <menu img="../media/com_helloworld/images/joomla16.jpg">COM_HELLOWORLD_MENU</menu> <!-- Administration Main File Copy Section --> <!-- Note the folder attribute : This attribute describes the folder to copy FROM in this section are copied from /admin/in the package --> <files folder="admin"> <filename>index.html</filename> <filename>access.xml</filename> <filename>config.xml</filename> <filename>helloworld.php</filename> <filename>controller.php</filename> <folder>models</folder> <folder>views</folder> <folder>controllers</folder> <folder>tables</folder> <folder>language</folder> <folder>helpers</folder> <!-- SQL files section--> <folder>sql</folder> </files> </administration> </extension>
评论 (0)