Welcome Guest, Not a member yet? Register   Sign In
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

(This post was last modified: 10-25-2018, 01:59 AM by neuron.)

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




Theme © iAndrew 2016 - Forum software by © MyBB