4

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

4
  • 3
    Can't you just refactor the code that is in your view that you need to reuse and put it someplace else? Like in your controller for instance? Commented Jun 8, 2010 at 12:41
  • 1
    It would be interesting to see exactly what you are trying to do. Commented Jun 8, 2010 at 12:49
  • Would also like to know what you are trying to do as this would help with the solution Commented Jun 8, 2010 at 12:50
  • I need it because I have two views (contract view and invoice) and need the same output from a controllers (contract-invoice) variable in a loop. It seems more logic to have a function in the controller than in the utility helper which is global. Commented Oct 14, 2013 at 10:05

4 Answers 4

9

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);
?>
Sign up to request clarification or add additional context in comments.

Comments

7

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.

1 Comment

Don't think this will work, doesn't $this refer to the Loader class inside a view?
4

In controller:

$this->method_call =& get_instance(); 

In view

$this->method_call->show();

Comments

4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.