《2022年Joomla.MVC组件开发教程 .pdf》由会员分享,可在线阅读,更多相关《2022年Joomla.MVC组件开发教程 .pdf(11页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、本节标题:添加类Joomla 框架已经实现了对所有组件分类的使用(has implemented the use of categories for all components),给一个组件添加分类会变得相当简单。修改 sql 为了管理分类,我们需要修改sql 表。.修改以下文件:admin/sql/install.mysql.utf8.sqlDROP TABLE IF EXISTS#_helloworld;CREATE TABLE#_helloworld(id int(11)NOT NULL AUTO_INCREMENT,greeting varchar(25)NOT NULL,catid
2、 int(11)NOT NULL DEFAULT 0,PRIMARY KEY (id)ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;INSERT INTO#_helloworld(greeting)VALUES(Hello World!),(Good bye World!);admin/sql/updates/mysql/0.0.12.sqlALTER TABLE#_helloworld ADD catid int(11)NOT NULL DEFAULT 0 修改表单现在每条 HelloWorld 信息将属于一个分类。我们需要修改它的编
3、辑表单,根据以前的学习,我们知道只需要修改如下xml 文件就可以实现了。admin/models/forms/helloworld.xml 名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 11 页 -JOPTION_SELECT_CATEGORY 注意,分类可以为0(代表未分类)。名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 11 页 -修改菜单样式HelloWorld 的当前菜单样式显示一个所有信息的列表,当信息分类以后,我们需要修改这个显示方式以显示分类。修改admin/models/fields/helloworld.php如下:admin/models/f
4、ields/helloworld.phpgetQuery(true);$query-select(#_helloworld.id as id,greeting,#_categories.title as category,catid);$query-from(#_helloworld);名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 11 页 -$query-leftJoin(#_categories on catid=#_categories.id);$db-setQuery(string)$query);$messages=$db-loadObjectList();$opti
5、ons=array();if($messages)foreach($messages as$message)$options=JHtml:_(select.option,$message-id,$message-greeting.($message-catid?(.$message-category.):);$options=array_merge(parent:getOptions(),$options);return$options;现在将在元括弧中间显示分类。It will now display the category between parenthesis.管理子菜单com_cat
6、egories组件允许通过 helper 文件设置子菜单,创建文件如下:admin/helpers/helloworld.phpaddStyleDeclaration(.icon-48-helloworld .background-image:url(./media/com_helloworld/images/tux-48x48.png););if($submenu=categories)$document-setTitle(JText:_(COM_HELLOWORLD_ADMINISTRATION_CATEGORIES);com_categories组件将自动调用该函数注意它将修改子菜单修改
7、一些 css 属性(以便显示图标)在子菜单是categories 时将修改浏览器标题修改标题并添加一个选择“分类”的按钮现在我们需要修改基本控制器来调用这个函数,并且需要修改(注意.icon-48-helloworld css 样式在 addSubmenu函数中已经设置了)admin/controller.php名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 11 页 -?php/No direct access to this file defined(_JEXEC)or die(Restricted access);/import Joomla controller libr
8、ary jimport(ponent.controller);/*General Controller of HelloWorld component */class HelloWorldController extends JController /*display task *return void */function display($cachable=false)/set default view if not set JRequest:setVar(view,JRequest:getCmd(view,HelloWorlds);/call parent behavior parent
9、:display($cachable);/Set the submenu HelloWorldHelper:addSubmenu(messages);admin/helloworld.phpexecute(JRequest:getCmd(task);/Redirect if set by the controller$controller-redirect();添加一些翻译字句admin/language/en-GB/en-GB.com_helloworld.ini(使用中文的记得在zh-CN,中)COM_HELLOWORLD=Hello World!COM_HELLOWORLD_ADMINI
10、STRATION=HelloWorld-Administration COM_HELLOWORLD_ADMINISTRATION_CATEGORIES=HelloWorld-Categories COM_HELLOWORLD_HELLOWORLD_CREATING=HelloWorld-Creating COM_HELLOWORLD_HELLOWORLD_DETAILS=Details COM_HELLOWORLD_HELLOWORLD_EDITING=HelloWorld-Editing COM_HELLOWORLD_HELLOWORLD_ERROR_UNACCEPTABLE=Some va
11、lues are unacceptable COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_DESC=The category the messages belongs to COM_HELLOWORLD_HELLOWORLD_FIELD_CATID_LABEL=Category COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC=This message will be displayed COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL=Message COM_HELLOWORL
12、D_HELLOWORLD_HEADING_GREETING=Greeting COM_HELLOWORLD_HELLOWORLD_HEADING_ID=Id COM_HELLOWORLD_MANAGER_HELLOWORLD_EDIT=HelloWorld manager:Edit Message COM_HELLOWORLD_MANAGER_HELLOWORLD_NEW=HelloWorld manager:New Message COM_HELLOWORLD_MANAGER_HELLOWORLDS=HelloWorld manager COM_HELLOWORLD_N_ITEMS_DELE
13、TED_1=One message deleted COM_HELLOWORLD_N_ITEMS_DELETED_MORE=%d messages deleted COM_HELLOWORLD_SUBMENU_MESSAGES=Messages COM_HELLOWORLD_SUBMENU_CATEGORIES=Categories 名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 11 页 -再次打包升级文件包含:helloworld.xmlsite/index.htmlsite/helloworld.phpsite/controller.phpsite/views/index.
14、htmlsite/views/helloworld/index.htmlsite/views/helloworld/view.html.phpsite/views/helloworld/tmpl/index.htmlsite/views/helloworld/tmpl/default.xmlsite/views/helloworld/tmpl/default.phpsite/models/index.htmlsite/models/helloworld.phpsite/language/index.htmlsite/language/en-GB/index.htmlsite/language/
15、en-GB/en-GB.com_helloworld.iniadmin/index.htmladmin/helloworld.phpadmin/controller.phpadmin/sql/index.htmladmin/sql/install.mysql.utf8.sqladmin/sql/uninstall.mysql.utf8.sqladmin/sql/updates/index.htmladmin/sql/updates/mysql/index.htmladmin/sql/updates/mysql/0.0.1.sqladmin/sql/updates/mysql/0.0.6.sql
16、admin/sql/updates/mysql/0.0.12.sqladmin/models/index.htmladmin/models/fields/index.htmladmin/models/fields/helloworld.phpadmin/models/forms/index.htmladmin/models/forms/helloworld.xmladmin/models/forms/helloworld.jsadmin/models/rules/index.htmladmin/models/rules/greeting.phpadmin/models/helloworld.p
17、hpadmin/models/helloworlds.phpadmin/views/index.htmladmin/views/helloworlds/index.htmladmin/views/helloworlds/view.html.phpadmin/views/helloworlds/tmpl/index.html名师资料总结-精品资料欢迎下载-名师精心整理-第 8 页,共 11 页 -admin/views/helloworlds/tmpl/default.phpadmin/views/helloworlds/tmpl/default_head.phpadmin/views/hell
18、oworlds/tmpl/default_body.phpadmin/views/helloworlds/tmpl/default_foot.phpadmin/views/helloworlds/index.htmladmin/views/helloworld/view.html.phpadmin/views/helloworld/submitbutton.jsadmin/views/helloworld/tmpl/index.htmladmin/views/helloworld/tmpl/edit.phpadmin/helpers/index.htmladmin/helpers/hellow
19、orld.phpadmin/tables/index.htmladmin/tables/helloworld.phpadmin/language/en-GB/en-GB.com_helloworld.iniadmin/language/en-GB/en-GB.com_helloworld.menu.iniadmin/controllers/index.htmladmin/controllers/helloworld.phpadmin/controllers/helloworldlist.phplanguage/en-GB/en-GB.inimedia/index.htmlmedia/image
20、s/index.htmlmedia/images/tux-16x16.pngmedia/images/tux-48x48.pnghelloworld.xml 文件如下 COM_HELLOWORLD November 2009 John Doe john.doeexample.org http:/www.example.org Copyright Info License Info 0.0.12 名师资料总结-精品资料欢迎下载-名师精心整理-第 9 页,共 11 页 -COM_HELLOWORLD_DESCRIPTION sql/install.mysql.utf8.sql sql/uninst
21、all.mysql.utf8.sql sql/updates/mysql index.html helloworld.php controller.php views models language index.html images 名师资料总结-精品资料欢迎下载-名师精心整理-第 10 页,共 11 页 -COM_HELLOWORLD_MENU index.html helloworld.php controller.php sql tables models views controllers helpers language/en-GB/en-GB.com_helloworld.ini language/en-GB/en-GB.com_helloworld.sys.ini 名师资料总结-精品资料欢迎下载-名师精心整理-第 11 页,共 11 页 -