php框架

 首页 > php框架 > ci常用总结

ci常用总结

分享到:
【字体:
导读:
          ?php if ( ! defined(BASEPATH)) exit(No direct script access allowed); class Test extends CI_Controller { public function __construct(){ parent::__construct(); $this-init(); } //负责处理一些初始化的工作 public function init(){ he...

 
class Test extends CI_Controller {
 
public function __construct(){
parent::__construct();
$this->init();
}
 
//负责处理一些初始化的工作
public function init(){
header("content-type:text/html;charset=utf-8");
$this->config->load("pagination",true);
$this->load->library("upload");
$this->load->model("role_model");
$this->load->database();
}
 
//配置文件的测试
public function testdemo(){
echo "
";print_r($this->config);
echo "路径:".$this->config->item("base_url");
}
 
//辅助函数、类库、模型的测试
public function testdemo1(){
echo "
";print_r($this->load);
echo "
";print_r($this->upload);
echo "
";print_r($this->role_model);
echo "
";print_r($this->db);
}
 
public function testdemo2(){
echo "test...";
$aa=&get_instance();
echo "
";print_r($aa);
}
 
public function testdemo3(){
 
}
 
 
}
 
/*
1、echo "
";print_r($this->config);//返回 CI_config核心类实例化对象
  echo $this->config->item("base_url");//调用CI_config核心类实例化对象的item方法
  
  注意:配置项都被存储在一个叫做$config的数组里面(数组名必须是$config,否则通过$this->config->item();方法获取不到想要的数据)
 
  配置项还可以是数组,如下所示:
 
  $config['config_big_thumb']=array(
'image_library' => 'gd2',//gd2图库
'create_thumb' => true,//是否创建缩略图
'maintain_ratio' => true,
'width' => 300,//缩略图宽度
'height' => 300,//缩略图的高度
'thumb_marker'=>"_300_300"//缩略图名字后加上 "_300_300",可以代表是一个300*300的缩略图
);
 
通过$this->config->item("config_big_thumb");获取到的是一个数组
 
 
 
//同时加载多个配置文件的情况
$this->config->load("config");
$this->config->load("pagination");
echo "
";print_r($this->config);
结论:两个配置文件中的配置项合并成一个数组,同名配置项的值会被后者覆盖
 
 
//不同的配置文件存在同名索引时,会发生冲突,为了避免这个问题,可以设置第二个参数为true,这样可以使得每个配置文件中的内容存放在一个单独的数组中,数组的索引名就是配置文件的名字
$config[base_url]="http://www.kuxiu.net/";
$config[pagination]=array(//pagination-配置文件名
                    base_url => http://www.baidu.com
                    total_rows => 200
        )
 
 
//"application/config/config.php"配置文件已自动加载,无需再手动启用
 
 
2、echo "
";print_r($this->load);//返回 CI_Loader 类的实例化对象
  $this->load->library("upload");//通过CI_Loader类的libraries方法加载CI_Upload类
  echo "
";print_r($this->upload);//返回CI_Upload类的实例化对象
 
  CI_Loader类加载的所有类库、模型、附属函数都给了当前控制器,所以可以在当前控制器中采用 $this->upload 这种方式获取加载的类
 
 
  $this->load->library("upload");  //返回CI_Upload类实例化对象本身
  $this->load->model("role_model");//返回Role_model类实例化对象本身
 
3、核心类控制器中:
 
  $this->load =& load_class('Loader', 'core');
 
  所以,我们可以在控制器中通过$this->load获取到CI_Loader类实例化对象,从而可以调用里面中的方法来动态加载一些类库、模型及公共辅助函数
 
 
4、当触发一个控制器的时候,自动加载的核心类库如下:
 
CI_Benchmark
CI_Hooks
CI_Config
CI_URI
CI_Router
CI_Output
CI_Security
CI_Input
CI_Lang
CI_Loader
 
5、在任何地方都可通过 $CI=&get_instance(); 来获取当前控制器实例化对象
 
 
6、$this->load->database();//返回CI_DB_mysql_driver类实例化对象,名字叫db
 
  echo "
";print_r($this->db);//返回CI_DB_mysql_driver类实例化对象
 
7、system/core/ 文件夹下的所有核心类文件在项目运行时,全部被加载
 
8、辅助函数:
 
url 辅助函数可以帮助我们创建链接
form 辅助函数可以帮助我们创建表单
file 辅助函数可以帮助我们处理文件
text 辅助函数可以为我们提供一系列的格式化输出方式
分享到:
config.php配置文件解读
CodeIgniter 基本配置信息在 application/config/config.php 文件,本文详细讲解每一个基本配置选项,从而快速掌握 CodeIgniter 进行开发。   $config['base_url'] = "http://www.nowamagic.net/"。 您网站的网址,CodeIgniter 会根据这个网址来生成链接、表单地址等。   $config['index_...
单一入口文件index.php分析
什么是单一入口应用程序?在解释什么是单一入口应用程序之前,我们先来看看传统的 web 应用程序。news.php 显示新闻列表news_edit.php 显示新闻编辑页面这两个页面不但分别实现了两个功能,还成为了应用程序的两个入口。那什么是入口啊?打个比方,大家上 WC,都是男生进一个门,女生进一个门。这两个门就是 WC 的两个入口...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……