Title: CakePHP 2.0.4: Using the "Number" Helper in Controllers
Tags: model-view-controller;cakephp;cakephp-2.0
Question: I know it's against MVC methodologies to use helpers in controllers, but there are some cases where it's useful. For example, consider this snippet of controller code from one of my CakePHP 1.3.13 projects that handles an image upload:
elseif ($_FILES['data']['error']['ModelName']['field_name'] === UPLOAD_ERR_INI_SIZE) {
App75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0import('Helper', 'Number');
$Number = new NumberHelper();
$this-&gt;Session-&gt;setFlash("The image you uploaded was not saved because it appeared to be larger than {$Number-&gt;toReadableSize($max_filesize_in_bytes)}.");
I'm working on a CakePHP 2.0.4 project now and I used the same code, except I replaced App75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0import('Helper', 'Number'); with App75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0uses('NumberHelper', 'View/Helper'); and I got this error message:
Warning (4096): Argument 1 passed to Helper75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0__construct() must be an instance of View, none given, called in /Path/To/My/Website/app/Controller/MyController.php
Any ideas?

Here is the accepted answer: you shouldnt use helpers in controllers. I proposed quite some time ago that there should be library classes for this. hopefully this will be integrated in 2.1
until then you should be using
$Number = new NumberHelper(new View(null));
Comment for this answer: I know - I already built the necessary classes: https://github.com/dereuromark/utility-upgrade/tree/master/Lib/Utility. the core team seems to be reluctant to finally make cakephp fully MVC compatible.
Comment for this answer: For the record: this has been added in cake2.1 - as I proposed. they are now Utility functions which can be accessed statically from anywhere in your application.
Comment for this answer: that is intentional. the html belongs to the view layer after all. if you really want to do something like that you still have to import the view helpers here to bend the rules your way. but no, it will never be moved from the helper to the lib, i'm afraid.
Comment for this answer: Exactly. Validation only, no filtering/sanitizing/escaping/autoLinking. That's what I have been preaching for the last 3 years.
Comment for this answer: If someone is looking for how to do it in CakePHP 2.1+, the documentation is here: http://book.cakephp.org/2.0/en/core-utility-libraries/number.html  Eg: echo CakeNumber75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0toReadableSize($peakMemoryUsage);
Comment for this answer: I know this is a year old, but just to add my thoughts: One shouldn't perform destructive changes (ie. regular expression voodoo) to user-submitted data (eg. blog comments) before it goes in the database as this is a perfect recipe for losing some or all of the original data. We have seen this on programming blogs before where a useful comment is sometimes swallowed by an over-zealous XSS filter and there is no original copy. By doing these things on output instead, you are able to make code adjustments after the fact.. it's not possible to fix the bugs before they happen.
Comment for this answer: That works! Thank you! I know, I feel dirty using it in the controller, but I don't know what else to do.
Comment for this answer: Yeah, I saw that and was pleased. However, not all of the "Text" helper's methods were moved into the "String" class, such as the "autoLink*" methods. It's too bad because I could see those methods being used outside of the "View" layer. For example, some people (not me) like to store HTML in the database, so they might want to run one of the "autoLink*" methods on, say, a "Comments" field before saving it to the database. Maybe those methods will be moved someday. Anyway, thanks for commenting for people who might happen upon this someday!

Here is another answer: I know it is late but Cake Core team had embedded CakeNumber Class in core library. From now onwards we should use directly below functions in our controller / model instead creating an object of helper.
CakeNumber75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0urrency(mixed $number, string $currency = 'USD', array $options = array())
CakeNumber75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0ultCurrency(string $currency)
For more useful functions list please see CakeNumber class and to see complete list of core libraries please visit Core Libraries.
I hope it may help some one.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:12/14/11', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<model-view-controller><cakephp><cakephp-2.0>', 'Title': 'CakePHP 2.0.4: Using the "Number" Helper in Controllers', 'ViewCount': 3202, 'id': 8511826, 'user_AboutMe': '', 'user_AccountId': 900908.0, 'user_DisplayName': 'Nick', 'user_Reputation': 7769.0, 'user_Views': 735.0, 'user_id': 937084.0}}