介紹不寫了,懶的寫。既然讀者看到這篇文章,你就知道CI是幹嗎的了。php
開始吧:css
1.去官網下載框架代碼html
咱們到中文站下個2.1的穩定版本web
http://codeigniter.org.cn/downloadsapache
Over了,下載完解壓壓縮包到你的站點目錄,學習的話基本都是在本地,咱們這裏放到以下子目錄dayup裏了,框架文件夾我重命名成ci2了:app
$$根目錄$$\dayup\ci2框架
好了訪問以下地址,我把apache配置在了82端口,因此是ide
http://localhost:82/dayup/ci2codeigniter
看到一個歡迎界面學習
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