PHP CI框架的安装配置和简单使用

来源:互联网 发布:淄博网站seo 编辑:程序博客网 时间:2024/06/06 01:49
概述和基本配置参数 配置CI:

application/config/config.php:14配置你的域名

application/config/database.php:40配置你数据库的相关参数

配置基于htaccess的重定向

RewriteEngine on

RewriteCond $1 !^(index¥.php|images|robots¥.txt)

RewriteRule ^(.*)$ /index.php/$1 [L]

任何除开 index.php,images, 和robots.txt 的 HTTP 请求都当成对 index.php 文件的请求。

增加 URL 后 缀

application/config/config.php:57 配置$config['url_suffix'] = ".html";

配置后带或不带后缀都可以访问

使用index.php的get方式访问控制类

application/config/config.php中:

$config['enable_query_strings'] = true;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';

访问方式:index.php?c=controller&m=method

自定义的简单的控制类

<?php

class Blog extends Controller {

 

              function index()

              {

                            echo 'Hello World!';

              }

}

?>

控制器的访问和参数传递

www.your-site.com/classname/functionname/para1/para2

www.your-site.com/classname/functionname/para1/para2.html

定义默认控制器

application/config/routes.php 中配置 $route['default_controller'] = 'classname';

控制器中的预定义方法

_remap() 方法:不管uri中调用的 方法是哪个,该请求都会被重定向到此方法

被覆盖的方法名会作为参数传递进来,参数依次排列到后面

_output() 方法:类中任意函数的输出结果都会交给此函数处理后再交给客户浏览器

Function _output($output){echo $output};

视图

在控制器中加载特定视图:$this->load->view('name');

name 是视图文件的文件名,不包括.php 后缀。

$this->load->view('folder_name/file_name');

向视图中传递动态数据:

$this->load->view('blogview', $data,$data2);

$data可以是数组或者对象(类的示例);

视图中使用PHP语法

模型

引入Model:$this->load->model('Model_name');

一旦引入:可以使用里面的函数:$this->Model_name->function();

还可以指定model名:

$this->load->model('Model_name', 'fubar');

$this->fubar->function();

模型被加载后不会自动建立数据库连接,只会在被调用的时候才会

要在引入模型时建立数据库连接,需要$this->load->model('Model_name', '', TRUE);

助手

助手包含完成特定功能的一系列函数,导入特定助手后即可以像使用php内置函数一样使用CI助手提供的函数了

$this->load->helper('name');

加载多个助手:$this->load->helper( array('helper1', 'helper2', 'helper3') );

自动加载某个助手:配置application/config/autoload.php文件,把要加载的插件加入到自动加载的数组里(autoload array)。

插件

与helper类似,但插件只提供单一的功能,而helper提供一系列的功能

$this->load->plugin('name');

比如$this->load->plugin('captcha');加载的就是captcha_pi.php

$this->load->plugin( array('plugin1', 'plugin2', 'plugin3') );

自动加载:application/config/autoload.php

CI库

加载库:$this->load->library('class name');

创建自己的库,要求,首字母大写,

自定义库中引用CI资源:get_instance() 函数

$CI =& get_instance();

$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');

使用CI管理 数据库

application/config/routes.php:$route['scaffolding_trigger'] = "zhougege";

在你的控制器的构造函数中添加:$this->load->scaffolding('marry_user_profile');

就可以通过访问http://localhost/yourclass/zhougege来 管理你的数据库了

CI错误处理

Index.php中默认的是显示所有错误:              error_reporting(E_ALL);

 

show_error('message')函数,按application/errors/error_general.php作为模板显示错误信息

show_404('page')函数,按application/errors/error_404.php作为模板显示一个404错误

log_message('level', 'message')函数,把错误信息写入错误日志。你必须在第一个参数中提供错误级别(三种),指出是哪种级别的错误(debug, error, info), 第二个参数是错误信息

注意:为了生成错误日志文件,必须在 application/config/config.php 文件中打开 "log_errors" 选项,并保证 "logs" 文件夹可写。另外,你可以 为日志设置 "threshold"。比如,你可以记录错误信息(Error Messages),但不记录其它两种类型。

CI缓存: 在控制器中打开缓存:$this->output->cache(n); n是缓存时间,单位是

0 0
原创粉丝点击