在表单操作设置为类似的情况下:
action="<?php echo JRoute::_('index.php?option=com_test&layout=edit&id='.(int) $this->item->id); ?>"
并且表单包含和隐藏输入:
<input type="hidden" name="task" value="testctrl.save" />
joomla如何路由到 Controller 方法?
如果它在表单操作中有任务,我会理解,但我看不到它如何从隐藏的输入中获取任务,以便路由到 testctrl
中的适当方法 Controller 方法
最佳答案
没那么复杂。在您的 com_mycom
目录中有一个名为 mycom.php
的文件。在其中你有一些看起来像这样的行:
$controller = JControllerLegacy::getInstance('Contact');
$controller->execute(JFactory::getApplication()->input->get('task'));
$controller->redirect();
请看这里的例子: https://github.com/joomla/joomla-cms/blob/staging/components/com_contact/contact.php#L15
这就是接受任务并实例化该 Controller 对象的实例,并从您指出的隐藏表单输入值中提取任务的原因。它从那里将任务传递给 Controller 。
Controller 在这里接收请求:
https://github.com/joomla/joomla-cms/blob/staging/components/com_contact/controller.php#L19
您可能会问“为什么我看不到它接收组件文件发送给它的任务?”。那是因为这个组件的 Controller 是 JControllerLegacy 类的子类:
https://github.com/joomla/joomla-cms/blob/staging/libraries/legacy/controller/legacy.php#L701
public function execute($task)
{ ... }
这个函数是从组件接收任务的执行函数。这是您的 Controller 任务的父类。希望这一切都有意义!
关于php - Joomla 如何在提交表单时路由到 Controller 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21896842/