這是一個CI的網址
http(s)://example.com/(index.php)/<控制器類別Controller-class>/<控制器方法Controller-method>/<參數arguments>
=> 控制器類別 Controller-class:繼承CI_Controller的Class
=> 控制器方法 Controller-method:Class裡面的Function
=> 參數 arguments:Function接收的參數
Models: application/models
Views: application/views
Controllers: application/controllers
/application/controllers/Test.php
<?php
defined('BASEPATH')or http_response_code(403); //禁止直接訪問檔案
Class Test extends CI_Controllers {
public function index() { //index是這個Controller的首頁
}
}
?>
我們需要用Controller傳遞資料,繼承CI_Controller就可以了。
NOTE:class name的首字要大寫!
/application/views/test-index.php
<?php
defined('BASEPATH')or http_response_code(403); //禁止直接訪問檔案
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Test</title>
</head>
<body>
<h1>HELLO WORLD!</h1>
</body>
</html>
利用Controller載入View
/application/controllers/Test.php
defined('BASEPATH')or http_response_code(403); //禁止直接訪問檔案
Class Test extends CI_Controllers {
public function index() { //index是這個Controller的首頁
$this->load->view('test-index');
}
}
?>
這個時候查看 http://example.com/test/
就會看到結果了!
利用Controller傳送參數至View
Controller /application/controllers/Test.php
defined('BASEPATH')or http_response_code(403); //禁止直接訪問檔案
Class Test extends CI_Controllers {
public function index($test) { //index是這個Controller的首頁
$send = array(
"testdata" => $test
);
$this->load->view('test-index',$send);
}
}
?>
View /application/views/test-index.php
<?php
defined('BASEPATH')or http_response_code(403); //禁止直接訪問檔案
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Test</title>
</head>
<body>
<h1><?php echo $testdata; ?></h1>
</body>
</html>