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

來源:互聯網
上載者:User

標籤:

通過CI建立自己的工程,只需要將CI壓縮包中的application目錄、system目錄和index.php檔案拷貝到自己的工程目錄就可以了。自己的代碼完全在application目錄中編輯,system目錄不要修改,以後CI出了新版本的時候,只需要替換掉system檔案的內容就可以了,如果自行修改,升級就會遇到麻煩。

 

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

通過這個頁面,CI提示我們當前展示的視圖是在welcome_message.php檔案定義的,當前使用的控制器是Welcome.php

開啟/application/controllers/Welcome.php檔案,這個檔案只有一個index方法,方法中載入了視圖welcome_message

<?phpdefined(‘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目錄下

通過URL存取控制器使用pathinfo,格式為:協議://網域名稱/入口檔案/控制器/方法名,對於私人方法、保護方法或以底線開頭的方法,不能通過pathinfo訪問

在上面的控制檔案Welcome.php中新增test方法:

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

通過pathinfo(http://localhost:8080/testCodeIgniter/index.php/Welcome/test)就可以調用到Welcome控制器的test方法

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

<?phpclass User extends CI_Controller {    public function index()    {        echo ‘user---index‘;    }}?>

控制器需要從CI_Controller類繼承

通過pathinfo可以訪問user控制器的index方法:http://localhost:8080/testCodeIgniter/index.php/user/index

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

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.