1.7 HelloWorld adding views

Source: Internet
Author: User

Model: Managing Data

Controller: Perform tasks, set or get the state of the model, request view display

View: Shows what is selected by the control

(1) Setting the controller

Jcontroller is a class that manages the controller and adds the following code to site/helloworld.php.

<?php//  No Direct access to this filedefineddie (' Restricted access '  // Import Joomla Controller Libraryjimport (' 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 ControlleR$controller->redirect ();
Jcontrollerlegacy::getinstance (' HelloWorld ');(Note that the 3.x version uses Jcontrollerlegacy if Jcontroller is used on the crossing web)
Create a controller class named Helloworldcontroller, and Joomla will look for the declaration of this class in controller.php.
So write the following code in the site/controller.php file:
 
 
 <? PHP  //  No direct access to the This file  Span style= "COLOR: #008080" >defined  (' _jexec ') or die  (' Restricted access '  //  import JOOMLA Controller library  Jimport (' Joomla.application.component.controller '  /*  * * Hello World Component Controller  */ class  Helloworldcontroller extends  jcontrollerlegacy (note 3. X to inherit jcontrollerlegacy) {}  
when you do not specify a value for a task, the default task is executed, and the default task task is display, So create the display method in Helloworldcontroller.
(2) Setting the view
When Jcontroller wants to show a view, he will find it from the directory component/com_helloworld/views/helloworld/directory
site/views/helloworld/view.html.php
<?PHP//No Direct Access to the This filedefined(' _jexec ') or die(' Restricted access '); //Import Joomla View libraryJimport (' Joomla.application.component.view '); /** * HTML View class for the HelloWorld Component*/classHelloworldviewhelloworld Extendsjviewlegacy (Note 3.x to inherit jviewlegacy) {    //overwriting JView Display method    functionDisplay$TPL=NULL){    //Assign Data to the view    $this->msg= ' Hello World '; //Display The ViewParent::d Isplay ($TPL);}}
The display method of the JView class is called by the task method of the Jcontroller class, in which case the display method displays the tmpl/default.php file.
site/views/helloworld/tmpl/default.php
<? PHP // No Direct Access to the This file defined  die (' Restricted access ');? >$this->msg;? >This template file will be included in the JView class, so $this refers to the Helloworldviewhelloworld class.
Helloworld.xml
<?xmlversion= "1.0" encoding= "Utf-8 "?><extensiontype= "Component"version= "2.5.0"Method= "Upgrade"><name>Hello world!</name><!--The following elements is optional and free of formatting constraints -<CreationDate>November 2009</CreationDate><author>John Doe</author><Authoremail>[Email protected]</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 the "To install therefore files" Copied in this section is 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 ther Efore files copied in this section is 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>






1.7 HelloWorld adding views

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.