• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Howto extend default controller

#1
I want to extend the default controller, in order to enforce login. I did the following

- Created a file in application/core/MY_Controller.php
- Filled it as bellow
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Controller extends CI_Controller{

   public function __construct() {
        parent::__construct();
        
        if (!$this->ion_auth->logged_in())
        {
            // redirect them to the login page
           redirect('auth/login', 'refresh');
           exit;
       }
   }  
}


Somehow, the code in __construct is not executed. I also do not get any error. I can see that the MY_Controller.php file is read. When I put plain text in the file, this plain text is shown as output in the browser.

I hope somebody is able to help me Smile.
Reply

#2
Does your normal Controller read?

class Menu extends MY_Controller
Reply

#3
jreklund. No, it did not. It was stil extending the default CI controller. Thnx!
Reply

#4
In your Controllers that extends MY_Controller:
PHP Code:
   public function __construct() 
 
   {
 
       parent::__construct(); // put it in first line in your controller

        //other codes

 
   


If child class has constructer method it overrides parent constructer method that means parent constructer will not run.
If child class does not have constructer method than parent constructer method will run.
If child class has constructer and you also want parent constructer to run your should put parent::__construct();
in constructer method of child class
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.