如何从codeigniter中的另一个控制器加载控制器? [英] How to load a controller from another controller in codeigniter?

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

问题描述

我想从另一个控制器中的函数加载控制器,因为我集成到我的项目中的库我不想加载到控制器,因为我想保持它干净和相关。

I want to load a controller from a function in another controller because the library I integrated to my project I don't want to load it to the controller because I want to keep it clean and related.

我试过使用模块,但我仍然不得不把控制器放在url,如

I tried using modules but I still had to put controller in the url like

http://example.com/maincontroller/function

http: //example.com/othercontroller/function

我有预设控制器,因此我可以载入 http://example.com/function ,所以我怎么能从一个来自main的函数访问控制器,所以我不必把控制器放在url中。

I have default controller so I can load http://example.com/function so how could I access the controller from a function from main so I don't have to put the controller in the url.

我仍然愿意使用HMVC,如果我可以从主控制器功能加载控制器功能。

I'm still willing to use HMVC if I can load the controller function from the main controller function.

推荐答案

无法从CI中的控制器加载控制器 - 除非使用HMVC或其他。

You can't load a controller from a controller in CI - unless you use HMVC or something.

您应该考虑一下您的架构。如果你需要从另一个控制器调用一个控制器方法,那么你应该将该代码抽象为助手或库并从两个控制器调用它。

You should think about your architecture a bit. If you need to call a controller method from another controller, then you should probably abstract that code out to a helper or library and call it from both controllers.

UPDATE

再次阅读您的问题后,我意识到您的最终目标不一定是HMVC,而是URI操作。如果我错了,请纠正我,但似乎你正在尝试完成URL,第一部分是方法名称,并完全忽略控制器名称。

After reading your question again, I realize that your end goal is not necessarily HMVC, but URI manipulation. Correct me if I'm wrong, but it seems like you're trying to accomplish URLs with the first section being the method name and leave out the controller name altogether.

如果是这种情况,您可以通过使用您的路线的广告获得更清洁的解决方案。

If this is the case, you'd get a cleaner solution by getting creative with your routes.

对于一个非常基本的例子,假设你有两个控制器, controller1 controller2 Controller1 有一个方法 method_1 - 和 controller2 method_2

For a really basic example, say you have two controllers, controller1 and controller2. Controller1 has a method method_1 - and controller2 has a method method_2.

您可以设置如下路线:

$route['method_1'] = "controller1/method_1";
$route['method_2'] = "controller2/method_2";


$ b site.com/method_1 和方法2与 http://site.com/method_2

虽然,这是一个硬编码,非常基本的例子 - 但它可以让你到你需要的地方,如果你需要做的是从URL中删除控制器。

Albeit, this is a hard-coded, very basic, example - but it could get you to where you need to be if all you need to do is remove the controller from the URL.

您也可以使用重新映射控制器

从文档:如果您的控制器包含一个名为_remap()的函数,它将总是被调用, URI包含。:

From the docs: "If your controller contains a function named _remap(), it will always get called regardless of what your URI contains.":

public function _remap($method)
{
    if ($method == 'some_method')
    {
        $this->$method();
    }
    else
    {
        $this->default_method();
    }
}

这篇关于如何从codeigniter中的另一个控制器加载控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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