1、ci框架介绍
CodeIgniter 是为 PHP 开发人员提供的一套 Web 应用程序工具包。 它的目标是能够让你比从零开始更加快速的完成项目,它提供了一套 丰富的的类库来满足我们日常的任务需求,并且提供了一个简单的 接口和逻辑结构来调用这些库。CodeIgniter 通过最小化你需要的代码量, 让你把更多的精力放到项目的创造性开发上。
CodeIgniter 通过 MIT 开源许可协议授权,你可以任意使用。阅读 许可协议 了解更多内容。
CodeIgniter 使用了模型-视图-控制器 架构,它能很好的将逻辑层和表示层分离。 特别是对于那些使用了模板文件的项目来说更好,它能减少模板文件中的代码量。
CodeIgniter 拥有全面的类库,能满足大多数 Web 开发任务的需要, 例如:访问数据库,发送邮件,验证表单数据,会话管理,处理图像, 处理 XML-RPC 数据
2、流程介绍
- index.php 文件作为前端控制器,初始化运行 CodeIgniter 所需的基本资源;
- Router 检查 HTTP 请求,以确定如何处理该请求;
- 如果存在缓存文件,将直接输出到浏览器,不用走下面正常的系统流程;
- 在加载应用程序控制器之前,对 HTTP 请求以及任何用户提交的数据进行安全检查;
- 控制器加载模型、核心类库、辅助函数以及其他所有处理请求所需的资源;
- 最后一步,渲染视图并发送至浏览器,如果开启了缓存,视图被会先缓存起来用于 后续的请求。

3、MVC 模型-视图-控制器
CodeIgniter 的开发基于 MVC(模型-视图-控制器)设计模式。MVC 是一种 用于将应用程序的逻辑层和表现层分离出来的软件方法。
4、ci安装
1.设置网站根目录
[application]->[config]选择config.php 文件设置你网站的根 URL
$config['base_url'] = 'localhost';//服务器的地址

2.设置数据库
3.隐藏system 和 application文件
隐藏 CodeIgniter 的文件位置来增加安全性,你可以将 system 和 application 目录修改为其他的名字,然后打开主目录下的 index.php 文件将 systempath和application_folder 两个变量设置为你修改的名字
$system_path = 'system'; $application_folder = 'application';
(3)运行服务器,并且在浏览器中运行http://localhost/seraph/
出现这个就成功了。
4.初次运行
把里面的代码全改为
defined('BASEPATH') OR exit('No direct script access allowed'); ?> <html lang="en"> <head> <meta charset="utf-8"> <title>hello CodeIgniter
title>
head> <body> <h1>hello world
h1>
body>
html>
这样在浏览器打开刚才的页面就是hello。word了。
5、页面跳转
1.设置CI框架的初始控制器
- 当运行CI项目时,我们所看到的第一个页面是由controller转发的视图,而这个controller我们是在[application]->[config]->[routes.php]进行设置
$route['default_controller'] = 'welcome';
当前设置的页面是由welcome转发的视图。
2. 创建两个页面
(1)首先在[application]->[controllers]目录下创建Page2.php
class Page2 extends CI_Controller {
public function index() {
$this->load->view('page2'); } }
(2)在[application]->[view]目录下创建Page2.PHP
defined('BASEPATH') OR exit('No direct script access allowed'); ?> <html lang="en"> <head> <meta charset="utf-8"> <title>page2
title>
head> <body> <h1 >page2
h1>
body>
html>
3.实现页面跳转
class Welcome extends CI_Controller {
function Welcome(){
parent::__construct(); $this->load->helper('url'); } public function index() {
$this->load->view('welcome'); } }
(2)在[application]->2 次浏览->[welcome.php]页面添加js代码
<html lang="en"> <head> <meta charset="utf-8"> <title>第一个页面
title> <script type="text/javascript"> function turnto(){
var url = "
"; window.location.href=url; }
script>
head> <body> <h1 onclick="turnto()">hello student
h1>
body>
html>
base_url() : application/config/config.php设置的路径
redirect( ‘控制器名/方法名’ ): 跳转
sit_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/php2
6、数据读取
前面讲解了ci中设置数据库,数据库操作都在model文件夹中。
1.创建Model
(1)在application/models创建SingerModels.php文件,在文件里面创建SingerModels类并且继承CI_Model
class SingerModel extends CI_Model {
function SingerModel() {
$this->load->database(); } function getSinger() {
$sql = 'SELECT * from singer'; // 查询数据库 $query = $this->db->query($sql); // $query=$this->db->get('singer'); // 以数组形式返回查询结果 return $query->result_array(); } function insertSinger() {
$sql = "insert into singer(name,introduce)values('king','123')"; $query = $this->db->query($sql); echo $query; } function insertSinger2() {
$data = array( 'name' => "wwesdd", 'introduce' => "33535y4erth" ); $query = $this->db->insert('singer', $data); echo $query; } function updataSinger1() {
$sql = "update singer set name = 'kinggg' where id=30"; $query = $this->db->query($sql); echo $query; } function updataSinger2() {
$this->db->set('name', 'field+1'); $this->db->where('id', 30); $query=$this->db->update('singer'); echo $query; } function deleteSinger1(){
$sql = "delete from singer where id =30 "; $query = $this->db->query($sql); echo $query; } function deleteSinger2(){
$this->db->where('id', 31); $query =$this->db->delete('singer'); echo $query; } } ?>
class Singer extends CI_Controller{
function Singer(){
parent::__construct(); $this->load->helper('url'); $this->load->model('SingerModel'); } function index(){
//$this->SingerModel->deleteSinger2(); $singers = $this->SingerModel->getSinger(); $data['singers'] = $singers; $this->load->view('singer',$data); } } ?>
$imageUrl = base_url()."images/"; foreach ($singers as $singer){ //echo "".$singer['name']."的介绍是
"; $url = $imageUrl.$singer['singer_icon_url']; $name = $singer['name']; $introduce = $singer['introduce']; echo "
.$url."" />
"
.$singer['name'].""; } ?>
7、ci中ajax的运用
1.views前端页面添加ajax请求
创建ajax发出请求
var name ='好哈哈aaaaa'; var url = '
Ajax/getAjaxData')?>'; $.ajax({ data:{name:name}, //要发送的数据 type:"POST", //发送的方式 url:url, //url地址 error:function(msg){
//处理出错的信息 }, success:function(msg){
//处理正确时的信息 alert( msg) } });
2.在controllers里面创建接受方法
function getAjaxData(){
$name = $this->input->get_post('name'); echo $name."呵呵呵呵呵"; }
8、图片上传
1.前端表单
echo form_open_multipart('upload/do_upload');?> <input type="file" name="userfile" size="20" /> <br /> <input type="submit" value="upload" />
form>
2.后台接收图片
public function do_upload() {
$config['upload_path'] = './uploads/'; //图片存放路径 $config['allowed_types'] = 'gif|jpg|png';//图片类型 $config['max_size'] = 100; //图片大小 $config['max_width'] = 2000; //图片宽度 $config['max_height'] = 2000; //图片高度 $config['file_name'] = time(); //图片名字 $this->load->library('upload', $config); if ( ! $this->upload->do_upload('userfile')) { echo $this->upload->display_errors(); } else { echo "成功!"; } }
9、获得url中参数
1,get参数获取
(1)请求的参数是
http://127.0.0.1/ci/index.php/Page2
http://127.0.0.1/ci/index.php/Page2/getId/id/2
控制器是Page2.
// $data=getId,getId控制器Page2里面的方法 使用$data=$this->uri->segment(2); // $data=id,id是请求的参数键 使用$data=$this->uri->segment(3);
2.post参数获取
$name = $this->input->get_post('name');
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/201693.html原文链接:https://javaforall.net
