开发者

Calling controller methods from inside view in Codeigniter

开发者 https://www.devze.com 2023-01-02 12:29 出处:网络
Is there a way to call a method inside the controller from our view开发者_StackOverflow社区 using codeigniter.I know it is a bad practice but, now I force to do this.Thank youIf you want to call a fun

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


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);
?>


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.


In controller:

$this->method_call =& get_instance(); 

In view

$this->method_call->show();


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消