1.7 HelloWorld 添加视图

模型: 管理数据

控制器:执行任务,设置或者获取模型的状态,请求视图显示

视图:显示被控件选中的内容

(1)Setting the controller

JController是一个管理控制器的类, 在site/helloworld.php添加如下代码。

<?php// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import joomla controller library
jimport('joomla.application.component.controller');
 
// Get an instance of the controller prefixed by HelloWorld
$controller= JControllerLegacy::getInstance('HelloWorld');
 
// Perform the Request task
$input= JFactory::getApplication()->input;
$controller->execute($input->getCmd('task')); 

// Redirect if set by the controlle
r$controller->redirect();
JControllerLegacy::getInstance('HelloWorld');(注意3.x版本使用的是JControllerLegacy,如果看官网上使用的是JController)
创建一个名为HelloWorldController的控制器类,Joomla将会在controller.php中查找这个类的声明。
 
所以在site/controller.php文件中写入下面代码:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla controller library
jimport('joomla.application.component.controller');
 
/**
 * Hello World Component Controller
 */
class HelloWorldController extends JControllerLegacy (注意3.x要继承JControllerLegacy)
{}
当没有指定task的值时,默认task将会被执行,默认task任务是display,所以在HelloWorldController中创建display方法。
(2)Setting the view
当JController想去展示一个view的时候,他将会从目录component/com_helloworld/views/helloworld/目录下查找
site/views/helloworld/view.html.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla view library
jimport('joomla.application.component.view');
 
/**
 * HTML View class for the HelloWorld Component
 */
class HelloWorldViewHelloWorld  extendsJViewLegacy(注意3.x要继承JViewLegacy){
    // Overwriting JView display method
    function display($tpl=null){
    // Assign data to the view
    $this->msg='Hello World';
 
    // Display the view
    parent::display($tpl);
}}
JView类的display方法会被JController类的task方法调用,在这个例子中display方法将会显示tmpl/default.php文件。
 
 
site/views/helloworld/tmpl/default.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
?>
<h1><?phpecho$this->msg;?></h1>
这个模板文件将会被JView类包含,所以$this指的是HelloWorldViewHelloWorld类。
 
 
helloworld.xml
<?xmlversion="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 of formatting constraints -->
<creationDate>November 2009</creationDate>
<author>John Doe</author>
<authorEmail>john.doe@example.org</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!--  The version string is recorded in the components table -->
<version>0.0.2</version>
<!-- The description is optional and defaults to the name -->
<description>Description of the Hello World component ...</description>
<update>
<!-- Runs on update; New in 2.5 -->
<schemas><schemapathtype="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 therefore files copied        in this section are copied from /site/ in the package -->
<filesfolder="site"><filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<folder>views</folder>
</files>
 
<administration>
    <!-- Administration Menu Section -->
    <menu>Hello World!</menu>
    <!-- Administration Main File Copy Section --><!-- Note the folder attribute: This attribute describes the folder            to copy FROM in the package to install therefore files copied            in this section are copied from /admin/ in the package -->
    <filesfolder="admin">
        <!-- Admin Main File Copy Section -->
        <filename>index.html</filename>
        <filename>helloworld.php</filename>
        <!-- SQL files section -->
        <folder>sql</folder>
    </files>
</administration>
 
</extension>






posted @ 2015-08-28 23:09  codergma  阅读(399)  评论(0编辑  收藏  举报