Welcome Guest, Not a member yet? Register   Sign In
custom helper function
#1

Hi all,

I have problem that, i am using custom helper function under controller class  __construct() method. is it correct way to use that function is there ?

PHP Code:
public function __construct()
{
    if(
is_user_login()){ // function define in custom_helper.php
        
redirect('');
    }
    
parent::__construct();


and also custom_helper.php file is autoload.

NOTE: is_user_login() function works with other methods but not work with construct method.

I am curious about know reason behind that, or if it is possible then how?

Thank in advance.
Reply
#2

You should always call the parent constructor first.

PHP Code:
public function __construct()
{
 
   parent::__construct();

 
   // function define in custom_helper.php
 
   if (is_user_login())
 
   {
 
       redirect('');
 
   }


Hope that helps.

Also you really only need a constructor if you are setting variables and parameters.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(04-19-2018, 03:47 AM)InsiteFX Wrote: You should always call the parent constructor first.

PHP Code:
public function __construct()
{
 
   parent::__construct();

 
   // function define in custom_helper.php
 
   if (is_user_login())
 
   {
 
       redirect('');
 
   }


Hope that helps.

Also you really only need a constructor if you are setting variables and parameters.

I loaded that file in autoload.php under helper.
Reply
#4

(04-19-2018, 04:01 AM)dhaval Wrote: I loaded that file in autoload.php under helper.

And autoload.php is executed by CI_Controller, the "parent" of your controller.
That's why you must call parent::__construct(); first in the __construct() method of your controllers.

(Essentially the same answer I gave for your Stackoverflow question.)
Reply
#5

Solved. Thank you all.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB