CodeIgniter安裝和入門使用(一)

CodeIgniter是個輕量級功能也強大的框架,適合作本身作小項目用,本文介紹CodeIgniter的安裝和使用。
安裝

官網連接http://codeigniter.org.cn/user_guide/installation/downloads.html ,最新的版本爲3.0.2,直接git下載下來就ok。安裝說明以下:

1,解壓壓縮包,而後將文件夾的內容所有上傳到服務器上的web根目錄,如/www/web/.

2,如何是本地的話,能夠直接將文件夾改爲本身項目的名字,放到本身的web根目錄下,如mac上是

/Users/yanzi/Sites/,將解壓縮後的文件夾更名爲CodeIgniter;

3,找到application/config/config.php,設置網站根目錄:(如下都是以部署在本地爲例)

$config['base_url'] = 'http://localhost/~yanzi/CodeIgniter/';


若是有數據庫的話,找到 application/config/database.php設置相關參數便可。

其餘的基本不用設置了,可是因爲CI是單入口的,即全部的訪問都要先通過網站一級目錄下的index.php.有幾個參數仍是要多注意下,打開根目錄下的/Users/yanzi/Sites/CodeIgniter/index.php,找到如下幾行:

$system_path = 'system';

$application_folder = 'application';

$view_folder = '';

前兩個基本不用動,view_folder是空,可是看如下代碼可知:php

  // The path to the "views" folder
        if ( ! is_dir($view_folder))
        {
            if ( ! empty($view_folder) && is_dir(APPPATH.$view_folder.DIRECTORY_SEPARATOR))
            {
                $view_folder = APPPATH.$view_folder;
            }
            elseif ( ! is_dir(APPPATH.'views'.DIRECTORY_SEPARATOR))
            {
                header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
                echo 'Your view folder path does not appear to be set correctly. Please open the following file and correct this: '.SELF;
                exit(3); // EXIT_CONFIG
            }
            else
            {
                $view_folder = APPPATH.'views';
            }
        }

默認的是

$view_folder = APPPATH.'views';

通過上面步驟,瀏覽器輸入:http://localhost/~yanzi/CodeIgniter/ (windows上輸入本身對應的根目錄便可)看到以下:


看到上圖表示CodeIgniter安裝ok。

下面簡單看下,爲何會顯示這個界面。

找到/Users/yanzi/Sites/CodeIgniter/application/config/routes.php,能夠看到以下代碼:html

   $route['default_controller'] = 'welcome';
    $route['404_override'] = '';
    $route['translate_uri_dashes'] = FALSE;

即默認訪問的Controller是welcome,也即/Users/yanzi/Sites/CodeIgniter/application/controllers/Welcome.php,

  git

 <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
     
    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');
    //        echo "HelloWorld!";
        }
    }

能夠看到這個controller加載了welcome_message.php,在view文件夾下。

加載靜態頁面的demo

對於連接http://example.com/news/latest/10 咱們能夠猜想訪問了news的控制器裏的latest的方法,且查詢id爲10的內容。

即一般URL以下所示:http://example.com/[controller-class]/[controller-method]/[arguments]

下面介紹一個加載靜態頁面的demo.

1,在application/controllers/新建Pages.php, 這就是一個Controller,繼承CI_Controller,代碼以下:web

<?php
     
    /**
     * Created by PhpStorm.
     * User: yanzi
     * Date: 15/10/21
     * Time: 下午8:20
     */
    class Pages extends CI_Controller
    {
        public function index($page = 'home')
        {
            
        }
    }

2,在views文件夾下新建templates文件夾,新建header.php:數據庫

    <html>
        <head>
            <title>CodeIgniter Tutorial</title>
        </head>
        <body>
     
            <h1><?php echo $title; ?></h1>

新建footer.php:

    <em>&copy; 2015</em>
    </body>
    </html>

注意:這兩個問題都必須是php,不能是html!這是CI的模版決定的。

在views文件夾下新建pages文件夾,新建home.php about.php,這是具體的界面。

在views文件夾下新建pages文件夾,裏面新建home.php about.php,這裏就是最終你要訪問的界面。依次在界面裏寫一句話:

<h1>This is Home page</h1>

<h1>This is About page</h1>  

3,接着是完善控制器裏的view方法。

最終的控制器是:windows

 class Pages extends CI_Controller
    {
        public function view($page = 'home')
        {
            if(!file_exists(APPPATH. '/views/pages/' . $page . '.php')){
                show_404();
            }else{
                $data['title'] = ucfirst($page); // Capitalize the first letter
                $this->load->view('templates/header', $data);
                $this->load->view('pages/'.$page, $data);
                $this->load->view('templates/footer', $data);
            }
        }
    }

注意:這裏將$page參數又賦給了data數組,對應key爲title。這樣在load->view()傳給php時,又去讀的$title參數。這個名字必須對應!

通過上述步驟,不須要配置任何東西就能夠訪問了。瀏覽器輸入:

http://localhost/~yanzi/CodeIgniter/index.php/pages/view

能夠看到默認參數爲home的界面:


輸入http://localhost/~yanzi/CodeIgniter/index.php/pages/view/about 將會看到about的界面。

注意:

1,url = site地址/index.php/控制器名字/方法名字/參數

2,當只輸入url = site地址/index.php/控制器名字/默認訪問的是控制器的index方法。若是函數名爲index,則不用加方法名。可是當你傳參數時仍是要寫上方法名字。

3,能夠經過路由規則,簡化url。

如找到config文件夾下的router.php,註釋掉原來的加上這兩句:

$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';


則瀏覽器輸入http://localhost/~yanzi/CodeIgniter/index.php/about  直接顯示about界面內容,自動使用默認的pages控制器裏的view方法,且將參數 做爲$1傳過去。

有如下幾個經典的路由:

a,

$route['product/:num'] = 'catalog/product_lookup';

url 訪問example.com/product/1/ 則自動觸發的是catalog控制器。

b,

$route['journals'] = 'blogs'; //重定向

c,

$route['blog/joe'] = 'blogs/users/34';

也是重定向。

關於路由這塊能夠參考:http://codeigniter.org.cn/user_guide/general/routing.html

參考:http://codeigniter.org.cn/user_guide/tutorial/static_pages.html
原文:https://blog.csdn.net/yanzi1225627/article/details/49233281

api

相關文章
相關標籤/搜索