Lets start from site folder. Site folder and its content is
using for front end development of this component and admin folder is using for
backend development of this component.
Site:
1 Index.html: Create this page and Don’t
write anything into it. This page is using for security purpose. No body can
access site folder directly.
2 Controller.php:
This is one of the most important pages of this controller.
It controls the flow of this component.
Write following code into this php page:
<?php
/**
* Product Controller
page
* @author Mukesh Das
* @license License GNU General
Public License version 2 or later
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla controller library
jimport('joomla.application.component.controller');
/**
* Front Component
Controller
*/
class ProductController extends JController
{
}
Here I have created a controller (<COMPONENT NAME>Controller).
Here component name is product and this class is extended by JController class.
3. Product.php:
Write following code:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted
access');
jimport('joomla.application.component.controller');
$controller =
JController::getInstance('Product');
/*
In above line, you have created
an instance of class ProductController. In above line, you see only Product. It
is not any mistake. Joomla automatically append controller. So, Finally its not
Product. It is ProductController.
*/
$controller->execute(JRequest::getCmd('task'));
/*
In above line, if controller
find any task, then controller execute and redirect to that task.
I am explaining in details below
by example.
*/
$controller->redirect();
?>
I am explaining by an example, that how a controller takes
action on task.
Open controller.php
page again and replace content by
following code:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');
class ProductController extends JController{
function
create(){
echo
'Welcome to create a product Task';
}
function
delete(){
$app
= JFactory::getApplication();
$id
= JRequest::getInt('id');
echo
'Welcome to delete a product By Id
'.$id;
$app->close();
}
}
?>
What did I do? I have created two functions create and
delete into this class.
Now Its time to test this component.
Firstly zip component folder and install it from
administrator.
If component installed successfully, Then you are ready to
test.
Open following link on browser:
YOUR_WEBSITE_URL/index.php?option=com_product&task=create
When you open this link, then controller finds that you are
requesting a task. In product.php page, you have created an instance of
controller.php. If instance get that a function is available with the same name
as task. For example, here task = create. It means, if instance finds a
function name create into ProductController class. Then this function will be executed.
You get output like below:
Welcome to create a
product Task
Next Open following link
YOUR_WEBSITE_URL/index.php?option=com_product&task=delete
Output:
Welcome to delete a
product By Id
Next Open following link
YOUR_WEBSITE_URL/index.php?option=com_product&task=delete&id=5
Output:
Welcome to delete a
product By Id 5
What difference you get, when open these link?
When you open first link, you get output on running
template. But for other link, you get output without any template. I mean a single line output on full white page.
Why did you get
white page when run delete task?
$app = JFactory::getApplication();
$app->close();
Here you have closed this application, that why template is
disabled for this task.
If you remove above code, then you can see graphics like
create task.
You can use this feature for ajax call. I will explain
later.
Now again run following link
YOUR_WEBSITE_URL/index.php?option=com_product
Here you are not assigning any task, then controller
searches for default view. But you have not created any view this time. So, It
throws an error of page not found.
You can remove this problem by creating a function named
display.
function display(){
echo
‘I am without task';
}
Now again run following link
YOUR_WEBSITE_URL/index.php?option=com_product
You will get output
I am without task.
If you run without task, then controller runs display
function always.
I have explained controller.php,product.php page.
Now I move to controllers folder.
You have learnt default controller. This is time to know
about subcontrollers. As you have created many task into controller.php and use
it.
Similarly, you can create many sub controllers for different
task.