(1) The model is used to manage the data, and he returns the data to the caller, so the first function that must be implemented is get.
In this example the caller is Helloworldviewhelloworld.
site/models/helloworld.php
<?PHP//No Direct Access to the This filedefined(' _jexec ') or die(' Restricted access '); //Import Joomla ModelItem libraryJimport (' Joomla.application.component.modelitem '); /** * HelloWorld Model*/classHelloworldmodelhelloworldextendsjmodelitem{/** * @var string msg*/ protected $msg; /** Get The message * @return string The message to being displayed to the user*/ Public functiongetmsg () {if(!isset($this-msg)) { $this->msg = ' Hello world! '; } return $this-msg; }}
(2) The Helloworldviewhelloworld class obtains data through the Get method
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*/classHelloworldviewhelloworldextendsJView (jviewlegacy) {//overwriting JView Display method functionDisplay$TPL=NULL) { //Assign Data to the view $this->msg =$this->get (' MSG ')); //Check for errors. if(Count($errors=$this->get (' Errors ')) {Jlog:: Add (implode(' <br/> ',$errors), jlog::warning, ' Jerror '); return false; } //Display The ViewParent::d Isplay ($TPL); }}
$this->get () is JView:: Get (Jviewlegacy::get), which is a delegate that gets the corresponding method in the default model class.
(3) Modify Helloworld.xml indicates that the model and view are used and the version number is modified
<?XML version= "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.4</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 therefor e 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> <folder>Models</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>
Finally packaged and installed.
1.9 Hello World Add model