Web笔记··By/蜜汁炒酸奶

CodeIgniter自学之旅-入门与简单应用

由于最近被 StartBBS吸引,但其简单的编辑器用起来不太爽,想换成富文本的,但无php经验,故只能先自学相关内容。 CodeIgniter 是基于 MVC 的一套 PHP 快速开发工具包,之后什么什么就不多说了,直接切入正题。 首先你先下载一套开发包在下面就有下载,解压缩之后你会看到一个 system 文件夹和一个 index.php 文件。 打开 CodeIgniter-3.0.1的application 就可以看到 7 个文件夹下面着重介绍一下。 controllers(控制器) views(视图) models(模型) 统称 MVC config(系统配置文件) 下面开始制作 在 application/controllers 新建一个 blog.php 文件

<?php
	class Blog extends CI_Controller{
		function index(){
			echo "come blog";
		}
		function comment(){
			echo "blog comment!";
		}
		function comments($id,$cid,$vid){
			echo "blog commnet!{$id},{$cid},{$vid}";
		}
	}
?>
1
2
3
4
5
6
7
8
9
10
11
12
13

注:以下默认http://yourdomain/为http://localhost/CodeIgniter/,即直接在CodeIgniter文件夹中修改。 问题:类函数为什么要为index()? 回答:index()类函数是默认执行的。 问题:如何指定自己的类函数? 回答:http://yourdomain/index.php/blog/ 这个地址是没有指定的 而如果是 http://yourdomain/index.php/blog/comment/ 这里表示指定了 comment() 类函数我们就可以写成代码中的第二个函数的样式:

function comment(){
			echo "blog comment!";
		}
1
2
3

如此这里页面显示的结果就是 blog comment 了 问题:如何带参数,可以带几个参数! 回答:首先说明的是可以带N个参数例如第三个函数中

function comments($id,$cid,$vid){
			echo "blog commnet!{$id},{$cid},{$vid}";
		}
1
2
3

此时用http://yourdomain/index.php/blog/comment/Bob/love/you 地址访问那么结果就显示 blog comment Bob,love,you 如果要显示在自己设置的前台页面里 $this->load->view(‘blog’); 这句话就是调用了 system/application/views/blog.php 模板文件了 以下是控制器中传数据到视图页面 例如控制器文件

<?php
	class Blog extends CI_Controller{
		function index(){
			$data = array('title'=>"欢迎进入5dayi,com",
						  "heading"=>"欢迎",
						  "message"=>"5dayi,com");
			$this->load->view("blog",$data);
			echo "come blog";
		}
		function comment(){
			echo "blog comment!";
		}
		function comments($id,$cid,$vid){
			echo "blog commnet!{$id},{$cid},{$vid}";
		}
	}
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

视图文件

<html>
	<head>
		<title><?=$title?></title>
	</head>
	<body>
		你好,<?=$heading?>进入<?=$message?>
	</body>
</html>
1
2
3
4
5
6
7
8

此时运行http://yourdomain/index.php/blog/可查看到结果为:come blog 你好,欢迎进入5dayi,com。 参考资料 CodeIgniter 入门与简单应用 此参考资料版本略低,请取舍着看

预览
Loading comments...
0 条评论

暂无数据

example
预览