Codeigniter - using the helper in the controller does not work

I need to use a function in multiple controllers. Therefore, I though about using a user assistant, but it seems I can not get it to work. (It works in the view, but I need it in the controller)

This gives me the following fatal error:

Fatal error: method call undefined Developers :: checkIfLoggedIn () in /application/controllers/developers.php on line 12

Is this a smart way to use an assistant to use a function in multiple controllers, or should I do it differently.

Thanks Advance,
Mark

EDIT:
Controller File:

if (!defined('BASEPATH')) exit('No direct script access allowed'); class Developers extends CI_Controller { public function __construct() { parent::__construct() $this->load->helper('form'); $this->load->helper('url'); $this->load->helper('login'); //helper function checkIfLoggedIn($this->session->userdata('loggedIn')); } } 

Helper File:

 if (!defined('BASEPATH')) exit('No direct script access allowed'); if (!function_exists('checkIfLoggedIn')) { function checkIfLoggedIn($session_loggedIn) { $loggedIn = $session_loggedIn; if($loggedIn == false) { redirect('login/'); } } } 

}

+4
source share
4 answers

In your controller, you are using it incorrectly, it is not a controller method, so you cannot use $this to call it.

The helper can be loaded anywhere in your controller functions (or even in your View files, although this is not a good practice) since when you load it before using it. You can load your helpers into your controller constructor so that they become available automatically in any function, or you can load a helper in a specific one that needs it.

To download the assistant you can use

 $this->load->helper('name'); // name is the name of the file without extention 

Unlike most other systems in CodeIgniter, helpers are not written in an object-oriented format. These are simple, procedural functions. each auxiliary function performs one specific task, independent of other functions.

So, to call a helper function in the controller, you should not use

 $this->function_name(); 

use instead

 function_name(); 

For example, if you have a helper function in a helper file named myCustomHelper.php as follows

 function myHelper() { // code } 

then you can load it into the controller and call it as follows

 $this->load->helper('myCustomHelper'); myHelper(); // call the function 

but it’s better to load the helpers in the constructor so that it is accessible through the whole script.

Update: If your auxiliary file name is login_helper.php , you can use it in your controller as follows

 $this->load->helper('login_helper'); checkIfLoggedIn($this->session->userdata('loggedIn')); 

More details here .

+16
source

Well, I know this question was asked 5 months ago, but maybe some people find it useful. I had the same problem and found that the name of my helper function was the name of a file that CodeIgniter already used.

So, if you do not receive a warning: “The requested file could not be loaded”, but instead receive a warning: “Fatal error: calling the undefined function [function_name], you are probably using a file name that already exists initially.

+4
source

Instead, create a library class and define your function there. Then load the library into the controller and call the library function. You can load the library on any controller and use its methods.

0
source

Regarding the related question: be careful when setting $ this-> load to __constructor: in PHP you must then explicitly call parent :: __ construct (); otherwise, the parent constructor will no longer be called, leaving $ this-> load undefined. The above solution does it right, but is easy to overlook.

 class My extends CI_Controller { public function __construct() { parent::__construct(); $this->load->helper('url'); } 

If you do not, you will receive an error message that My :: $ load does not exist and the helper does not load.

0
source

All Articles