2019年8月2日 天气晴
初识CodeIgniter框架
本文将浅显的介绍CI框架的接收
、跳转
、载入
;详情请参考CI手册输入类
1. 如何接受get和post的传值
$this->input->post('name');
//括号中的name为欲接受的post中的值,若数据不存在方法将返回false
$this->input->post('name',boolean);
//第二个参数是可选的,如果想让取得的数据经过跨站脚本过滤,把第二个参数设置为true
$this->input->post();
//(不设置任何参数,该方法将以数组的形式)返回过来全部的post数据,且不经过跨站脚本过滤
$this->input->get();
$this->input->get('name');
$this->input->get('name',boolean);
//get的用法与post一样
奇葩
$this->input->get_post('name',boolean);
//该方法将会搜索get和post方式传过来的数据流,先以post方式搜索,再以get方式搜索
2. 如何获取、设置server和cookie的数据
$this->input->cookie('name',boolean);
$this->input->server('name',boolean);
//此方法类似post和get的获取方式
设置cookie的值
$this->input->set_cookie();
//该函数接受两种形式的参数
//1.数组(用数组形式,传递的是一个关联数组)
$cookie = array(
'name' => 'The Cookie Name',
'value' => 'The Value',
'expire' => '86500',
'domain' => '.some-domain.com',
'path' => '/',
'prefix' => 'myprefix_',
'secure' => TRUE
);
$this->input->set_cookie($cookie);
删除cookie:可以通过将 expire 设置成空来实现删除cookie的操作
1.1 如果expire设置成0,那么cookie仅在浏览器关闭时失效
//2.参数
$this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);
3. 跳转
site_url()和base_url()的区别
假如你config文件里面的base_url和index_page是这样定义的:
config['base_url'] = "http://domain.com/";
config['index_page'] = "index.php";
那么你若使用site_url(“news/php/2”);则实际url为
http://domain.com/index.php/news/php/2
若使用base_url(“news/php/2”);则url为:
http://domain.com/news/php/2
发现没有,base_url()少了一个index.php
在CI框架中做跳转的方法
header(“location:”.site_url(“common/login”));
带参数跳转
<button class="btn btn-xs btn-info" "type="button"
onClick="window.location.href='?n=admin&c=product_brand&m=edit&a=product&id=<?php echo $v['id'] ?>';
>
<i class="icon-edit bigger-120"></i>修改
</button>
超链接跳转
<a href="<?php echo reurl('?n=admin&a=product&c=product_vendors&m=index') ?>">
<i class="icon-plus"></i>供应商管理
</a>
reurl()这个方法只是将传入的url返回而已
4.载入、调用模型
$this->input->model(‘modelName’);
如果模型文件在子文件夹下,引用的时候要带上相对路径名。例如:如果你有一个模型 application/models/blog/queries.php。 下面的代码可以引用它:
$this->input->model(‘blog/queries’);
模型一旦被载入,可以使用下面的方法调用他
$this->load->model('Model_name');
$this->Model_name->functionName();
新大陆
使用三元运算符定义常量
define('_COMPANY_ID_', $_SESSION['userid'] ? $this->user_model->admin_company($_SESSION['userid']) : 0);