CodeIgniter學習筆記(二)——CI中的控制器

經過CI建立本身的工程,只須要將CI壓縮包中的application目錄、system目錄和index.php文件拷貝到本身的工程目錄就能夠了。本身的代碼徹底在application目錄中編輯,system目錄不要修改,之後CI出了新版本的時候,只須要替換掉system文件的內容就能夠了,若是自行修改,升級就會遇到麻煩。php

 

拷貝完成後,經過URL打開新工程的首頁:http://localhost:8080/testCodeIgniter/html

`4D%7NDFSY1V}96]HU_K5]S

經過這個頁面,CI提示咱們當前展現的視圖是在welcome_message.php文件定義的,當前使用的控制器是Welcome.phpapp

打開/application/controllers/Welcome.php文件,這個文件只有一個index方法,方法中加載了視圖welcome_messageide

<?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');
    }
}
?>

視圖文件welcome_message.php在/application/views目錄下codeigniter

經過URL訪問控制器使用pathinfo,格式爲:協議://域名/入口文件/控制器/方法名,對於私有方法、保護方法或如下劃線開頭的方法,不能經過pathinfo訪問測試

在上面的控制文件Welcome.php中新增test方法:ui

public function test()
{
    echo "這是Welcome控制器的test方法";
}

經過pathinfo(http://localhost:8080/testCodeIgniter/index.php/Welcome/test)就能夠調用到Welcome控制器的test方法this

新建一個user控制器,包括一個index方法url

<?php
class User extends CI_Controller 
{
    public function index()
    {
        echo 'user---index';
    }
}
?>

控制器須要從CI_Controller類繼承spa

經過pathinfo能夠訪問user控制器的index方法:http://localhost:8080/testCodeIgniter/index.php/user/index

注:兄弟連視頻中提到pathinfo中區分大小寫,通過使用CI3.0版本測試,是不區分大小寫的

相關文章
相關標籤/搜索