A question asked from time to time in the IRC channel is: “How can I use helper X in my controller?”.
Well, helpers are not thought to be used in controllers. They are designed to be used in views. So whenever this question arises, you have to ask yourself (or others) whether you are doing something wrong and if there is no better solution.
Nonetheless, in some (rare) cases it can be useful to use a helper inside a controller to avoid code duplication. It can be accomplished in the following way:
class UsersController extends AppController {
function index() {
App::import('Helper', 'Html'); // loadHelper('Html'); in CakePHP 1.1.x.x
$html = new HtmlHelper();
debug($html->link('Cake!', 'http://cakephp.org'));
}
}
Update 2007-08-13: m3nt0r has published a helper component in his blog which allows you to use a helper like a component. The article is in German, but the code should be self-explanatory.
Update 2008-06-07: Using App::import() instead of loadHelper() in the example. Thanks to mjamesd for the hint!

[...] Vía | CakeBaker [...]
Could it be, hmmm…. Saaaataan?
Only time I’ve ever found it useful to use a helper in a controller is when I’m formatting an email for to send out, or formatting text to display in some fashion other than html (csv, perhaps). But I’m kind of learning the proper way is still to send the data to a view and format it in a view.
Yeah once in a rare rare while helpers are needed in controllers though.
I use it for $time->daysAsSQL
Maybe I m wrong using it like that
When I start “baking” with that framework I try to access a Model in a View directly :D
Now my mind is turned MVC, so this looking really odd to me.
Here is a question to ponder…
Say I have a single function that I want available to all MVC’s in my application. Where do I create it?
This, IMO, is where making helpers “global” is helpful. I think many people when they hear “helper” don’t immediately assume its view related.
@bparise /app/config/bootstrap.php
@all: Thanks for your comments.
@wluigi: Yes, that’s one method I could imagine to use in a controller because it is probably by accident in a helper and not in a component.
@bparise: That’s a good point. Maybe it would be easier to have one “global helper” concept instead of the three slightly different concepts “behavior”, “component”, and “helper”.
CakePHP: HelperComponent…
Als ich heute so durch meine RSS Subscriptions wanderte fand ich einen Artikel bei CakeBaker wie man in einem Controller einen Helper verwenden kann. Ich stimme natürlich zu, dass es nicht immer Sinn macht und eigentlich vermieden werden sollte, aber …
Really there also one usefull topic when RoR developers use helpers insede controller. This is RJS calls. For the cake case this is CJS helper developed by the RoS.
@Yevgeny: Hm, I never used RJS nor CJS, but shouldn’t you use them usually in views?
@cakebaker,
You does not disturb when you use $this->Session->setFlash in your controller.
For example you write ajax driven application and one of your action just doing something and without refresh screen show text result. Will you create view with two line of code
replace_html(‘#message’,$message); ?>
effect(‘#message’,'Highlight’,array(‘duration’=>2, ’startcolor’=>’#7777ff’)); ?>
and use $this->set(‘message’, __(’some_msg’,true)); in your controller.
And if you have many such views you will not DRY.
But you can create helper Msg with function
showMessage ($message) {
$this->Page->replace_html(‘#message’,$message); ?>
$this->Page->effect(‘#message’,'Highlight’,array(‘duration’=>2, ’startcolor’=>’#7777ff’));
}
and use this helper from controller like this:
$this->Msg->showMessage(‘ok’);
exit;
@Yevgeny: Yes, that’s a possible solution to avoid creating too many very simple views.
Personally, I have used a different approach to avoid such views. I created a one simple view with only one statement:
This view I rendered then from all places where I needed such a simple view.
In CakePHP 1.2, “loadHelper(‘NameOfHelper’)” is depreciated. Use “App::import(‘Helper’,'NameOfHelper’)” instead.
@mjamesd: Thanks for the hint, it is now fixed in the article!
Damn, you save my life with this article, and I agree with bparise, global helper will be very helpful. Thanks Daniel
@maulana: Cool to hear that :) And yes, it would be useful to have global helpers.
Just curious, is your CMS realized with Cake?
for the english version check this link:
http://translate.google.com/translate?hl=en&u=http%3A%2F%2Fm3nt0r.de%2Fblog%2F2007%2F08%2F12%2Fcakephp-helpercomponent%2F&sl=de&tl=en
@rrd: Thanks for the link!
Thanks again for the help. I agree with the original premise of helpers though – you shouldn’t be doing anything that the user sees in the controller (at least not finally).
The problem for me came in the form of AJAX requests, which forwarded some controller generated JSON.
@Brian: I’m glad to hear this article was helpful for you!