Codeigniter:从另一个控制器调用一个方法 [英] Codeigniter : calling a method of one controller from other

查看:92
本文介绍了Codeigniter:从另一个控制器调用一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个控制器'a'和'b'

I have two controllers 'a' and 'b'

我想从控制器'b'的方法调用控制器'

I would like to call a method of controller 'a' from a method of controller ''b'

任何人都可以指导我如何实现

could anyone guide me how can I achieve this

b

推荐答案

这是不支持的MVC系统的行为。如果你想执行另一个控制器的操作,你只是将用户重定向到你想要的页面(即使用URL的控制器函数)。

This is not supported behavior of the MVC System. If you want to execute an action of another controller you just redirect the user to the page you want (i.e. the controller function that consumes the url).

如果你想共享功能,你应该建立一个库,用于两个不同的控制器。

If you want common functionality, you should build a library to be used in the two different controllers.

我只能假设你想建立你的网站有点模块化。 (即在其他控制器方法中重用一个控制器方法的输出。)有一些CI的插件/扩展可以帮助你建立。然而,最简单的方法是使用库来构建常见的控件(即加载模型,将视图呈现为字符串)。

I can only assume you want to build up your site a bit modular. (I.e. re-use the output of one controller method in other controller methods.) There's some plugins / extensions for CI that help you build like that. However, the simplest way is to use a library to build up common "controls" (i.e. load the model, render the view into a string). Then you can return that string and pass it along to the other controller's view.

您可以通过添加 true 来加载到字符串中。 $ c>在视图调用结束时:

You can load into a string by adding true at the end of the view call:

$string_view = $this->load->view('someview', array('data'=>'stuff'), true);

这篇关于Codeigniter:从另一个控制器调用一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆