介绍不写了,懒的写。既然读者看到这篇文章,你就知道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:
- class Welcome extends CI_Controller {
- /**
- * Index Page for this controller.
- *
- * Maps to the following URL
- * http://example.com/index.php/welcome
- * - or -
- * http://example.com/index.php/welcome/index
- * - or -
- * Since this controller is set as the default controller in
- * config/routes.php, it's displayed at http://example.com/
- *
- * So any other public methods not prefixed with an underscore will
- * map to /index.php/welcome/<method_name>
- * @see http://codeigniter.com/user_guide/general/urls.html
- */
- public function index()
- {
- $this->load->view('welcome_message');
- }
- }
我们看到代码里是如何调用的view
我们去找下view目录里面确实有这么个页面:welcome_message.php
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>Welcome to CodeIgniter</title>
- <style type="text/css">
- ::selection{ background-color: #E13300; color: white; }
- ::moz-selection{ background-color: #E13300; color: white; }
- ::webkit-selection{ background-color: #E13300; color: white; }
- body {
- background-color: #fff;
- margin: 40px;
- font: 13px/20px normal Helvetica, Arial, sans-serif;
- color: #4F5155;
- }
- a {
- color: #003399;
- background-color: transparent;
- font-weight: normal;
- }
- h1 {
- color: #444;
- background-color: transparent;
- border-bottom: 1px solid #D0D0D0;
- font-size: 19px;
- font-weight: normal;
- margin: 0 0 14px 0;
- padding: 14px 15px 10px 15px;
- }
- code {
- font-family: Consolas, Monaco, Courier New, Courier, monospace;
- font-size: 12px;
- background-color: #f9f9f9;
- border: 1px solid #D0D0D0;
- color: #002166;
- display: block;
- margin: 14px 0 14px 0;
- padding: 12px 10px 12px 10px;
- }
- #body{
- margin: 0 15px 0 15px;
- }
- p.footer{
- text-align: right;
- font-size: 11px;
- border-top: 1px solid #D0D0D0;
- line-height: 32px;
- padding: 0 10px 0 10px;
- margin: 20px 0 0 0;
- }
- #container{
- margin: 10px;
- border: 1px solid #D0D0D0;
- -webkit-box-shadow: 0 0 8px #D0D0D0;
- }
- </style>
- </head>
- <body>
- <div id="container">
- <h1>Welcome to CodeIgniter!</h1>
- <div id="body">
- <p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
- <p>If you would like to edit this page you'll find it located at:</p>
- <code>application/views/welcome_message.php</code>
- <p>The corresponding controller for this page is found at:</p>
- <code>application/controllers/welcome.php</code>
- <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>
- </div>
- <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
- </div>
- </body>
- </html>
MVC嘛,前面是controller和view了,再看还有个models模型目录。
当然现在还没有用的模型。
4.现在我们依葫芦画个瓢吧,来个hello,world。
我们在controllers目录里面建个hello.php文件:
- <?php
- class Hello extends CI_Controller
- {
- function index()
- {
- echo "Hello,World!";
- }
- }
- ?>
问:控制器名字和文件名必须一致吗?
这里我们直接在控制器里echo输出的,没有使用View视图.
换成中文试试:
- <?php
- class Hello extends CI_Controller
- {
- function index()
- {
- echo "你好,世界!";
- }
- }
- ?>
访问http://localhost:82/dayup/ci2/index.php/Hello/index
和默认页面类似,控制器方法index也可以省略
http://127.0.0.1:82/dayup/ci2/index.php/Hello/
这样和上面等价,如果其他方法就需要明确指明了。
- <?php
- class Hello extends CI_Controller
- {
- function index()
- {
- echo "你好,世界!";
- }
- function other()
- {
- echo "另一个.";
- }
- }
- ?>
http://127.0.0.1:82/dayup/ci2/index.php/Hello/other
这些方法也可以带参数:
- <?php
- class Hello extends CI_Controller
- {
- function index()
- {
- echo "你好,世界!";
- }
- function other($id,$name)
- {
- echo "ID=>{$id},Name=>{$name}";
- }
- }
- ?>
测试下,http://127.0.0.1:82/dayup/ci2/index.php/Hello/other/1/Jack
输出:
- ID=>1,Name=>Jack
完毕!
缺省控制器可以修改通过配置路由
application\config里面的routes.php
- $route['default_controller'] = "welcome";
改成
- $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