介绍不写了,懒的写。既然读者看到这篇文章,你就知道CI是干嘛的了。

开始吧:

1.去官网下载框架代码

我们到中文站下个2.1的稳定版本

http://codeigniter.org.cn/downloads

Over了,下载完解压压缩包到你的站点目录,学习的话基本都是在本地,我们这里放到如下子目录dayup里了,框架文件夹我重命名成ci2了:

$$根目录$$\dayup\ci2

好了访问如下地址,我把apache配置在了82端口,所以是

http://localhost:82/dayup/ci2

看到一个欢迎界面

Welcome to CodeIgniter!

2.看下ci的目录

user_guide 用户指南(用户文档,帮助文档之类的,这个开发时很重要!)

system 是ci的代码啦,必须的。

application 这个嘛就是放你应用代码的地方啦

3.看下应用的子目录

controllers 这个是放控制器的地方

比如里面的welcome.php就是我们看到的那个欢迎界面的。

welcome.php:

 
  
  1. class Welcome extends CI_Controller { 
  2.  
  3.     /** 
  4.      * Index Page for this controller. 
  5.      * 
  6.      * Maps to the following URL 
  7.      *      http://example.com/index.php/welcome 
  8.      *  - or -   
  9.      *      http://example.com/index.php/welcome/index 
  10.      *  - or - 
  11.      * Since this controller is set as the default controller in  
  12.      * config/routes.php, it's displayed at http://example.com/ 
  13.      * 
  14.      * So any other public methods not prefixed with an underscore will 
  15.      * map to /index.php/welcome/<method_name> 
  16.      * @see http://codeigniter.com/user_guide/general/urls.html 
  17.      */ 
  18.     public function index() 
  19.     { 
  20.         $this->load->view('welcome_message'); 
  21.     } 

我们看到代码里是如何调用的view

我们去找下view目录里面确实有这么个页面:welcome_message.php

 

 
  
  1. <!DOCTYPE html> 
  2. <html lang="en"> 
  3. <head> 
  4.     <meta charset="utf-8"> 
  5.     <title>Welcome to CodeIgniter</title> 
  6.  
  7.     <style type="text/css"> 
  8.  
  9.     ::selection{ background-color: #E13300; color: white; } 
  10.     ::moz-selection{ background-color: #E13300; color: white; } 
  11.     ::webkit-selection{ background-color: #E13300; color: white; } 
  12.  
  13.     body { 
  14.         background-color: #fff; 
  15.         margin: 40px; 
  16.         font: 13px/20px normal Helvetica, Arial, sans-serif; 
  17.         color: #4F5155; 
  18.     } 
  19.  
  20.     a { 
  21.         color: #003399; 
  22.         background-color: transparent; 
  23.         font-weight: normal; 
  24.     } 
  25.  
  26.     h1 { 
  27.         color: #444; 
  28.         background-color: transparent; 
  29.         border-bottom: 1px solid #D0D0D0; 
  30.         font-size: 19px; 
  31.         font-weight: normal; 
  32.         margin: 0 0 14px 0; 
  33.         padding: 14px 15px 10px 15px; 
  34.     } 
  35.  
  36.     code { 
  37.         font-family: Consolas, Monaco, Courier New, Courier, monospace; 
  38.         font-size: 12px; 
  39.         background-color: #f9f9f9; 
  40.         border: 1px solid #D0D0D0; 
  41.         color: #002166; 
  42.         display: block; 
  43.         margin: 14px 0 14px 0; 
  44.         padding: 12px 10px 12px 10px; 
  45.     } 
  46.  
  47.     #body{ 
  48.         margin: 0 15px 0 15px; 
  49.     } 
  50.      
  51.     p.footer{ 
  52.         text-align: right; 
  53.         font-size: 11px; 
  54.         border-top: 1px solid #D0D0D0; 
  55.         line-height: 32px; 
  56.         padding: 0 10px 0 10px; 
  57.         margin: 20px 0 0 0; 
  58.     } 
  59.      
  60.     #container{ 
  61.         margin: 10px; 
  62.         border: 1px solid #D0D0D0; 
  63.         -webkit-box-shadow: 0 0 8px #D0D0D0; 
  64.     } 
  65.     </style> 
  66. </head> 
  67. <body> 
  68.  
  69. <div id="container"> 
  70.     <h1>Welcome to CodeIgniter!</h1> 
  71.  
  72.     <div id="body"> 
  73.         <p>The page you are looking at is being generated dynamically by CodeIgniter.</p> 
  74.  
  75.         <p>If you would like to edit this page you'll find it located at:</p> 
  76.         <code>application/views/welcome_message.php</code> 
  77.  
  78.         <p>The corresponding controller for this page is found at:</p> 
  79.         <code>application/controllers/welcome.php</code> 
  80.  
  81.         <p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p> 
  82.     </div> 
  83.  
  84.     <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p> 
  85. </div> 
  86.  
  87. </body> 
  88. </html> 

MVC嘛,前面是controller和view了,再看还有个models模型目录。

当然现在还没有用的模型。

4.现在我们依葫芦画个瓢吧,来个hello,world。

我们在controllers目录里面建个hello.php文件:

 

 
  
  1. <?php 
  2. class Hello extends CI_Controller 
  3.     function index() 
  4.     { 
  5.         echo "Hello,World!"
  6.     } 
  7. ?> 

问:控制器名字和文件名必须一致吗?

这里我们直接在控制器里echo输出的,没有使用View视图.

换成中文试试:

 
  
  1. <?php 
  2. class Hello extends CI_Controller 
  3.     function index() 
  4.     { 
  5.         echo "你好,世界!"
  6.     } 
  7. ?> 

访问http://localhost:82/dayup/ci2/index.php/Hello/index

和默认页面类似,控制器方法index也可以省略

http://127.0.0.1:82/dayup/ci2/index.php/Hello/

这样和上面等价,如果其他方法就需要明确指明了。

 

 
  
  1. <?php 
  2. class Hello extends CI_Controller 
  3.     function index() 
  4.     { 
  5.         echo "你好,世界!"
  6.     } 
  7.     function other() 
  8.     { 
  9.         echo "另一个."
  10.     } 
  11. ?> 

http://127.0.0.1:82/dayup/ci2/index.php/Hello/other

这些方法也可以带参数:

 

 
  
  1. <?php 
  2. class Hello extends CI_Controller 
  3.     function index() 
  4.     { 
  5.         echo "你好,世界!"
  6.     } 
  7.     function other($id,$name
  8.     { 
  9.         echo "ID=>{$id},Name=>{$name}"
  10.     } 
  11. ?> 

测试下,http://127.0.0.1:82/dayup/ci2/index.php/Hello/other/1/Jack

输出:

 

 
  
  1. ID=>1,Name=>Jack 

完毕! 

 

缺省控制器可以修改通过配置路由

application\config里面的routes.php

 

 
  
  1. $route['default_controller'] = "welcome"; 

改成

 
  
  1. $route['default_controller'] = "hello"; 

这样访问

http://127.0.0.1:82/dayup/ci2/index.php

就相当于

http://127.0.0.1:82/dayup/ci2/index.php/Hello/index