How Controller Works in Joomla

After Joomla begins to load your component it needs to know where to go. A controller makes it easy to define a list of places where Joomla can take a request, so let's add a controller,go to be components and com_noidacity folder in your Joomla Installation (which we have created in our earlier tutorials) and create a file called controller.php.

Creating Controller file in joomla

And Replace with the following code

  1. <?php
  2. defined( '_JEXEC' ) or die;
  4. jimport('joomla.application.component.controller');
  6. class NoidaCityController extends JController{
  7. public function save(){
  8. echo "saving mall...";
  9. }
  11. public function delete(){
  12. echo "deleting mall...";
  13. }
  14. }


  1. jimport('joomla.application.component.controller');


Let's take a look at this controller.php file now, you'll notice that first we're importing the library that has all the controller code through this jimport function. You'll notice that this jimport function here at the top is being used to pull in a library. This function is used throughout Joomla to do just that pull in libraries that you want to use in this file.

  1. class NoidaCityController extends JController{
  2. .
  3. .
  4. .
  5. }


Next we've a class definition and first the name of the class is NoidaCity which matches the name of our component and then that's followed by controller and this is a convention, so that Joomla knows how to name the controller that it's trying to load and this class extends the JController class that's the base class that Joomla has available for controllers.

  1. public function save(){
  2. echo "saving mall...";
  3. }
  5. public function delete(){
  6. echo "deleting mall...";
  7. }


And then we have two different functions in this controller, we have one call the save, and then we have one call Delete and in each case we're doing something very basic, we're just echoing out what we're doing we're not actually saving or deleting any records at this point.

So next to make sure that this controller gets executed we need to change the noidacity.php code that's currently on the site, so that it loads the controller rather than having our current output.

Default Component display in the frontend

Our current output in the frontend looks like this. If you go to index.php?option=com_noidacity it just says front end. This is the output that we bundled with the original installation package in our earlier tutorials, so let's replace this with code that loads the controller instead.

Opening component file for editing

So open up noidacity.php and replace it with the following code

  1. <?php
  2. defined( '_JEXEC' ) or die;
  4. jimport('joomla.application.component.controller');
  6. $controller = JController::getInstance('NoidaCity');
  7. $controller->execute(JRequest::getCmd('task'));
  8. $controller->redirect();


  1. jimport('joomla.application.component.controller');


Notice again where importing the controller class and this is because we're calling the JController class directly using to get instance function.

  1. $controller = JController::getInstance('NoidaCity');


What this is going to do is it's going to get an instance of the controller that's in the other file (remember the controller file we created with a class name NoidaCityController). Once it gets an instance of the controllers that's in the other file and it gets the one that's prefixed with NoidaCity.

  1. $controller->execute(JRequest::getCmd('task'));


We're going to call the execute function on the controller object. We pass the value of task into this execute function that way Joomla knows what value to use when choosing the function that it needs to run.

  1. $controller->redirect();


Finally at the end we're calling the redirect function on the object and what this does is if at any point in the controller we set a redirect to say when the request is done go to this other page, this redirect function is going to take us to any other page we set. In this case we haven't done that yet but we will use this feature in our later tutorials.

So let's fire off some tasks. We have the Save task and the Delete task. To fire off these tasks all we need to do is add a variable to our URL called task and match the name of the function to the variable in the URL.

Saving mall in the frontend joomla

Fronend displaying for component

So we're going to add &task=save and hit Enter and now it's says saving mall and if we change this to delete it shows deleting mall. So you notice we have save and delete.

500 error in joomla

If I try another task say close it gives us a 500 error because we don't have a task defined for close and this is very powerful, because we can use our controller to restrict exactly what people can and cannot do with our component.

So controllers make clear what tasks your component can perform. The controller only executes one task at a time, so you don't have to worry about running the wrong code and whenever you need a new task it's as easy as adding a new function.