標籤:
通過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中的控制器