Helpers

What Are They? Helpers are PHP files with little (or big) snippets of code that you can use anywhere in your application. Helpers are commonly PHP files, with classes, methods and more.

Note: It is highly recommended that you use models instead of helpers whenever possible. However, if you need to use a helper to include a new behavior that is not supported by the core or the extended libraries of the framework.

Helper Example Create a new PHP file at application/helpers/hello.php. This will be your helper and as all yours helpers they will be located in the application/helpers folder.

hello.php

<?php
//This is a really simple example of a helper, this is more like ridiculous
echo "Hello, World!";
?>
You can now use this helper in your controllers, views, and models like so:

maincontroller.php

<?php
namespace Controller;
use Salem\load;
class maincontroller {
	public function index() {
		load::helper('hello');	
	}
}
 
?>

The above would display “Hello, World!”.

Auto Loading If you are going to use a particular helper a lot, then you should consider auto loading it by adding it to the autoload_helpers array in /application/config/config.php like so:

config.php

/* Auto Load Helpers */
Config::set('autoload_helper',array("hello"));
It is also possible to load helpers on a controller-by-controller way by adding the $autoload_helpers array to the controller class.

maincontroller.php

<?php
namespace Controller;
use Salem\load;
class maincontroller
{
    public $autoload_helpers = array('hello');
 
    // ... More code
}
?>


Go back to Documentation