Monday 8 April 2013

Create a Joomla Component

Download Demo Component: com_hello
 
For our basic component, we only require five files:
  • site/hello.php - this is the entry point to our component
  • site/controller.php - this file contains our base controller
  • site/views/hello/view.html.php - this file retrieves the necessary data and pushes it into the template
  • site/views/hello/tmpl/default.php - this is the template for our output(Page)
  • hello.xml - this is an XML (manifest) file that tells Joomla! how to install our component.
  Remember that the filename for the entry point must have the same name of the component. For example, if you call your component "customer", at install time (see below in the hello.xml section) Joomla! will create the folder "com_cutomer" and the entry point php file must be named "cutomer.php"  otherwise it will not work. Be aware that use of some special characters, noticeably the underscore '_', may have special meaning in Joomla and should be avoided in component names or files.

 For example: if we want to create a separate component which name is customer then u have to changed some files like this

 Folder name: com_customer

  • site/customer.php - this is the entry point to our component
  • admin/customer.php - this is the entry point to our admin component
  • site/controller.php - this file contains our base controller
  • site/views/customer/view.html.php - this file retrieves the necessary data and pushes it into the template
  • site/views/customer/tmpl/default.php - this is the template for our output(Page)
  • customer.xml - this is an XML (manifest) file that tells Joomla! how to install our component.

Creating the Entry Point


Joomla! is always accessed through a single point of entry: index.php for the Site Application or administrator/index.php for the Administrator Application. The application will then load the required component, based on the value of 'option' in the URL or in the POST data. For our component, the URL would be:
index.php?option=com_customer&view=customer
This will load our main file, which can be seen as the single point of entry for our component: components/com_customer/customer.php.
The code for this file is fairly typical across components.
site/customer.php:
<?php
/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * components/com_hello/hello.php
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1
 * @license    GNU/GPL
*/
 
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
 
// Require the base controller
 
require_once( JPATH_COMPONENT.DS.'controller.php' );
 
// Require specific controller if requested
if($controller = JRequest::getWord('controller')) {
    $path = JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php';
    if (file_exists($path)) {
        require_once $path;
    } else {
        $controller = '';
    }
}
 
// Create the controller
$classname    = 'CustomerController'.$controller;
$controller   = new $classname( );
 
// Perform the Request task
$controller->execute( JRequest::getWord( 'task' ) );
 
// Redirect if set by the controller
$controller->redirect();
  • JPATH_COMPONENT is the absolute path to the current component, in our case components/com_customer. you can use JPATH_COMPONENT_SITE or JPATH_COMPONENT_ADMINISTRATOR.

  • DS is the directory separator of your system: either '/' or '\'. This is automatically set by the framework according to OS.

  • After loading the base controller, we check if a specific controller is needed. In this component, the base controller is the only controller, but we will leave this conditional check "in place" for future use. 

  • JRequest::getWord() finds a word variable in the URL or the POST data. So if our URL is index.php?option=com_hello&controller=controller_name, then we can retrieve our controller name in our component using: echo JRequest::getWord('controller'); 

  • this is a base controller if in 1 component there are many controller with many view then this base controller redirect the page according to controller, so  
Now we have our base controller 'CustomerController' in com_customer/controller.php, and, if needed, additional controllers like 'CustomerControllerController1' in com_customer/controllers/controller1.php. 
  • After the controller is created, we instruct the controller to execute the task, as defined in the URL: index.php?option=com_customer&task=sometask. If no task is set, the default task 'display' will be assumed. When display is used, the 'view' variable will decide what will be displayed. Other common tasks are save, edit, new... 

  • The controller might decide to redirect the page, usually after a task like 'save' has been completed. This last statement takes care of the actual redirection. 

 Creating the Controller

The code for the base controller site/controller.php is:
<?php
/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1
 * @license    GNU/GPL
 */
 
// No direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport('joomla.application.component.controller');
 
/**
 * Hello World Component Controller
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class CustomerController extends JController
{
    /**
     * Method to display the view
     *
     * @access    public
     */
    function display()
    {
        parent::display();
    }
 
}

 The JController::display() method will determine the name of the view and layout from the request and load that view and set the layout.

Creating the View

 

The task of the view is very simple: It retrieves the data to be displayed and pushes it into the template. Data is pushed into the template using the JView::assignRef method. (Note: The key (the first argument) passed to the assignRef method cannot be preceded by an underscore i.e. $this->assignRef('_greeting',$greeting). Doing so will cause the assignRef method to return false and your variable will not be pushed into the template.)
The code for the view at site/views/customer/view.html.php:

<?php
/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1
 * @license    GNU/GPL
*/
 
// no direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport( 'joomla.application.component.view');
 
/**
 * HTML View class for the HelloWorld Component
 *
 * @package    HelloWorld
 */
 
class CustomerViewCustomer extends JView
{
    function display($tpl = null)
    {
        $greeting = "Hello World!";
        $this->assignRef( 'greeting', $greeting );
 
        parent::display($tpl);
    }
}

Creating the Template

Template is nothing but 1 page which is display on your customer link 
 
URL: index.php?option=com_customer&controller=customer&view=customer
Our template is very simple: we only want to display the greeting that was passed in from the view - this file is:
site/views/customer/tmpl/default.php:
<?php
 
// No direct access
 
defined('_JEXEC') or die('Restricted access'); ?>
<h1><?php echo $this->greeting; ?></h1>
 

Creating the hello.xml File (for installing component in your project)

This package file contains a variety of information:
  • basic descriptive details about your component (i.e. name), and optionally, a description, copyright and license information.
  • a list of files that need to be copied.
  • optionally, a PHP file that performs additional install and uninstall operations.
  • optionally, an SQL file which contains database queries that should be executed upon install/uninstall
The format of the XML file at hello.xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<install type="component" version="1.5.0">
 <name>Hello</name>
 <!-- The following elements are optional and free of formatting constraints -->
 <creationDate>2007-02-22</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>1.01</version>
 <!-- The description is optional and defaults to the name -->
 <description>Description of the component ...</description>
 
 <!-- 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 -->
 <files folder="site">
  <filename>controller.php</filename>
  <filename>customer.php</filename>
  <filename>index.html</filename>
  <filename>views/index.html</filename>
  <filename>views/customer/index.html</filename>
  <filename>views/customer/view.html.php</filename>
  <filename>views/customer/tmpl/default.php</filename>
  <filename>views/customer/tmpl/index.html</filename>
 </files>
 
 <administration>
  <!-- Administration Menu Section -->
  <menu>Hello World!</menu>
 
  <!-- Administration Main File Copy Section -->
  <files folder="admin">
   <filename>customer.php</filename>
   <filename>index.html</filename>
  </files>
 
 </administration>
</install>

 Note:--

so I will show u functionality flow in brief please remember
  • At the time of page loading first goto 
-->base controller(com_customer/customer.php) 
-->controller(com_customer/controller.php)
-->view.html.php(com_customer/views/customer/)---fetch all data for template
-->default.php(com_customer/views/customer/tmpl/default.php)---default page
  •  At the time of any action like save,keyword search and submit any form
-->base controller(com_customer/customer.php) 
-->controller(com_customer/controller.php)---in this you have to write save function according to task which is defind in form and call model
--> create model (com_customer/models/customer.php)--here you have to write a query for insering updating.
---> Back to controller(com_customer/controller.php)
---->and redirect to default .php
  • At the time of editing
-->base controller(com_customer/customer.php) 
-->controller(com_customer/controller.php)
-->view.html.php(com_customer/views/customer/)---fetch all data for template
 --->model (com_customer/models/customer.php)--here you have to write a query for insering updating.
---> Back to view.html.php(com_customer/views/customer/)
---->and redirect to default .php
 

Thanks Guys if u have any question just e-mail me or comment me!!

No comments:

Post a Comment