What is a helper?

Helper is simply a collection of functions in a particular category to help you with tasks. There are HTML helpers, that assist in creating links, Form helpers that help you create form elements, Text helpers perform various text formatting routines, etc.

Creating helpers

In order to function, a helper must do the following:

  • Reside in "application/helper" folder (or a sub-folder).
  • The class name must have each word capitalized.
  • All methods in a helper must be static.
  • Must have the Helper class as a (grand)parent.

Some examples of helper names and file locations:

// application/helpers/events.php
class Events_Helper extends Helper {
// application/helpers/news.php
class News_Helper extends Helper {

Helpers can be in sub-folders, note that class names can be anything you like do not have to match folder or file names:

// application/helpers/example/events.php
class Events_Helper extends Helper {
// application/helpers/news/news.php
class News_Helper extends Helper {

Example helper

Typical helper would have the code to perform basic common tasks:

class News_Helper extends Helper
	static public function getNews()
		$entries = self::instance()->news_model->getEntries('in_list');
		view::assign(array('entries' => $entries), '', 'news/helpers/news');
		return view::load('news/helpers/news', array(), 1);

Loading helpers

Helper can be loaded from anywhere in the software. Once loaded, it becomes globally available:

echo news_helper::getNews();

Accessing objects

Since helpers are static, you may access models, libraries and other objects that are normally available in the controller like this:

// Instead of using $this you may use self::instance()

Core helpers

There are several core helpers that are included as part of the core framework. These helpers are located in the "system/helpers" folder. They can be accessed the same way as in the examples above.

Extending core helpers

It is possible to extend core libraries by creating files with the same name and placing them in the "application/helpers" folder. For example to extend "Text" helper, we would create a file called "text.php" in that folder and place the following code in it (notice the name of the helper we extend):

class Text_Helper extends CodeBreeder_Text_Helper
	public static function decodeEntities($str)
		return html_entity_decode($str, ENT_QUOTES, config::item('charset'));