CodeIgniter Forums
calling a public controller method from within a view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: calling a public controller method from within a view (/showthread.php?tid=23165)



calling a public controller method from within a view - El Forum - 10-02-2009

[eluser]0x0b4dc0d3[/eluser]
I have searched high and low, and I cannot determine how to call a controller method from within a view. Is it possible? Is there a better solution?

Thanks in advance.


calling a public controller method from within a view - El Forum - 10-02-2009

[eluser]0x0b4dc0d3[/eluser]
Actually, I can see why this is not "MVC-compliant" and I should really just pass whatever data I need to the view from within the controller. Is that correct?


calling a public controller method from within a view - El Forum - 10-02-2009

[eluser]philm[/eluser]
[quote author="0x0b4dc0d3" date="1254516406"]Actually, I can see why this is not "MVC-compliant" and I should really just pass whatever data I need to the view from within the controller. Is that correct?[/quote]
Bingo! :-)


calling a public controller method from within a view - El Forum - 10-02-2009

[eluser]n0xie[/eluser]
[quote author="0x0b4dc0d3" date="1254516406"]Actually, I can see why this is not "MVC-compliant" and I should really just pass whatever data I need to the view from within the controller. Is that correct?[/quote]
The short answer: Yes.

The long and generally more advanced answer: what you ask for can be done with some adjustments. wiredesignz has written several enhancements to the CI framework which make this possible, either through modules or widgets although it might be a bit advanced for someone just trying to get his feet wet.


calling a public controller method from within a view - El Forum - 10-02-2009

[eluser]0x0b4dc0d3[/eluser]
Thanks for all the info.


calling a public controller method from within a view - El Forum - 10-03-2009

[eluser]bigtony[/eluser]
I agree will all the previous comments, but as an aside it's worth remembering that you can easily setup helpers with functions that do stuff useful for a view.

Here's an example:
Code:
/*
* Returns a formatted decimal value for viewing
*/
function format_decimal($value, $currency='£', $decimals=2) {
    return  $currency . number_format($value, $decimals);
}

Then in your view:
Code:
<p>The cost is &lt;?php format_decimal($some_value); ?&gt;, a great bargain!</p>
Which will produce something like "The cost is £1,234.56, a great bargain!"