Thursday, 14 March 2013

Joomla Component Development Example-I (Controller)

          In the previous post I described the file structure for Joomla! component. To learn what code goes into each of the files, lets take an example for a component for student records. This is just a basic example that illustrates the basic component development in joomla, including use of JDatabase and JTable. 

         As Joomla is an MVC, we know the basic working of Model, View and Component. We build a component named com_student, in which Model, View and Controller are controller.php, Views/view.html.php and Models/student.php respectively. The file structure remains the same as described in my previous post, just replace 'hello' by 'student' in it.

      The component for site and administrator can be thought of as two different components. Here we build a component that allows the user to edit the student name at the administrator and the component at the site just displays all student names. We first take a look at the component for administrator.

         The code for student.php is described below. What each line does here, is described in the comments:


<?php

// 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    = 'StudentController'.$controller;
$controller   = new $classname( );

// Perform the Request task
$controller->execute( JRequest::getWord( 'task' ) );

// Redirect if set by the controller
$controller->redirect();



                           Our controller.php will contain functions for every tasks that user can issue. The tasks can be edit, remove, save, publish, etc..

<?php
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');

class StudentController extends JController
{
public function display()
{
parent::display();
}

public function edit()
{
JRequest::setVar( 'layout', 'form'  );
JRequest::setVar( 'hidemainmenu', 1 );
parent::display();
}

       }



  • The first line of the code, as the comment shows is to ensure that the file is not accessed directly. This is for the matter of security.
  • Next the ‘jimport(…);’ will include the php file "/libraries/joomla/application/component/controller.php". This file holds the class JController.
  • From the next line starts our controller class, which extends the JController.
  • The naming convention for our controller class is controller_nameController.
  • The display() function will display the default.php file,  and this is the default function called when there are no other tasks to be done.
  • Now, for tasks save and delete only the database operations are done so you make those functions same as the 'display()' function.
  • For the tasks edit and new we need to display a form that allows the user to select a new image. So for these tasks we need to set a layout different from the default layout, as shown in the function 'edit()'.  And the toolbar also wont be same as the default layout so we hide that using the second line of 'edit()' function ( we will see this later in detail ).

  • For all the database operations we use the JTable class of Joomla!
  • We make an extension for that class as follows, and this file resides at admin/tables/slider.php.
   <?php
   defined('_JEXEC') or die();
   class TableSlider extends JTable
   {
        var $id = null;
        var $name = null;
        var $published = 0;
        function __construct(&$db)
        {
                parent::__construct( '#__slider', 'id', $db );
        }
   }


  • The variables inside this class corresponds to the columns in our database.
  • Then create a constructor for the class that accepts a reference to the current database instance. This will call the parent constructor which needs the name of the table, the name of the primary key column, and the database instance. The name of the table uses #__ instead of jos_, as the administrator can pick any table prefix desired during Joomla! installation.

  • Now, we can it can used throughout Joomla! for creating, reading, updating, and deleting records in the database table.
  • In our publish() function we get the instance of our table class, and set the publish value for the image as ‘1’. First line of the function includes our Table class and then we get the instance of our JTable extension and use it to publish the student name.

    function publish()
    {
    JTable::addIncludePath(JPATH_COMPONENT.DS.'tables');
    $cid = JRequest::getVar ('cid', array(), '', 'array');
    $task=JRequest::getWord('task');
    if($task=='publish')
    {             
    $publish=1;
    }
    $sliderTable =& JTable::getInstance('student', 'Table');
    $sliderTable->publish($cid, $publish);     
    parent::display();
    }

  • Same thing is done with the unpublish() function. In that we just do $publish=0.

2 comments:

  1. Thanks you lot it solve lot's of problems of mine

    ReplyDelete
  2. You are welcome...:)
    You can tell me here if you any some problems.

    ReplyDelete