Kin
Kin

Reputation: 4596

How to create helper in codeigniter?

I just created file /CodeIgniter/application/helpers/ajax_helper.php and now want to use it's functions in CodeIgniter/application/controllers/Ajax.php so i use:

function __construct(){
    parent::__construct();
    if((bool)$this->session->userdata('logged_in'))
        $this->load->helper('ajax');
    else
        return FALSE;
}

But error occurred:

Unable to load the requested file: helpers/ajax_helper.php

What is wrong?

Upvotes: 0

Views: 1362

Answers (2)

hit1205
hit1205

Reputation: 141

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.

Upvotes: 1

Jeremie Ges
Jeremie Ges

Reputation: 2745

The helper "ajax" doesn't exist in system of codeigniter, so first you can rename your filename like ajax_helper.php, second use :

 function __construct(){
parent::__construct();
if((bool)$this->session->userdata('logged_in'))
    $this->load->helper('ajax_helper');

else
    return FALSE;
}

Upvotes: 0

Related Questions