CodeIgniter

来自tomtalk
跳转至: 导航搜索

图像处理

$this->img_resize(realpath($rename));     //取文件绝对路径
 
private function img_resize($file_name)
{
    $CI =& get_instance();
 
    $config['image_library'] = 'gd2';
    $config['source_image'] = $file_name; //要写绝对地址,不然会报gd错误
    $config['create_thumb'] = TRUE;
    $config['thumb_marker'] = '';
    $config['maintain_ratio'] = TRUE;
    $config['width'] = 800;
    $config['height'] = 800;
 
    $CI->load->library('image_lib', $config);
 
    if (!$CI->image_lib->resize()) {
        echo $CI->image_lib->display_errors();
        exit;
    }
}

CI 框架数据库操作函数 this->db->where() 的使用

1) $this->db->where(‘MATCH (field) AGAINST (“value”)’, NULL, FALSE)

如果把$this->db->where() 接受可选的第三个参数设置为 FALSE, CodeIgniter 将不会为那些包含反勾号的字段名或表名提供保护。

2) $this->db->or_where()

本函数与上面的那个几乎完全相同,唯一的区别是本函数生成的子句是用 OR 来连接的: $this->db->where(‘name !=’, $name); $this->db->or_where(‘id >’, $id); // 生成: WHERE name != ‘Joe’ OR id > 50 说明: or_where() 以前被叫作 orwhere(), 后者已经过时。

3) $this->db->where_in();

生成一段 WHERE field IN (‘item’, ‘item’) 查询语句,如果合适的话,用 AND 连接起来。 $names = array(‘Frank’, ‘Todd’, ‘James’); $this->db->where_in(‘username’, $names); // 生成: WHERE username IN (‘Frank’, ‘Todd’, ‘James’)

4)$this->db->or_where_in();

生成一段 WHERE field IN (‘item’, ‘item’) 查询语句,如果合适的话,用 OR 连接起来。 $names = array(‘Frank’, ‘Todd’, ‘James’); $this->db->or_where_in(‘username’, $names); // 生成: OR username IN (‘Frank’, ‘Todd’, ‘James’)

5)$this->db->where_not_in();

生成一段 WHERE field NOT IN (‘item’, ‘item’) 查询语句,如果合适的话,用 AND 连接起来。 $names = array(‘Frank’, ‘Todd’, ‘James’); $this->db->where_not_in(‘username’, $names); // 生成: WHERE username NOT IN (‘Frank’, ‘Todd’, ‘James’)

6)$this->db->or_where_not_in();

生成一段 WHERE field NOT IN (‘item’, ‘item’) 查询语句,如果合适的话,用 OR 连接起来。 $names = array(‘Frank’, ‘Todd’, ‘James’); $this->db->or_where_not_in(‘username’, $names); // 生成: OR username NOT IN (‘Frank’, ‘Todd’, ‘James’)

CodeIgniter 一个Model中调用另一个Model的问题

$CI =& get_instance();
$CI->car_model->update_car($option);

CI模板中如何引入模板

<div class="tab-content">
    <?php $this->load->view('user/coupon_list', array('coupons' => $coupons_unused, 'id' => 'Unused', 'class' => 'in active')) ?>
    <?php $this->load->view('user/coupon_list', array('coupons' => $coupons_used, 'id' => 'Used', 'class' => '')) ?>
    <?php $this->load->view('user/coupon_list', array('coupons' => $coupons_all, 'id' => 'Expired', 'class' => '')) ?>
</div>

CI发邮件

public function forget_password() {
    //Email配置
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'smtp.163.com';
    $config['smtp_user'] = 'sky007_tom';
    $config['smtp_pass'] = '******';
    $config['smtp_port'] = '25';
    $config['charset'] = 'utf-8';
    $config['wordwrap'] = TRUE;
    $config['mailtype'] = 'html';
 
    $this->load->library('email', $config);  
 
    //邮件内容
    $this->email->to('11877803@qq.com');
    $this->email->from('sky007_tom@163.com', 'tom');  
    $this->email->subject('Email Test');
    $this->email->message('<font color=red>Testing the email class.</font>');
 
    if ($this->email->send()) {
        echo 'Your email was sent, fool.';
    } else {
        show_error($this->email->print_debugger());
    }
}

$this->email->from(),第一个参数应为smtp发件邮箱地址,否则smtp会返回 550 Invalid User。没留意随便写点东西,就发不了邮件了,用了1.5小时,查各种可能性,最后才发现这个地方写错了。

表单验证

private function register_validate()
{
    $this->load->helper(array('form', 'url'));
 
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('', '');
 
    $inputs = array(
        (object)array(
            'field' => 'customer_id',
            'label' => '所属企业',
            'rules' => 'required'
        ),
        (object)array(
            'field' => 'email',
            'label' => '邮箱',
            'rules' => 'required|valid_email'
        ),
        (object)array(
            'field' => 'pwd1',
            'label' => '密码',
            'rules' => 'required|min_length[6]'
        ),
        (object)array(
            'field' => 'real_name',
            'label' => '姓名',
            'rules' => 'required'
        ),
        (object)array(
            'field' => 'mobile',
            'label' => '手机号',
            'rules' => 'required|integer|exact_length[11]'
        )
    );
 
    foreach ($inputs as $input) {
        $this->form_validation->set_rules($input->field, $input->label, $input->rules);
 
        if ($this->form_validation->run() == FALSE) {
            echo json_encode(array(
                'success' => false,
                'input' => $input->field,
                'msg' => form_error($input->field) //validation_errors()
            ));
 
            exit;
        }
    }
}

代码片段

//数据库读取
function get($option)
{
    $this->db->order_by('id','DESC');
    $query = $this->db->get('articles', 20, $option['start']);
 
    return (array('data' => $query->result(),
        'total' => $this->db->count_all_results('articles')
    ));
}

基于 CodeIgniter 开源 CMS Ionize

演示地址:http://demo.ionizecms.com/admin

用户名和密码都是:demo

codeigniter(CI框架)在Nginx的rewrite的配置

http://konts.blog.163.com/blog/static/13841306420139253207638/

apache rewrite

在/目录下放.htaccess 文件

RewriteEngine on

RewriteCond $1 !^(index\.php|file|api|images|xml|media|data|css|js|scripts|responses|tools|test\.php|robots\.txt)

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

在view里可使用的ci内部模板变量有哪些?

{elapsed_time} 执行时间

{memory_usage} 内存消耗

Codeigniter基本配置详讲

codeigniter 基本配置信息在 application/config/config.php 文件,本文详细讲解每一个基本配置选项,从而快速掌握 codeigniter 进行开发。

$config['base_url'] = "http://www.example.com/" 您网站的网址,codeigniter 会根据这个网址来生成链接、表单地址等。

$config['index_page'] = "index.php" codeigniter 根目录下的 index.php 文件名,codeigniter 会使用它来生成链接地址。如果使用隐藏 index.php 的 URL,将其设置为空字符串:$config['index_page'] = ""。

$config['uri_protocol'] = "AUTO" codeigniter 生成 URL 使用的格式,设置为“AUTO”自动探测。如果链接不能正常工作,可以尝试以下值:PATH_INFO、QUERY_STRING、REQUEST_URI、ORIG_PATH_INFO。

$config['url_suffix'] = "" codeigniter 产生链接时使用的 URL 后缀,如果要实现伪静态,可以设置 $config['url_suffix'] = ".html"。

$config['language'] = "english" codeigniter 程序默认使用的语言

$config['charset'] = "UTF-8" codeigniter 程序默认使用的字符集

$config['enable_hooks'] = FALSE 是否启用钩子,钩子功能使得您可以在不修改系统核心文件的基础上来改变或增加系统的核心运行功能。

$config['subclass_prefix'] = 'MY_' 设置扩展 codeigniter 类库时使用的类名前缀

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-' 设置 codeigniter URL 中允许使用的字符,这是一个正则表达式。当访问者试图访问的 codeigniter URL 包含其它字符时,会得到一个警告。应该尽量限制 codeigniter URL 使用的字符来提高安全性,可以有效的过滤注入攻击。如果设置为空,允许使用所有字符,强烈建议不要这么做。

$config['enable_query_strings'] = FALSE codeigniter URL 默认使用分段的 URL,此选项也允许 codeigniter 开启查询字符串形式 URL。您可以使用查询字符串来传递要访问的控制器和函数。例如: index.php?c=controller&m=method。codeigniter 默认使用分段的 URL,查询字符串的 URL 很多特性不被支持。

$config['controller_trigger'] = 'c' codeigniter 将查询字符串中此选项对应的值当做 codeigniter 控制器的名字。

$config['function_trigger'] = 'm' codeigniter 将查询字符串中此选项对应的值当做 codeigniter 控制器方法的名字

$config['log_threshold'] = 0 启用错误日志,设置记录哪些类型的错误。

  • 0 = 关闭错误日志记录
  • 1 = 记录错误信息
  • 2 = 记录调试信息
  • 3 = 记录通知信息
  • 4 = 记录所有信息

$config['log_path'] = 如果您不想使用默认的错误日志记录目录配置(system/logs/),可以设置完整的服务器目录。

$config['log_date_format'] = 'Y-m-d H:i:s' codeigniter 错误日志时间格式

$config['cache_path'] = 如果您不想使用默认的缓存目录(system/cache/)来存储缓存,可以设置完整的服务器目录

$config['encryption_key'] = "" codeigniter 使用的密钥

$config['global_xss_filtering'] = FALSE 是否对输入数据(GET、POST)自动过滤跨脚本攻击

$config['compress_output'] = FALSE 启用Gzip压缩达到最快的页面加载速度

$config['time_reference'] = 'local' 设置时间格式:"local"、"GMT"

$config['rewrite_short_tags'] = FALSE 如果您想要使用短标记,但 PHP 服务器不支持,codeigniter 可以通过重写短标记来支持这一功能。

$config['proxy_ips'] = 如果访问者通过代理服务器来访问您的网站,您必须设置代理服务器 IP 列表,以识别出访问者真正的 IP。

codeigniter目录结构及运行流程

主要目录:

application 实际应用目录。可以新建多个,可以改名。
cache 保存cache文件目录
codeigniter 框架核心文件目录
database 框架操作数据库文件目录
helpers 框架自带助手类文件目录
language 提示信息语言文件目录
libraries 框架类库目录
logs 日志文件目录
plugins 插件类文件目录,可扩展添加新文件
scaffolding 脚手架文件目录

application子目录:

config 配置文件目录
controllers 网站控制层文件
errors 保存错误输出信息文件
helpers 用户定义助手类文件目录
hooks 用户定义钩子类文件目录
language 用户定义提示信息语言文件目录
libraries 用户定义扩展类文件目录
models 网站业务逻辑层文件目录
views 网站展现层文件目录

所有的入口都从根目录下的index.php进入,之后加载codeigniter/CodeIgniter.php文件,该文件会顺序加载以下文件执行整个流程。

  1. codeigniter/Common.php :全局函数定义文件
  2. codeigniter/Compat.php :字符与数字匹配函数定义文件
  3. application/config/contants.php :应用程序宏定义文件
  4. libraries/Benchmark.php 初始化 CI_Benchmark 对象,用来记录执行时间
  5. libraries/Hook.php 初始化 CI_Hooks 对象,检测是否有钩子对象调用
  6. libraries/Config.php 初始化 CI_Config 对象,读取应用配置文件
  7. libraries/URI.php 初始化 CI_URI 对象,解析url参数
  8. libraries/Router.php 初始化 CI_Router 对象,检测路由配置,解析 HTTP 请求,以确定谁来处理
  9. libraries/Output.php 初始化 CI_Output 对象,检查是否有缓存文件,如果存在则直接输出内容。
  10. libraries/Input.php 初始化 CI_Input 对象,过滤 HTTP 请求和任何用户提交的数据
  11. libraries/Language.php 初始化 CI_Language 对象,初始化提示语言变量
  12. libraries/Controller.php 初始化 CI_Controller 基类,该类初始化的同时,会装载模型、核心库、插件、辅助函数,以及任何处理特定请求所需的其它资源,然后初始化处理请求的控制器对象处理请求,处理后展 现层(View)渲染发送到浏览器中的内容。如果开启缓存(Caching),视图首先被缓存再输出到浏览器,缓存文件可用于以后的请求。