php 在 Codeigniter 中从内部视图调用控制器方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2997288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:22:36  来源:igfitidea点击:

Calling controller methods from inside view in Codeigniter

phpcodeigniter

提问by Aadi

Is there a way to call a method inside the controller from our view using codeigniter.I know it is a bad practice but, now I force to do this.Thank you

有没有办法使用 codeigniter 从我们的视图中调用控制器内部的方法。我知道这是一个不好的做法,但现在我强制这样做。谢谢

回答by Raj

If you want to call a function of the current controller, you have to get the instance of the current controller this way:

如果你想调用当前控制器的一个函数,你必须通过这种方式获取当前控制器的实例:

<?php
$CI =& get_instance();
$CI->your_method($param);
?>

回答by Kurucu

You can just do:

你可以这样做:

$this->controller_method();

While this might answer your question, I personally agree with the comments of – Matthew J Morrison and DamienL.

虽然这可能会回答您的问题,但我个人同意 Matthew J Morrison 和 DamienL 的评论。

回答by Haruo

in your controller put

在你的控制器放

$data['myClass'] = $this;

That way when you send the data to the view, it will load the controller :)

这样,当您将数据发送到视图时,它将加载控制器:)

Then you can access the methods like

然后你可以访问像这样的方法

$myClass->method();

回答by Igor

In controller:

在控制器中:

$this->method_call =& get_instance(); 

In view

在视图中

$this->method_call->show();