Saturday, February 11, 2012

[Joomla] Hello World Component ver 5

For this version, I'll add the template in our View

First, create the xml file and name it jchelloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<install type="component" version="1.5" method="upgrade">
  <name>JC Hello World</name>
  <creationDate>February 2012</creationDate>
  <author>Joef Clarin</author>
  <version>3.0.0</version>
  <description>JC Hello World</description>
  
  <files>
    <filename>jchelloworld.php</filename>
    <filename>controller.php</filename>
    <folder>views</folder>
    <folder>models</folder>
  </files>
  
  <administration/>
</install>

Second, create the entry point file and name it jchelloworld.php
<?php
  defined('_JEXEC') or die('Restricted access');
  require_once JPATH_COMPONENT.DS.'controller.php';
  $controller = new JCHelloWorldController();
  $controller->execute(JRequest::getWord('task'));
  $controller->redirect();
?>

Third, create the controller and name it controller.php
<?php
  defined('_JEXEC') or die('Restricted access');
  jimport('joomla.application.component.controller');
  
  class JCHelloWorldController extends JController
  {
    public function display()
    {
      parent::display();
    }
  }
?>

Fourth, create a folder and name it views. Inside the views folder, create another folder then name it jchelloworld.

Fifth, create the view file and name it view.html.php
<?php
  defined('_JEXEC') or die('Restricted access');
  jimport('joomla.application.component.view'); 

  class JCHelloWorldViewJCHelloWorld extends JView
  {
    public function display()
    {
      $model =& $this->getModel();

      $message = $model->getMessage();
      $this->assignRef('message', $message);

      parent::display();
    }
  }
?>

Fifth, create a folder inside /views/jchelloworld and name it tmpl.

Sixth, create the template file inside the tmpl folder and name it default.php
<?php
  defined('_JEXEC') or die('Restricted access');
  echo $this->message;
?>

Seventh, in your component's folder create another folder and name it models

Eighth, create the model file and name it jchelloworld.php
<?php
  defined('_JEXEC') or die('Restricted access');
  jimport('joomla.application.component.model');

  class JCHelloWorldModelJCHelloWorld extends JModel
  {
    public function getMessage()
    {
      return "hello world!";
    }
  }
?>

Zip the folder and install. You should see the result like this


Download version 5 here

----------------
version 1
version 2
version 3
version 4
version 5

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...