1. Build a ci project, http://codeigniter.org.cn/p Go to this website to download a ci framework. Currently I am using 3.0.0. (There are helpful instructions on the website)
2. After deleting some unused files and directories, as shown below:
R&D is mainly in the application directory, and the system directory is the framework directory. The public directory was newly created by me and is mainly used to store js, css, and images.
3.ci basic configuration, referenced by cssjsimages, added a system variable in config.php in the config directory:
$config['asset_url'] = "http://127.0.0.1/"; //js\css\images访问地址,区分
4. Set the default access controller, config/routes.php:
$route['default_controller'] = 'home'; //默认controller名称 $route['404_override'] = ''; //404 $route['translate_uri_dashes'] = TRUE;
5. Controller inheritance Base class methods. Create a new MY_Controller.php file in the core directory (can be downloaded directly from the blog). The code is as follows:
<?php class MY_Controller extends CI_Controller{ function __construct(){ parent::__construct(); } } /** * 后台控制器基类方法,只要继承AdminBase * $this->uri->segment(1) 获取控制器名称 * $this->uri->segment(2) 获取action名称 */ class AdminBase extends MY_Controller{ public $skip_filter = array(); //array( 'controller_name', 'controller_name' ) 不用登录的页面 public $data = array(); function __construct() { parent::__construct(); $this->data['infos'] = array(); //传递给前台页面的变量,变量名为infos $this->load->library('session'); $this->load->library('layout'); //加载模板文件 libraries目录下 if( !is_login() ){ redirect('login/index'); //如果未登录跳转到登录页 } } /**过滤方法*/ public function is_login(){ if( in_array($this->uri->segment(1), $skip_filter) ){ return true; } //无需登录的控制器 if( $this->session->has_userdata('user_id') ){ //判断是否登录 //方法, 判断用户是否被封 return true; }else{ return false; } } } /*** 前面控制器基类方法,只要继承HomeBase**/ class HomeBase extends MY_Controller{ function __construct(){ parent::__construct(); } }
6.1 How to reference the layout template. Create a new Layout.php file in the libraries directory. You can download it at the bottom of the blog. Pay attention to the places to be modified:
$this->layout = 'layouts/' . $layout; // 'layouts/' layouts为views目录,主要用来放模板的
6.2layouts are mainly templates for the entire page:
If it starts with an underscore, we are the partial code; if it starts with a letter, we are the layout of the entire page; (Features: No Public files need to be called repeatedly, it is convenient to modify public files, the layout is clear, etc.).
index.php code is as follows:
<div> <?php $this->load->view('layouts/_top_menu'); //头部开始 $this->load->view('layouts/_left_menu'); //左边菜单开始 ?> <!--右边框架开始--> <div> <div name="code">//$this->load->view('info/index', $this->data); //未用模板文件 $this->layout->view('info/index', $this->data); //引用模板的写法 $this->data为传到view层能用的变量,如:$this->data = array('infos'=>array(), 'page'=>1) 表示:有两个变量,infos一个,page一个
The above introduces the basic application of PHP ci development, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.