The CI framework extends the method analysis of system core classes and the ci framework. This document describes how to extend the core class of the CI framework. I will share with you for your reference. The details are as follows: first, you will analyze the method of extending the system core class in the system CI framework, and then the ci framework.
This article describes how to extend the core class of the CI framework. We will share this with you for your reference. The details are as follows:
First, your system extension class is placed under application/core. Originally, the system core class is CI_Controller, so you cannot start with CI _. you need to open application/config. php to modify it:
$config['subclass_prefix'] = 'MY_';
Your prefix!
Some public modules can unmount their core classes!
These days, I read Dilicms (a lightweight background architecture). For example, his background extension class is like this:
<? Php if (! Defined ('basepath') exit ('no direct script access allowed '); abstract class Admin_Controller extends CI_Controller {/*** _ admin * save the information of the current logon user ** @ var object * @ access public **/public $ _ admin = NULL; /*** constructor ** @ access public * @ return void */public function _ construct () {parent ::__ construct (); $ this-> load-> library ('Session'); $ this-> settings-> load ('backend'); $ this-> load-> switch_theme (s Etting ('backend _ theme '); $ this-> _ check_login (); $ this-> load-> library ('acl '); $ this-> load-> library ('plugin _ manager ');} /*** check whether the user is logged on ** @ access protected * @ return void */protected function _ check_login () {if (! $ This-> session-> userdata ('uid') {redirect (setting ('backend _ access_point '). '/login');} else {$ this-> _ admin = $ this-> user_mdl-> get_full_user_by_username ($ this-> session-> userdata ('uid '), 'uid'); if ($ this-> _ admin-> status! = 1) {$ this-> session-> set_flashdata ('error', "this account has been frozen. please contact the administrator! "); Redirect (setting ('backend _ access_point '). '/login ');}}} /*** load view ** @ access protected * @ param string * @ param array * @ return void */protected function _ template ($ template, $ data = array ()) {$ data ['tpl'] = $ template; $ this-> load-> view ('sys _ entry ', $ data );} /*** check permissions ** @ access protected * @ param string * @ return void */protected function _ check_permit ($ action = '', $ folder = ''){ If (! $ This-> acl-> permit ($ action, $ folder) {$ this-> _ message ('sorry, you have no permission to access this site! ', '', FALSE );}} /*** message ** @ access public * @ param string * @ param bool * @ param string * @ return void */public function _ message ($ msg, $ goto = '', $ auto = TRUE, $ fix ='') {if ($ goto = '') {$ goto = isset ($ _ SERVER ['http _ referer'])? $ _ SERVER ['http _ referer']: site_url ();} else {$ goto = strpos ($ goto, 'http ')! = False? $ Goto: backend_url ($ goto);} $ goto. = $ fix; $ this-> _ template ('sys _ message', array ('MSG '=> $ msg, 'Goto' => $ goto, 'auto' => $ auto); echo $ this-> output-> get_output (); exit ();}}
It puts some public information into the verification, login, and jump in Admin_Controller, so I also performed this operation when writing the front-end, extending the Font_Controller:
Abstract class Font_Controller extends CI_Controller {public function _ construct () {parent ::__ construct () ;}// the front-end loading View method public function _ template ($ template, $ data = array () {$ data ['Nav'] = $ this-> get_nav (); $ data ['tpl'] = $ template; $ this-> load-> view ('default', $ data);} // public navigation information public function get_nav () {}// public link information public function get_friendlink (){}}
Note: Some frequently used tools and functions can be placed in helper!