问题描述
我有一个自定义模块,现在想从checkout/cart
调用add()
函数.如何调用控制器和函数?
I have a custom module, and now want to call the add()
function from checkout/cart
. How do I call the controller and function?
我尝试了$this->load->controller('checkout/cart');
,但这会返回致命异常.
I have tried $this->load->controller('checkout/cart');
but this returns a fatal exception.
我正在使用OpenCart v 1.5.6.4
I am using OpenCart v 1.5.6.4
推荐答案
在OpenCart 1.5.*中,getChild
用于加载其他控制器.具体来说,它正在运行到所需控制器和功能的路由.例如,common/home
将从common
组/文件夹中加载home
控制器.通过添加第三个选项,我们指定了一个函数.在这种情况下,为添加"-checkout/cart/add
.
In OpenCart 1.5.*, getChild
is used to load other controllers. Specifically, it is running a route to the desired controller and function. For example, common/home
would load the home
controller from the common
group/folder. By adding a third option we specify a function. In this case, 'add' - checkout/cart/add
.
class ControllerModuleModule1 extends Controller {
protected function index() {
ob_start();
$this->getChild('checkout/cart/add');
$this->response->output();
$response = ob_get_clean();
}
}
大多数控制器不返回或回显任何内容,而是在$this->response
对象中指定要输出的内容.要获取正在渲染的内容,您需要调用$this->response->output();
.在上面的代码中,$response
是checkout/cart/add
回显的json字符串.
Most controllers don't return or echo anything, but specify what to output in the $this->response
object. To get what is being rendered you need to call $this->response->output();
. In the above code $response
is the json string that checkout/cart/add
echos.
这篇关于OpenCart调用其他控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!