如何在opencart中调用控制器内的函数

Qch*_*mqs 0 php ajax opencart

我有一个控制器,让我们说thingy/stuff目录

<?php public function index() { /*thingy stuff */ }

public function anotherfunction() {/*other thingy stuff*/} ?>
Run Code Online (Sandbox Code Playgroud)

我看到网址就像 index.php?route=thingy/stuff&var=dd

我想要的是在该控制器内调用$ .post到这个函数

所以它使用另一个模板文件thingy.tpl并返回html使用

URL应该是什么样的?

我搜索了几个小时,听起来好像没有开放式购物车的开发文档

Toh*_*hid 6

假设你在thingy文件夹下有一个名为"stuff"的控制器,在该类中有一个名为"my function"的函数,如下所示:

class ControllerThingyStuff extends Controller {
    public function index() {  
  // Some code
}
public function myfunction() {
// Your code
}
}
Run Code Online (Sandbox Code Playgroud)

如果你想使用URL直接与这个函数通信,你可以将函数名称添加到路由参数"route = thingy/stuff/myfunction&..."的末尾,并在函数内加载thingy.tpl并在之后返回渲染:

// some code
$this->template = 'template/product/thingy.tpl';
...
$this->response->setOutput($this->render());
Run Code Online (Sandbox Code Playgroud)

如果使用open cart 1.5并且你想使用带有JSON的jQuery AJAX,那么你需要在渲染之前导入JSON库:

$this->template = 'thingy/stuff/thingy.tpl';
$json['output'] = $this->render();
$this->load->library('json');
$this->response->setOutput(Json::encode($json));
Run Code Online (Sandbox Code Playgroud)

看一下结账页面以获得一些想法,默认的open cart 1.5模板使用相同的技术来加载每个部分的模板.