25

I have created a few utility functions like one h() that acts as echo htmlentities($var) . I want theses functions to be available everywhere . so where do i put it.

aWebDeveloper
  • 36,687
  • 39
  • 170
  • 242
  • 3
    Create a helper that is autoloaded via config. I'm still quite green with CI but instinctively this is how I would do it. – David Barker May 31 '12 at 09:52
  • Upto now you got that, helpers file is the answer, then you should mark any one answers from below as a correct answer. (just a suggestion) – TechCare99 May 31 '12 at 11:01

4 Answers4

57

As @david barker said, you might use an helper. Create a file named, for example, "site_helper", that contains all the functions.

be aware you need to check if the function already exists, though, or you'll get an "function already declared" error.

So, something like:

file site_helper.php (in application/helpers/ )

if(!function_exists('h'))
{
  function h($value)
  {
   return htmlentities($value);
  }
}

if(!functin_exists('other_function')
//....etc.

And then you can autoload it in config/autoload.php:

$autoload['helpers'] = array('site');

^-- note how you only use the part before the underscore to call the file. Also, helpers are not classes, but a collection of functions.

Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
  • 3
    This worked perfectly for me but I needed to change $autoload['helpers'] to $autoload['helper'] – Dave Mar 08 '14 at 06:43
  • Note that you cannot use models (or any other `$this` features) from a helper, however you could make a model and autoload it. Inside models you are allowed to use other models. – Kevin Van Ryckegem Mar 11 '16 at 21:02
  • Remove the "s" from "helpers". Just write `$autoload['helper'] = array('site');` . I am using codeigniter 3. – Nesar Ahmad Nori Feb 24 '23 at 13:50
7

You should include your global variables file in your /application/config/constants.php file. Then move your global function file to /application/helpers folder. Then you should autoload the global functions file. /application/config/autoload.php

$autoload['helpers'] = array('your-global-function-file.php');

I would suggest not moving anything inside of the system folder project, as upgrading would be an absolute nightmare. Sometimes refactoring your code to conform to CI logic, maybe faster in the long run rather than trying to copy/paste stuff all over the place.

Kara
  • 6,115
  • 16
  • 50
  • 57
Shiven
  • 206
  • 1
  • 9
  • 17
  • Sometimes it's even better to keep them in the system folder for some small projects that are created and run without updates. Especially it is useful if you have one system folder and use it for 2+ applications, like backend, frontend, etc., so they will always have same helpers and you can use your custom functions in every part of the project without copy/paste, that makes it more manageable. – Sergey Telshevsky May 31 '12 at 17:22
4

This is exactly what Helpers are for.

Create a new helper (remember to append it with _helper.php) and put it in your helper folder.

You can either auto load it in your config (so you can use it anywhere), or just manually load it when needed.

Laurence
  • 58,936
  • 21
  • 171
  • 212
3

Use codeigniter-kint and a custom file for global Functions. This will give you a more helpful and beautiful Output like: enter image description here

File: application/helpers/globalfunctions_helper.php

<?php
    /**
     * @file Global Helper Functions
     * 
     */

    if(!function_exists('pr'))
    {
      function pr($value=false){
       $CI = get_instance();
       $CI->load->library('kint');
       return @Kint::dump($value);   
      }
    }
    if(!function_exists('dp')){
      function dp($value=false)
      {
       $CI = get_instance();
       $CI->load->library('kint');
       Kint::dump($value);
       die();
      }
    }

See also: http://raveren.github.io/kint/

digitaldonkey
  • 2,431
  • 1
  • 20
  • 16