我有两个 Controller a
和b
。
我想从 Controller b
的方法调用 Controller a
的方法。
有人可以帮助解释我如何实现这一目标吗?
最佳答案
这不是 MVC 系统支持的行为。如果您想执行另一个 Controller 的操作,只需将用户重定向到您想要的页面(即使用 url 的 Controller 函数)。
如果您想要通用功能,您应该构建一个库以在两个不同的 Controller 中使用。
我只能假设您想要以模块化的方式构建您的网站。 (即在其他 Controller 方法中重复使用一个 Controller 方法的输出。)有一些 CI 插件/扩展可以帮助您进行这样的构建。然而,最简单的方法是使用库来构建通用“控件”(即加载模型,将 View 渲染为字符串)。然后您可以返回该字符串并将其传递到另一个 Controller 的 View 。
您可以通过在 View 调用末尾添加 true
来加载到字符串中:
$string_view = $this->load->view('someview', array('data'=>'stuff'), true);
关于代码点火器 : calling a method of one controller from other,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6500022/