Specifying helpers for static pages

Published on and tagged with cakephp  controller  helper

In a recent comment I got asked how you can reference Javascript files from static pages (i.e. from files in app/views/pages) as “you can’t add the JavaScript helper, because there isn’t a controller”.

Well, the assumption that there is no controller is wrong. There is one, the PagesController, though by default it is “hidden” in cake/libs/controller (unless you baked your project with the bake script, then you will also find a PagesController in app/controllers).

And this means you can specify the helpers you want to use in your static pages in the usual way: by adding them to the $helpers array of the PagesController resp. the AppController (app/app_controller.php), if you want to use the helpers in the views of more than one controller. Please do not edit the PagesController in the cake/libs/controller directory, but copy it to app/controllers and make the changes there.

© daniel hofstetter. Licensed under a Creative Commons License