5

I am struggling to understand how to call sub controllers from a joomla component. What are to be placed in the controllers folder?

I have the entry point of my component like -

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// require helper file
JLoader::register('TieraerzteHelper', dirname(__FILE__) . DS . 'helpers' . DS . 'my_helper.php');

// import joomla controller library
jimport('joomla.application.component.controller');

$controller = JController::getInstance('MyController');  

// Get the task
$jinput = JFactory::getApplication()->input;
$task = $jinput->get('task', "", 'STR' );

// Perform the Request task
$controller->execute($task);

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

Then if I want to call a controller, which is placed in the controllers folder, how do I do that?

4
  • Magnets, how do they work?
    – uchuugaka
    Commented Mar 31, 2013 at 15:55
  • YOu've got a strange mixture there of old and new i.e. JInput (new, good), jimport (not really used any more because of JLoader). If you look at the joomla core components you will see how to put the subcontrollers, yes in the controllers folder. You might want to look at the MVC tutorialhttp://docs.joomla.org/Developing_a_Model-View-Controller_Component/2.5/Introduction or Andrew Eddie has one specifically on subcontrollers on his site.
    – Elin
    Commented Mar 31, 2013 at 16:28
  • I was just following the tutorial on the core site for joomla 2.5
    – fefe
    Commented Mar 31, 2013 at 16:29
  • my problem how do I call the sub controller, how is going to be initialized, where should I do that?!
    – fefe
    Commented Mar 31, 2013 at 18:18

1 Answer 1

8

You do a task=controller.function

As an example: You want to call the MycomponentControllerFoo in /controllers/foo.php and execute the function bar(). You use the following URL to call this:

index.php?option=com_mycomponent&task=foo.bar

Or you can use a form where there is a hidden task field.

3
  • it seems like I'm getting closer to the issue. So the task GET variable can hold as well a controller. I thought is reserved for action tasks like edit, delete...
    – fefe
    Commented Mar 31, 2013 at 18:42
  • 1
    Doesn't matter if GET or POST, both will work. The is supposed to hold the task which consists of controller.function. If you don't have a dot in it, Joomla assumes you want to run the default controller, no subcontroller. So "edit", "delete", ... usually don't work. You need "yoursubcontroller.edit", "yoursubcontroller.delete".
    – Bakual
    Commented Mar 31, 2013 at 19:08
  • But how will this work when he (in his code) explicitly told it to use the MyController? Commented Jul 24, 2014 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.