码迷,mamicode.com
首页 > 其他好文 > 详细

ci学习

时间:2016-08-10 19:10:07      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

<?php 
#访问配置
1.遇到无论什么URL 只能访问默认页面.  强制在你的 URL 中添加一个问号,要做到这点,打开 application/config/config.php 文件

2.访问方式  application/config.php 配置文件中启用它
$config[‘enable_query_strings‘] = FALSE; //index.php/controller/method/id/33   TRUE  index.php?c=controller&m=method&id=44
$config[‘controller_trigger‘] = ‘c‘;
$config[‘function_trigger‘] = ‘m‘;

3.控制器多级目录 如何命名  (类名与文件名一致,目录小写控制器名大写)

CLI方式执行 
php index.php tools message "John Smith"   #tools控制器 message方法被执行  并传一个参数

 
开启自动加载
application/config/config.php 配置文件中的 $config[‘composer_autoload‘] 设置为 TRUE
	只能自动加载如下目录文件
	libraries/ 目录下的核心类
	helpers/ 目录下的辅助函数
	config/ 目录下的用户自定义配置文件
	system/language/ 目录下的语言文件
	models/ 目录下的模型类
 
 
钩子hooks - 扩展框架核心
启用钩子:  application/config/config.php 文件 $config[‘enable_hooks‘] = TRUE;
定义钩子:application/config/hooks.php 文件中  参考 http://codeigniter.org.cn/user_guide/general/hooks.html
$hook[‘pre_controller‘] = array(//挂钩点为 pre_controller表示控制器调用之前执行,所有的基础类都已加载,路由和安全检查也已经完成
    ‘class‘    => ‘MyClass‘, //class 希望调用的类名 喜欢过程式的函数的话,这一项可以留空
    ‘function‘ => ‘Myfunction‘, //希望调用的方法或函数的名称
    ‘filename‘ => ‘Myclass.php‘, //包含类或函数的文件名
    ‘filepath‘ => ‘hooks‘, //脚本文件的目录名
    ‘params‘   => array(‘beer‘, ‘wine‘, ‘snacks‘) //希望传递给你脚本的任何参数,可选
);



#接收参数
$_POST
    $this->input->post(‘title‘)
    $this->input->get()  // 如果希望数据通过 XSS 过滤器进行过滤,将第二个参数设置为 TRUE:如$this->input->get(array(‘field1‘, ‘field2‘), TRUE);
    $this->input->cookie()
    $this->input->server()

redirect(); //跳转


 
 
############控制器############  文件名和类名应该一致 继承CI_Controller
1.控制器 文件名、类名 必须是大写字母开头,如:‘Index.php‘ 。  
2.控制器或者方法命名 不能用 CI_Controller Default  index 等

加载模型: $this->load->model(‘model_name‘); $this->load->model(‘blog/queries‘); 
自动加载:将模型添加到 autoload 数组中
模型层别名:  $this->load->model(‘blog/art‘, ‘art‘);  /* 多级目录不考虑斜线 */
调用模型层:$this->model_name->method(); 或者 $art->method();
 
加载辅助函数  $this->load->helper(array(‘helper1‘, ‘helper2‘, ‘helper3‘) );
自动加载:辅助函数添加到 autoload 数组中
调用:在视图文件中<?php echo anchor(‘blog/comments‘, ‘Click Here‘);?>"Click Here" 链接名称 blog/comments连接到此处
辅助函数扩展: application/helpers/MY_array_helper.php (文件命名注意小写)


加载系统类库 $this->load->library(array(‘email‘, ‘form_validation‘));  初始化时也可以传入参数 $this->load->library(‘someclass‘, $params);
调用类库:$this->form_validation->run() 
类库扩展: application/libraries/Myclass.php 
  (注意自己的类中使用 CodeIgniter 类$CI =& get_instance(); ===> $CI->load->helper(‘url‘);$CI->load->library(‘session‘);)


加载系统驱动器  $this->load->driver(‘cache‘, array(‘adapter‘ => ‘file‘));
加载驱动器  $this->load->driver(‘some_parent‘); (一个父类和任意多个子类。子类可以访问父类, 但不能访问兄弟类)
调用驱动器:$this->some_parent->some_method();
调用驱动器子类:$this->some_parent->child_one->some_method();
扩展:  /application/libraries/Driver_name.php  或者 /application/libraries/Driver_name/Driver_name_subclass_1.php
(注意 驱动器是子驱动器并不继承主驱动器,因此在子驱动器里无法访问主驱动器中的属性或方法。)


 
 
核心类行为修改  替换核心类
创建文件: application/core/Input.php 类名必须以 CI 开头class CI_Input { }
 
扩展核心类
扩展: application/core/Input.php  类名必须以MY_ 开头class MY_Input extends CI_Input { 记得要调用父类的构造函数: }
 (你定义的类必须继承自父类。)


缓存 
开启缓存: $this->output->cache($n);    $n 是缓存更新的时间(单位分钟)。
(注意:只有通过 view 输出的页面才能缓存,修改了可能影响页面输出的配置,需要手工删除掉缓存文件)
删除缓存:$this->output->delete_cache(); 
删除其他地方的缓存 $this->output->delete_cache(‘/foo/bar‘);
 
 
#分析器
启用分析器: $this->output->enable_profiler(TRUE);

一种方法是: 在 application/config/profiler.php 
                $config[‘config‘] = FALSE;
                $config[‘queries‘] = FALSE;
另一种方法是: 控制器里调用输出类 的 set_profiler_sections() 函数
                $sections = array(
                    ‘config‘  => TRUE,
                    ‘queries‘ => TRUE
                ); 
               $this->output->set_profiler_sections($sections);
 
禁用分析器: $this->output->enable_profiler(FALSE);
 
 
#基准测试类
 $this->benchmark->mark(‘dog‘); 
//这里发生了一些代码 
$this->benchmark->mark(‘cat‘); 
// 这里发生了一些代码 
$this->benchmark->mark(‘bird‘); 
echo $this->benchmark->elapsed_time(‘dog‘, ‘cat‘);  得到时间差保留4位小数
echo $this->benchmark->elapsed_time(‘cat‘, ‘bird‘);
echo $this->benchmark->elapsed_time(‘dog‘, ‘bird‘);
  
 <?php echo $this->benchmark->elapsed_time(); ?> #显示总执行时间 (只能在视图中加此代码)
 <?php echo $this->benchmark->memory_usage();?>  #显示内存占用(只能在视图中加此代码)
 
 
 
#疑问  为什么CI_Controller中没有 下面这些属性却任然可以 $this->Input 等访问
Benchmark --
Hooks --
Config --
Log --
Utf8 --
URI --
Router --
Output --
Security --
Input --
Lang -- 
 
答:因为公共函数中load_class()加载的所有类名都放到了 is_loaded()中所以一旦执行
		foreach (is_loaded() as $var => $class)
		{ 
			$this->$var =& load_class($class);
		}
则所有的属性值都有了;
 
 
  
 
 
 
############模型类############  文件名和类名应该一致 继承CI_Model
  模型加载之后,它并不会自动去连接你的数据库 可以使用 
  1.标准的数据库方法 连接数据库。
  2.加载模型时第三个参数,加载时自动连接数据库,会使用config/databases.php $this->load->model(‘model_name‘, ‘‘, TRUE);
  3.可以通过第三个参数传一个数据库连接配置,使用临时配置 $this->load->model(‘model_name‘, ‘‘, $config);

  
$this->db->escape($title) 
  
增
$this->db->insert(‘articles‘, array(‘name‘ => $name, ‘url‘ => $url));
$str = $this->db->insert_string(‘table_name‘, array(‘name‘ => $name, ‘url‘ => $url));
$this->db->insert_id(); //返回新插入行的ID。 
$this->db->affected_rows(); 返回受影响的行数。


改 
$where = "author_id = 1 AND status = ‘active‘"; 
$str = $this->db->update_string(‘table_name‘, array(‘name‘ => $name, ‘url‘ => $url), $where);
 
 
查   
$this->db->order_by(‘id‘, ‘DESC‘);
$this->db->join(‘categoryes‘, ‘categoryes.cid = articles.cid‘,‘left‘);	
$query = $this->db->get(‘articles‘, $num, $offset);
$query->result_array(); //查多行
  
$query=$this->db->get_where(‘articles‘,array(‘id‘=> $id));
$query->row_array();//查询单行
 
$sql = "SELECT * FROM some_table WHERE id = 2 AND status = 1 AND author =2";
$this->db->query($sql)->row_array(); 
   
$this->db->last_query();返回上一次执行的查询语句 
$this->db->count_all(‘my_table‘); 统计条数
 

############视图##############  在控制器做分开加载
$this->load->view(‘header‘);
$this->load->view(‘menu‘);
$this->load->view(‘content‘, $data);
$string = $this->load->view(‘main/footer‘, array(‘a‘=> 2, ‘b‘=> 4), TRUE);



事务
$this->db->trans_begin();
$this->db->query(‘AN SQL QUERY...‘);
$this->db->query(‘ANOTHER QUERY...‘);
$this->db->query(‘AND YET ANOTHER QUERY...‘);

if ($this->db->trans_status() === FALSE)
{
    $this->db->trans_rollback();
} else {
    $this->db->trans_commit();
}




缓存的使用
http://my.oschina.net/u/1472370/blog/220726

 


     /*所有的表*/
     public function init_tables() {
        $this->table_admin_group = array(‘table‘ => ‘admin_group‘, ‘key‘ => ‘id‘);
        ......
	}
 
    /**
	  $r = $this->db_get(‘uid‘, null, array(‘where‘ => array(‘username‘ => $param)));
	  
     * MY_Model::db_get()
     * 查询单个
     * @param string $select
     * @param string $id
     * @param mixed $param
     * @param mixed $table_key
     * @return
     */
    public function db_get($select = ‘*‘, $id = null, $param = array(), $table_key = array()) 

    /**
	  //调用
	   $param[‘group‘] = ‘group‘;
	   $param[‘key‘] = ‘group‘;
	   $param[‘do_page‘] = 0;
	   return $this->db_lists(‘COUNT(uid) AS count, `group`‘, $param);

     * MY_Model::db_lists()
     * 查询列表
     * @param string $select
     * @param mixed $param
     * @param mixed $table_key
     * @param bool $no_distinct
     * @return
     */
    public function db_lists($select = ‘*‘, $param = array(), $table_key = array(), $no_distinct = false)

    /**
	  //调用
		$data[‘menus‘] = isset($post[‘menus‘]) ? implode(‘,‘, $post[‘menus‘]) : ‘‘;
        $data[‘purviews‘] = isset($post[‘purviews‘]) ? implode(‘,‘, $post[‘purviews‘]) : ‘‘;
        $data[‘status‘] = 1;
        $id = $this->db_add($data, $this->table_admin_group);
     * MY_Model::db_add()
     * 插入数据
     * @param mixed $data
     * @param mixed $table_key
     * @return
     */
    public function db_add($data = array(), $table_key = array()) 
	

    /**
	    //调用
	    $data[‘purviews‘] = isset($post[‘purviews‘]) ? implode(‘,‘, $post[‘purviews‘]) : ‘‘;
        $id = $this->db_update($data, $id, null, $this->table_admin_group);
		
     * MY_Model::db_update()
     * 更新数据
     * @param mixed $data
     * @param string $ids
     * @param mixed $param
     * @param mixed $table_key
     * @return
     */
    public function db_update($data = array(), $id = null, $param = array(), $table_key = array()) {
	
		
    /**
	//调用
	  $this->db_del($like_id, null, $this->table_college_post_like);
	
     * MY_Model::db_del()
     * 删除数据
     * @param string $id
     * @param mixed $param
     * @param mixed $table_key
     * @return
     */
    public function db_del($id = null, $param = array(), $table_key = array()) {	
		
		
    /**
	 
	
     * MY_Model::db_counts()
     * 计算总数
     * @param mixed $param
     * @param mixed $table_key
     * @param bool $no_distinct
     * @return
     */
    public function db_counts($param = array(), $table_key = array(), $no_distinct = false) {	
	



#公共函数
   
     /**
     * @desc 错误信息 
     * @param string  $message 错误消息
     * @param string  $status_code   http状态码
     * @param string  $heading   错误页面标题
     * @return void   
     使用错误模板  application/views/errors/html/error_general.php 
                   application/views/errors/cli/error_general.php
     */
     show_error($message, $status_code, $heading = ‘An Error Was Encountered‘)
 
     /**
     * @desc  404 错误信息:
     * @param string  $page -- URI string
     * @param string  $log_error  是否记录日志
     * @return void   
     使用错误模板  application/views/errors/html/error_404.php
                   application/views/errors/cli/error_404.php
     */
     show_404($page = ‘‘, $log_error = TRUE)
 
     /**
     * @desc 写入日志文件信息
     * @param string  $level -- 可选 ‘error‘,  ‘debug‘ , ‘info‘
     * @param string  $message  -- 日志内容
     * @param string  $php_error  -- Whether we‘re logging a native PHP error message
     * @return void    
     */
     log_message($level, $message, $php_error = FALSE)

     /**
     * @desc 生成错误信息 
     * @param string  $message 错误消息
     * @param string  $status_code   http状态码
     * @param string  $heading   错误页面标题
     * @return void   使用错误模板  application/views/errors/html/error_general.php
     */
     show_error($message, $status_code, $heading = ‘An Error Was Encountered‘)
 
     /**
     * @desc 判断当前运行的 PHP 版本是否高于或等于你提供的版本号
     * @access  
     * @param string  5.3  
     * @return boole
     */
    if (is_php(‘5.3‘)) { echo true; }
    
    /**
     * @desc 判断某个文件是否可写
     * @access
     * @param string  /data/cluster/web/stc/index.php
     * @return boole
     */
    if (is_really_writable(‘file.txt‘)){echo true;}

    /**
     * @desc 访问单个配置项
     * @access
     * @param string  $key   配置文件中的关联数组 key
     * @return mixed
     */    
     config_item($key)

     /**
      * @desc 手动设置服务器的 HTTP 状态码
      * @access
      * @param int  $code   http状态码
      * @param string  $text  响应值
      * @return void
      */
     set_status_header(401);
     
     /**
      * @desc 特殊字符过滤,防止 XSS 攻击
      * @access
      * @param mixed  $var   需要过滤的字符串或数组
      * @return mixed
      */
      html_escape($var)

      /**
       * @desc  引入 application/config/mimes.php 文件中定义的 MIME 数组 
       * @return array
       */
      get_mimes()

      /**
       * @desc 判断是否是https
       * @return boolean
       */
      is_https() 

      /**
       * @desc 检查一个函数是否可用
       * @param string  $function_name  函数名字
       * @return boolean
       */ 
      function_usable($function_name)

	  今天:
	  1.APP首页弹窗广告功能测试优化,及数据迁移;
	  
	  
	  
	  
	  
	  
1、入口初始化	
  常量、      
      [ENVIRONMENT] => development
      [SELF] => admin.php
      [BASEPATH] => /data/cluster/web/src/ci/system/
      [FCPATH] => /data/cluster/web/src/ci/
      [SYSDIR] => system
      [APPPATH] => /data/cluster/web/src/ci/admin/
      [VIEWPATH] => /data/cluster/web/src/ci/admin/views/   
	  
2、core/CodeIgniter.php 
      2.1、引入框架常量
       APPPATH.‘config/ENVIRONMENT/constants.php‘
       APPPATH.‘config/constants.php‘   
	  
	  2.2、引入全局函数 SYSTEM.‘core/Common.php‘
	  
	  2.3、注册 错误 异常 执行结束 处理函数
	  
	  2.4、引入自定义 自动加载机
	  
	  2.5、引入启动计时  SYSTEM.‘core/Benchmark‘
	  
	  2.6、引入配置 SYSTEM.‘core/Config‘

	  2.7、设置php字符集

	  2.8、引入常用处理函数 不论是否加载扩展 mbstring  hash

	  2.9、引入URI、Utf8、Output、Security(安全)类   SYSTEM.‘core/Benchmark‘

	  2.10、引入路由  SYSTEM.‘core/Router‘

	  2.11、引入控制器父类 BASEPATH.‘core/Controller.php‘
	              在此处执行了ci自带的自动加载机

   	  2.12、实例化请求url中的控制器  $CI = new $class();

	  2.13、执行url中的方法 call_user_func_array(array(&$CI, $method), $params);
	  
	  
	  

  
	   
	 chnia	http://download.eclipse.org/technology/babel/update-site/R0.13.0/mars	启用 
	  svn	http://subclipse.tigris.org/update_1.8.x	启用
	  
	  
	   Reflection::export(new ReflectionClass ($this->db ));
          
		   

 

ci学习

标签:

原文地址:http://www.cnblogs.com/sixiong/p/5757937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!