Howto extend default controller - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Howto extend default controller (/showthread.php?tid=71989) |
Howto extend default controller - wjonker - 10-23-2018 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 . RE: Howto extend default controller - jreklund - 10-23-2018 Does your normal Controller read? class Menu extends MY_Controller RE: Howto extend default controller - wjonker - 10-25-2018 jreklund. No, it did not. It was stil extending the default CI controller. Thnx! RE: Howto extend default controller - neuron - 10-25-2018 In your Controllers that extends MY_Controller: PHP Code: public function __construct() 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 |