CodeIgniter: Create new helper?
Clash Royale CLAN TAG#URR8PPP
CodeIgniter: Create new helper?
I need to loop lot of arrays in different ways and display it in a page. The arrays are generated by a module class. I know that its better not to include functions on 'views' and I want to know where to insert the functions file.
I know I can 'extend' the helpers, but I don't want to extend a helper. I want to kind of create a helper with my loop functions.. Lets call it loops_helper.php
I can, but that misses the point of sepparating the view from the functions..
– Jonathan
Apr 29 '09 at 22:37
7 Answers
7
A CodeIgniter helper is a PHP file with multiple functions. It is not a class
Create a file and put the following code into it.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('test_method'))
function test_method($var = '')
return $var;
Save this to application/helpers/ . We shall call it "new_helper.php"
The first line exists to make sure the file cannot be included and ran from outside the CodeIgniter scope. Everything after this is self explanatory.
Using the Helper
This can be in your controller, model or view (not preferable)
$this->load->helper('new_helper');
echo test_method('Hello World');
If you use this helper in a lot of locations you can have it load automatically by adding it to the autoload configuration file i.e. <your-web-app>applicationconfigautoload.php
.
<your-web-app>applicationconfigautoload.php
$autoload['helper'] = array('new_helper');
-Mathew
Always make use that the helper file name is appended with "_helper" otherwise you will get an error. So "helper_name" wont work but name your file "name_helper".
– Bhumi Singhal
Dec 11 '12 at 10:06
As of CI2, you will also need to get the CI instance in order to use a helper within a model: $ci = get_instance(); $ci->load->helper(’name_helper’);
– Evernoob
Apr 24 '13 at 9:36
Just a note, The helper doesn't have to be a function. It can be a class as well. For instance, check out the strategy to create "Widgets" over at EllisLab's Forum. Then you can use that class anywhere... also Techincally, you could load your helper into the CI instance if you desire to by doing getting the instance and then setting
$this
as a property of it... All if you want to of course.– General Redneck
Apr 26 '13 at 15:24
$this
What is the use of the if function_exists check? Is it a code igniter thing, is it a general php good practice thing? Why would you need it?
– skrln
Apr 14 '14 at 9:33
@skrln there's a chance that you have a helper auto-loaded (let's call it
cool_helper
) and, if you forget that and manually call $this->load->helper('cool_helper')
after it was auto-loaded, you will get an "already defined" PHP error or something. This prevents from loading the same code twice (it's something like the include_once()
PHP function but for CodeIgniter helpers, to avoid problems). Basically, translating a little: if function doesn't exist, it means the helper wasn't loaded before. Let's define it.– Alejandro Iván
May 15 '14 at 16:03
cool_helper
$this->load->helper('cool_helper')
include_once()
Some code that allows you to use CI instance inside the helper:
function yourHelperFunction()
$ci=& get_instance();
$ci->load->database();
$sql = "select * from table";
$query = $ci->db->query($sql);
$row = $query->result();
thank you for showing how to use the CI instance.
– IEnumerator
Jan 28 '10 at 3:10
no worries mate
– r4ccoon
Jan 30 '10 at 5:59
WOW that's useful thanksss :)
– Aditya M P
Mar 12 '11 at 10:13
@r4ccoon, so right. Didn't even take me 5 mins to come back for the instance definitions. :)
– user1048839
Dec 31 '14 at 14:01
@r4ccoon, and that is a fantastic tip
– Arun
Mar 4 '16 at 11:44
Well for me only works adding the text "_helper"
after in the php file like:
"_helper"
And to load automatically the helper in the folder aplication -> file autoload.php add in the array helper's the name without "_helper" like:
$autoload['helper'] = array('comunes');
And with that I can use all the helper's functions
I just discovered that like you said, CodeIgniter will require
_helper
at the end of the file name.– Jared Eitnier
May 30 '13 at 20:48
_helper
+1 for using NetBeans :)
– Ing. Michal Hudak
Nov 8 '13 at 12:21
@Jared, yes, it was also the case for me, it requires the _helper at the end of the file name!
– pollux1er
Sep 19 '14 at 9:52
To create a new helper you can follow the instructions from The Pixel Developer, but my advice is not to create a helper just for the logic required by a particular part of a particular application. Instead, use that logic in the controller to set the arrays to their final intended values. Once you got that, you pass them to the view using the Template Parser Class and (hopefully) you can keep the view clean from anything that looks like PHP using simple variables or variable tag pairs instead of echos and foreachs. i.e:
blog_entries
<h5>title</h5>
<p>body</p>
/blog_entries
instead of
<?php foreach ($blog_entries as $blog_entry): ?>
<h5><?php echo $blog_entry['title']; ?></h5>
<p><?php echo $blog_entry['body']; ?></p>
<?php endforeach; ?>
Another benefit from this approach is that you don't have to worry about adding the CI instance as you would if you use custom helpers to do all the work.
Create a file with the name of your helper in /application/helpers and add it to the autoload config file/load it manually.
E.g. place a file called user_helper.php in /application/helpers with this content:
<?php
function pre($var)
echo '<pre>';
if(is_array($var))
print_r($var);
else
var_dump($var);
echo '</pre>';
?>
Now you can either load the helper via $this->load->helper(‘user’);
or add it to application/config/autoload.php config.
$this->load->helper(‘user’);
Just define a helper in application helper directory
then call from your controller just function name like
helper name = new_helper.php
function test_method($data)
return $data
in controller
load the helper
$this->load->new_helper();
$result = test_method('Hello world!');
if($result)
echo $result
output will be
Hello World!
To retrieve an item from your config file, use the following function:
$this->config->item('item name');
Where item name is the $config array index you want to retrieve. For example, to fetch your language choice you'll do this:
$this->config->item('item name');
$lang = $this->config->item('language');
The function returns FALSE (boolean) if the item you are trying to fetch does not exist.
$lang = $this->config->item('language');
If you are using the second parameter of the $this->config->load function in order to assign your config items to a specific index you can retrieve it by specifying the index name in the second parameter of the $this->config->item() function. Example:
// Loads a config file named blog_settings.php and assigns it to an index named "blog_settings"
$this->config->load('blog_settings', TRUE);
// Retrieve a config item named site_name contained within the blog_settings array
$site_name = $this->config->item('site_name', 'blog_settings');
// An alternate way to specify the same item:
$blog_config = $this->config->item('blog_settings');
$site_name = $blog_config['site_name'];
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why can't you just pass those arrays to the view?
– Mike Hordecki
Apr 29 '09 at 22:07