Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Joomla route to the controller method on submitting the form?

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

like image 494
doovers Avatar asked Sep 08 '25 07:09

doovers


1 Answers

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!

like image 99
Chad Windnagle Avatar answered Sep 10 '25 22:09

Chad Windnagle