您的位置:首页 > 编程语言 > PHP开发

ci框架基础详解(入门学习)

2017-07-22 12:27 716 查看

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 是一种 用于将应用程序的逻辑层和表现层分离出来的软件方法。

模型 代表你的数据结构。通常来说,模型类将包含帮助你对数据库进行增删改查的方法。

视图 是要展现给用户的信息。一个视图通常就是一个网页。

控制器 是模型、视图以及其他任何处理 HTTP 请求所必须的资源之间的中介,并生成网页。



4、ci安装

(1)下载安装包:安装包下载

或者百度一下官网下载

因为我本机学习用wampserver,php文件我都放到了www目录下

文件夹可以自己改名,这里改为seraph。

(2)将 CodeIgniter 文件夹及里面的文件上传到服务器,通常 index.php 文件将位于网站的根目录;

使用文本编辑器打开 application/config/config.php 文件设置你网站的根 URL,如果你想使用加密或会话,在这里设置上你的加密密钥;

如果你打算使用数据库,打开 application/config/database.php 文件设置数据库参数。

1.设置网站根目录

[application]->[config]选择config.php 文件设置你网站的根 URL

$config['base_url'] = 'localhost';//服务器的地址




2.设置数据库

[application]->[config]选择database.PHP 文件设置数据库参数



3.隐藏system 和 application文件

隐藏 CodeIgniter 的文件位置来增加安全性,你可以将 system 和 application 目录修改为其他的名字,然后打开主目录下的 index.php 文件将 systempath和application_folder 两个变量设置为你修改的名字

$system_path = 'system';
$application_folder = 'application';


(3)运行服务器,并且在浏览器中运行http://localhost/seraph/



出现这个就成功了。

4.初次运行

1.打开PhpStorm,找到解压的seraph文件

选中视图seraph->application->view->welcome_message.php



把里面的代码全改为

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<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

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>page2</title>
</head>
<body>

<h1 >page2</h1>

</body>
</html>


3.实现页面跳转

(1)在[application]->[controllers]->[Welcome.php]

添加代码$this->load->helper(‘url’);

class Welcome extends CI_Controller {

function Welcome(){
parent::__construct();

$this->load->helper('url');
}
public function index()
{
$this->load->view('welcome');
}
}


(2)在[application]->[views]->[welcome.php]页面添加js代码

<html lang="en">
<head>
<meta charset="utf-8">
<title>第一个页面</title>
<script type="text/javascript">
function turnto(){
var url = "<?php echo site_url('Page2')?>";

window.location.href=url;
}
</script>

</head>
<body>
<h1 onclick="turnto()">hello student</h1>
</body>
</html>


跳转方法:

site_url(): 获得url参数

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

<?php

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;
}
}
?>


(2)控制器获得数据,并且传递参数给视图

在application/controllers/添加相关的代码

<?php
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);

}
}
?>


(3)视图显示数据

在views中相应的文件

<?php

$imageUrl = base_url()."images/";
foreach ($singers as $singer){
//echo "<p>".$singer['name']."的介绍是</p>";
$url = $imageUrl.$singer['singer_icon_url'];
$name = $singer['name'];
$introduce = $singer['introduce'];

echo "<tr><td><img id='image' src='".$url."'></td><td><div id='infor'>".$singer['name']."</div></td></tr>";
}

?>


7、ci中ajax的运用

1.views前端页面添加ajax请求

创建ajax发出请求

var name ='好哈哈4444555aaaaa';
var url = '<?php echo site_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.前端表单

<?php 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


控制器是Page2.

使用data=this->uri->segment(1);

$data值是Page2

(2)请求的参数是

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');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php ci框架
相关文章推荐