CI3如何使用controller继承

这两天看了下CI3,想使用controller继承,后来才发现CI的继承是另有玄机,这里做个笔记。

1、检查application\config\config.php文件

$config['subclass_prefix'] = 'MY_';

值为My_,那么你的controller需要以这个开头

2、在application\core文件夹下面新建My_Controller.php文件

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
*My_Controller
**/
class My_Controller extends CI_Controller{
	public function __construct(){
		parent::__construct();
	}
}

?>

3、在你的controllers中使用它

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends My_Controller{	
    public function __construct(){
	parent::__construct();
	$this->load->library('theme');
    }
}		
?>



备注:如果需要扩展多个继承,那么需要在application\core\My_Controller.php文件中书写多个controller

<?php
	defined('BASEPATH') OR exit('No direct script access allowed');
	/**
	*基础controller
	**/
class My_Controller extends CI_Controller{
	public function __construct(){
		parent::__construct();
	}
}

class BaseController extends My_Controller{
	public function __construct(){
		parent::__construct();
	}
}

class AdminController extends My_Controller{
	public function __construct(){
		parent::__construct();
	}
}

?>


你可能感兴趣的:(codeigniter3)