标签:
如访问:http://example.com/index.php?option=com_helloworld
Joomla将定位到:/components/com_helloworld,并且加载里面的helloworld.php
helloworld.php的工作就是加载相应的controller,继而调用相应的view
如访问:http://domain.com/index.php?option=com_helloworld&view=helloworld&task=display
注意这里的view和task不是必须的,但按以下默认规则:
task 默认值:display
通过HelloWorldController里display方法调用到view.html.php里display方法.
view 默认值:component‘s name
此处component名称为helloworld,故默认的view helloworld将被调用
上图说明:
HelloWorldController为空类,但其继承了JControllerLegacy,其父类里有display方法,其指到view,见下
$view->display();
相应view的获取方式为:components/com_helloworld/views/helloworld/view.html.php
default.php里$this指HelloWorldViewHelloWorld对象,故$this->msg可直接使用
默认情况下是返回html,是由view.html.php来输出的,如想返回json,xml等,则增加以下php
通过参数format来控制返回类型
举个返回xml的例子 view.xml.php:
<?php // No direct access to this file defined(‘_JEXEC‘) or die(‘Restricted access‘); // import Joomla view libraryjimport(‘joomla.application.component.view‘); /** * XML View class for the HelloWorld Component */ class HelloWorldViewHelloWorld extends JViewLegacy { // Overwriting JView display method function display($tpl = null) { echo "<?xml version=‘1.0‘ encoding=‘UTF-8‘?> <article> <title>How to create a Joomla Component</title> <alias>create-component</alias> </article>"; } }
则访问
http://example.com/index.php?option=com_helloworld&format=xml
返回
MVC结构还少了M,此处给出
model定义在components/com_helloworld/models文件夹里,名称为helloworld.php
model类名为HelloWorldModelHelloWorld,里面方法名都是set,get打头的。
在view里通过$this->get(‘组件名‘)调用相应的model下的代码,见下
可见,调用model的方式
view | 当view调用 |
$this->get(‘Msg‘); |
model | ...接下来model里对应的function将会被调用 |
getMsg() |
controller
controller的命名方式<Name>Controller
class <Name>Controller extends JControllerLegacy{}
controller的调用方式
// Get an instance of the controller prefixed by <name> $controller = JControllerLegacy::getInstance(‘<Name>‘); // Perform the Request task $controller->execute(JFactory::getApplication()->input->getCmd(‘task‘)); // Redirect if set by the controller $controller->redirect();
view
view的命名方式<Name>View<Viewname>
class <Name>View<Viewname> extends JViewLegacy{ function display($tpl=null) { // Prepare the data $data1 = .... $data2 = .... $moredata[] = array.... // Inject the data $this->variablename = $data1; $this->variablename2 = $data2; $this->variablename3 = $moredata; // Call the layout template. If no tpl value is set Joomla! will look for a default.php file $tpl = ‘myTemplate‘; parent::display($tpl); }}
model
model的命名方式
访问方式:http://example.com/index.php?option=com_<name>&view=<myview>&format=ajax
标签:
原文地址:http://my.oschina.net/u/914655/blog/402027