[英]OpenCart Call Different Controller
I have a custom module, and now want to call the add()
function from checkout/cart
. 我有一个自定义模块,现在想从
checkout/cart
调用add()
函数。 How do I call the controller and function? 如何调用控制器和功能?
I have tried $this->load->controller('checkout/cart');
我已经尝试过
$this->load->controller('checkout/cart');
but this returns a fatal exception. 但这会返回致命异常。
I am using OpenCart v 1.5.6.4 我正在使用OpenCart v 1.5.6.4
In OpenCart 1.5.*, getChild
is used to load other controllers. 在OpenCart 1.5。*中,
getChild
用于加载其他控制器。 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. 例如,
common/home
将从common
组/文件夹中加载home
控制器。 By adding a third option we specify a function. 通过添加第三个选项,我们指定了一个函数。 In this case, 'add' -
checkout/cart/add
. 在这种情况下,'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();
}
}
Most controllers don't return or echo anything, but specify what to output in the $this->response
object. 大多数控制器不返回或回显任何内容,而是在
$this->response
对象中指定要输出的内容。 To get what is being rendered you need to call $this->response->output();
要获取正在渲染的内容,您需要调用
$this->response->output();
. 。 In the above code
$response
is the json string that checkout/cart/add
echos. 在上面的代码中,
$response
是checkout/cart/add
回显的json字符串。
To solve the same issue, I use $this->load->controller("checkout/cart/add"). 为了解决相同的问题,我使用$ this-> load-> controller(“ checkout / cart / add”)。
If I use getChild, this exception get thrown : "Call to undefined method Loader::getChild()". 如果我使用getChild,则会引发此异常:“对未定义方法Loader :: getChild()的调用”。
What is the difference between the 2 methods? 两种方法有什么区别? Is getChild better?
getChild更好吗?
The problem with getChild()
is it only works if the controller calls $this->response->setOutput()
or echo
at the end - producing actual output. 这个问题
getChild()
是只有当控制器调用工作$this->response->setOutput()
或echo
末-产生实际输出。 If on the other hand, you want to call a controller method that returns a variable response, it isn't going to work. 另一方面,如果您要调用返回变量响应的控制器方法,则它将无法正常工作。 There is also no way to pass more than one argument since
getChild()
accepts only one argument to pass, $args
. 由于
getChild()
仅接受传递一个参数$args
,因此也无法传递多个$args
。
My solution was to add this bit in 1.5.6.4 to system/engine/loader.php which allows you to load a controller and call it's methods in the same way as you would a model: 我的解决方案是将1.5.6.4中的此位添加到system / engine / loader.php中 ,它允许您加载控制器并以与模型相同的方式调用其方法:
public function controller($controller) {
$file = DIR_APPLICATION . 'controller/' . $controller . '.php';
$class = 'controller' . preg_replace('/[^a-zA-Z0-9]/', '', $controller);
if (file_exists($file)) {
include_once($file);
$this->registry->set('controller_' . str_replace('/', '_', $controller), new $class($this->registry));
} else {
trigger_error('Error: Could not load controller ' . $controller . '!');
exit();
}
}
Now you can do this: 现在您可以执行以下操作:
$this->load->controller('catalog/example');
$result = $this->controller_catalog_example->myMethod($var1, $var2, $var3);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.