Muestra las diferencias entre dos versiones de la página.
— |
helpers [2016/12/30 18:42] (actual) |
||
---|---|---|---|
Línea 1: | Línea 1: | ||
+ | ====== 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. | ||
+ | <code php| hello.php> | ||
+ | <?php | ||
+ | //This is a really simple example of a helper, this is more like ridiculous | ||
+ | echo "Hello, World!"; | ||
+ | ?> | ||
+ | </code> | ||
+ | You can now use this helper in your controllers, views, and models like so: | ||
+ | <code php| maincontroller.php> | ||
+ | <?php | ||
+ | namespace Controller; | ||
+ | use Salem\load; | ||
+ | class maincontroller { | ||
+ | public function index() { | ||
+ | load::helper('hello'); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | ?> | ||
+ | </code> | ||
+ | |||
+ | 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: | ||
+ | <code php| config.php> | ||
+ | /* Auto Load Helpers */ | ||
+ | Config::set('autoload_helper',array("hello")); | ||
+ | </code> | ||
+ | It is also possible to load helpers on a controller-by-controller way by adding the $autoload_helpers array to the controller class. | ||
+ | <code php| maincontroller.php> | ||
+ | <?php | ||
+ | namespace Controller; | ||
+ | use Salem\load; | ||
+ | class maincontroller | ||
+ | { | ||
+ | public $autoload_helpers = array('hello'); | ||
+ | |||
+ | // ... More code | ||
+ | } | ||
+ | ?> | ||
+ | </code> | ||
+ | ---- | ||
+ | [[documentation|Go back to Documentation]] | ||