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) {
App::import('Helper', 'Number');
$Number = new NumberHelper();
$this->Session->setFlash("The image you uploaded was not saved because it appeared to be larger than {$Number->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 App::import('Helper', 'Number'); with App::uses('NumberHelper', 'View/Helper'); and I got this error message:
Warning (4096): Argument 1 passed to Helper::__construct() must be an instance of View, none given, called in /Path/To/My/Website/app/Controller/MyController.php
Any ideas?
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