Question

In a case where the form action is set to something like:

action="<?php echo JRoute::_('index.php?option=com_test&layout=edit&id='.(int) $this->item->id); ?>"

and the form contains and hidden input:

<input type="hidden" name="task" value="testctrl.save" />

How does joomla route to the controller method?

I would understand if it had the task in the form action, but I can't see how it picks up the task from the hidden input in order to route to the appropriate method in the testctrl controller method

Was it helpful?

Solution

It's not that complicated. In your com_mycom directory there is a file called mycom.php. In it you have some lines that look like this:

$controller = JControllerLegacy::getInstance('Contact');
$controller->execute(JFactory::getApplication()->input->get('task'));
$controller->redirect();

See an example here: https://github.com/joomla/joomla-cms/blob/staging/components/com_contact/contact.php#L15

So that's what takes the task and instantiates an instance of that controller object, and pulls the task from the hidden form input value that you pointed out. It passes the task to the controller from there.

The controller receives the request here:

https://github.com/joomla/joomla-cms/blob/staging/components/com_contact/controller.php#L19

You might be asking "why don't I see it receiving the task that the component file sends it?". Well that's because the controller for this component is a child-class of the JControllerLegacy class:

https://github.com/joomla/joomla-cms/blob/staging/libraries/legacy/controller/legacy.php#L701

public function execute($task)
{ ... }

This function is the execute function which receives the task from the component. This is the parent class of your controller task. Hopefully this all makes sense!

OTHER TIPS

When you set the controller name explicitly with hidden fields

<input type="hidden" name="task" value="testctrl.save" />

or

<input type="hidden" name="controller" value="testctrl" />
<input type="hidden" name="task" value="save" />

or even not specifying the controller with task , just used it with view name.

All the cases your component file like com_test have a file with test.php

it includes Joomla library files.

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

when you check the library file it have two function for getting related controller and models.

createFileName() and getInstance() in libraries/joomla/application/component/controller.php

These two function do the task.

The above files are applicable only for Joomla 1.5 to Joomla 2.x

Edit

For Joomla3.x

In Joomla 3. x The file structure little bit changed.

Instead of jimport('joomla.application.component.controller'); Joomla 3.x uses

$controller = JControllerLegacy::getInstance('Content');

This will call the JControllerLegacy class in libraries\legacy\controller\legacy.php

you can find the same functions createFileName() ,getInstance() on the above path.

Hope its helps..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top