I need to use a function in multiple controllers. So I though about using a custom helper, but it seems I can't get it to work. (It works in the view, but I need it in the controller)
It gives me following Fatal Error:
Fatal error: Call to undefined method Developers::checkIfLoggedIn() in /application/controllers/developers.php on line 12
Is it a smart move to use a helper to use a function in multiple controllers, or should I do it otherwise.
Thanks in 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/');
}
}
}
}
Instead create a library class and define your function there. Then load the library in the controller and call the function of library. You can load library in any controller and use its methods.
Ok, I know this question has been asked 5 months ago but maybe some people will find this useful. I had just had the same problem and discovered that the filename of my helper functions was a filename that was already used by CodeIgniter.
So if you don't get the warning: 'Unable to load the requested file', but instead get the warning: 'Fatal error: Call to undefined function [function_name], you're probably using a filename that already exists natively.
Helpers are the ideal way to declare global functions in codeigniter and what you are doing is correct. Following are just few points that will help you.
Load all helpers through single line instead of separate lines.
If you want to make it global across all the controller files, then you can put it in the autoload.php file located in '
config/
' directory. Update the$autoload['helper']
variable as follows:Source: How to create & call global PHP functions in CodeIgniter
In your controller you are using it in wrong way, it's not a method of controller so you can't use
$this
to call it.To load a helper you can use
So, to call a helper function in a controller you should not use
instead use
For example if you have a helper function in a helper file named
myCustomHelper.php
as followsthen you can load it in the controller and call it as follows
but it's better to load helpers in the constructor so it'll be available through the whole script.
Update: If your helper file's name is
login_helper.php
then you can use it in your controller as followsRead more here.
On a related matter: caution when putting $this->load in the __constructor: in PHP you must then then explicitly call parent::__construct(); otherwise the parent constructor is no longer called, leaving $this->load undefined. The above solution does it correctly, but it's easy to overlook.
If you don't do this, you'll get the error message that My::$load does not exist and the helper will not load.