Codeigniter: Create New Helper

CodeIgniter: Create new helper?

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>\application\config\autoload.php.

$autoload['helper'] = array('new_helper');

-Mathew

How to create and load new helper in CodeIgniter 4

It's not mentioned in documents but remember to add a suffix _helper to filename of your helper otherwise it will not work in codeigniter 4.

For example if you have created a helper xxx.php, change it to xxx_helper.php.

To load a helper you can use helper function (Like: helper('xxx.php');) or add it to $helpers array that is an protected property in BaseController

How to create helper in codeigniter?

You can only use "MY_" prefix when CodeIgniter has built-in same-name helper (ajax_helper).
Please change the file name "my_ajax_helper.php" into "ajax_helper.php" and use $this->load->helper('ajax');

The "MY_" prefix is only use to extend CI's built-in core helper (same rule on controller, model, etc.),
for example, you can extend url_helper with my_url_helper, and load it by $this->load->helper('url');, not 'my_url', but you cannot create you own helper with this prefix.

Update:
Oops, sorry I found my answer maybe is wrong, CI could load custom helper with 'my_ajax' in my test. Maybe another probably reason is the file / folder permission?

Update: I checked CI's code and found it only output this message when file_exists() is return false, it means helper file not exists. So problem maybe cause by the file name, path, or letter case of ajax_helper.php.

Code Igniter Custom Helper

Using $this when not in object context

This simply means you can not use the $this keyword outside of an object(class),
as @Kryten pointed out.

Helpers are generally only used for embedding in html, such as formatting data.

<p><?php echo formatHelper(escape($var)); ?></p>

What you need to do is read up a little on creating a Library.

Creating my own Helper in Codeigniter

You need to assign function return value to the variable:

$result = percentage($num1, $num2);

so you can print it:

print_r($result);

codeigniter new helper is not load

Let's see what des doc says :

Loading a helper file is quite simple using the following function:
$this->load->helper('name');

Where name is the file name of the helper, without the .php file
extension or the "helper" part.

For example, to load the URL Helper file, which is named
url_helper.php, you would do this: $this->load->helper('url');

It clearly suggests that CI will look for a file named xxx_helper.php.
In you case, when load getdata, CI will look for getdata_helper.php inside application/helpers .

You just have to rename your file to make it work.



Related Topics



Leave a reply



Submit