codeigniter框架扩展核心类---实现前台后台视图的分离
1. 扩展核心类,主要作用就是扩展系统现在的功能。
为前台增加独立的视图文件夹:
a. 自定义路径常量 :在application ->config/ constants.php中增加
/*my constant*/ define('THEMEES_DIR','themes/');
b. 在application文件夹的core中自定义MY_Loader.php
/*
分析核心类中自动加载的功能的实现方式
*/
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Loader extends CI_Loader { protected $_theme = 'default/'; public function switch_themes_on() { $this->_ci_view_paths = array(FCPATH.THEMEES_DIR.$this->_theme => TRUE); var_dump($this->_ci_view_paths); } public function switch_themes_off() { } }
c. 自定义控制器:实现前台后台视图分离
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Home_Controller extends CI_Controller { public function __construct() { parent::__construct(); $this->load->switch_themes_on(); } } class Admin_Controller extends CI_Controller { public function __construct() { parent::__construct(); $this->load->switch_themes_off(); } }
d. 定义控制器类,继承相应的Home_controller,admin_controller控制器。 在admin子控制器文件夹中,
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Hello extends Admin_Controller { function index() { $data['title'] = "this is title."; $data['content'] = "this is content."; $this->load->view('hello.php',$data); } }
定义相应的视图文件夹及文件view /admin /hello.php
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> </body>admin <?php echo $content; ?> </html>
前台独立文件夹 /themes/default
控制器themes.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Themes extends Home_Controller { function index() { $data['title'] = "this is title."; $data['content'] = "this is content."; $this->load->view('themes.php',$data); } }
注:系统仅支持一层子文件夹,因此需要扩展核心类,实现多层视图文件,及放置到其它独立文件夹中。