Home > Backend Development > PHP Tutorial > php ci develop basic applications

php ci develop basic applications

WBOY
Release: 2016-08-08 09:19:30
Original
1257 people have browsed it

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:

php ci develop basic applications

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访问地址,区分
Copy after login

4. Set the default access controller, config/routes.php:

$route['default_controller'] = 'home';        //默认controller名称
$route['404_override'] = '';                 //404
$route['translate_uri_dashes'] = TRUE;
Copy after login

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();
	}
}
Copy after login

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目录,主要用来放模板的
Copy after login

6.2layouts are mainly templates for the entire page:

php ci develop basic applications

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一个
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template