joomla mvc架構, 原理

component基本結構與訪問方式

如訪問:http://example.com/index.php?option=com_helloworldphp

Joomla將定位到:/components/com_helloworld,而且加載裏面的helloworld.phphtml

helloworld.php的工做就是加載相應的controller,繼而調用相應的viewjson

含view, task的訪問方式

如訪問:http://domain.com/index.php?option=com_helloworld&view=helloworld&task=display&layout=defaultapp

joomla將定位到:看下面標紅的,實際上是先定位到helloworld.php,而後調實例化HelloWorldController,並調用指定的task方法,此處爲display,在display裏再去實例化HelloWorldViewHelloWorld類,繼而調用其display方法。因此此處能夠說直接定位到components/com_helloworld/views/helloworld/view.html.phpdom

注意這裏的view和task不是必須的,但按如下默認規則:函數

task    默認值:display
this

           經過HelloWorldController裏display方法調用到view.html.php裏display方法.spa

view   默認值:component's namecode

           此處component名稱爲helloworld,故默認的view helloworld將被調用component

layout 默認值:default

           layout指調用相應view下哪一個模板,此處爲components/com_helloworld/views/helloworld/tmpl/default.php

           如layout指定值,則調用相應的<layout>.php

           注意:要使用layout參數,在view類裏必須調用 'parent::display($tpl);'此處$tpl極值 

上圖說明:

HelloWorldController爲空類,但其繼承了JControllerLegacy,其父類裏有display方法,裏面將實例化給定view,並調用view下display方法,見下

$view->display();

相應view的獲取方式爲:components/com_helloworld/views/helloworld/view.html.php

default.php裏$this指HelloWorldViewHelloWorld對象,故$this->msg可直接使用


返回json,xml等格式

joomla還有個format參數,默認值是html,將調用view.html.php來輸出,如想返回json,xml等,則增長如下php

舉個返回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

返回

component裏model訪問方式

MVC結構還少了M,此處給出解釋

model定義在components/com_helloworld/models文件夾裏,名稱爲helloworld.php

model類名爲HelloWorldModelHelloWorld,裏面方法名都是setget打頭的。

在view裏經過$this->get('組件名')調用相應的model下的代碼,見下

可見,調用model的方式

view view調用
$this->get('Msg');
model ...接下來model裏對應的function將會被調用
getMsg()



總結下:

如下綠色是固定的,紅色是變化的

<Name>表示組件名

controller

controller的命名方式:<Name>Controller

class <Name>Controller extends JControllerLegacy{}

controller的調用方式:

$controller = JControllerLegacy::getInstance('<Name>');
$controller->execute(JFactory::getApplication()->input->getCmd('task'));
$controller->redirect();

view

view不指定的話即爲組件名,即爲<Name>

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); //此處指定$tpl的話,則調用相應的tmpl下<$tpl>.php,默認值爲default
  }}

model

model的命名方式:<Name>Model<Modelname>

class <Name>Model<Viewname> extends JModelLegacy{}

全部get開頭的函數在view裏的調用方式爲$this->get('xxx');

相關文章
相關標籤/搜索