建立與運行 CodeIgniter 專案
什麼是 CodeIgniter?
CodeIgniter 是一個輕量級的 PHP 框架,適合開發快速且高效的網頁應用程式。它遵循 MVC(模型-視圖-控制器)設計模式,提供一套豐富的功能和工具,使開發者可以迅速建立高質量的網頁應用程式。CodeIgniter 以其簡單、高效能和易學性著稱,是開發者間的熱門選擇。
CodeIgniter 的主要特色和優勢
- 輕量級:CodeIgniter 的核心系統非常小,只包含必要的組件,使其載入速度非常快。
- 高效能:CodeIgniter 以其高效的性能和速度而聞名,能夠處理高並發量。
- 易於學習:提供簡單易懂的 API 與完整的文檔,讓開發者可以快速上手。
- 靈活性:允許開發者自由選擇和使用第三方庫和插件,方便擴展和定制。
- 強大的社群支持:有一個活躍的開發者社群和豐富的第三方擴展。
CodeIgniter 可以幫助開發者快速建立高效能、高質量的網頁應用程式,適用於各種規模的專案。
使用 ServBay 建立並運行 CodeIgniter 專案
本文中,我們將使用 ServBay 的 PHP 環境來建立並運行一個 CodeIgniter 專案。我們將利用 ServBay 的 'Host' 功能來設置網頁伺服器,通過簡單的配置進行專案訪問。
注意:如果你是 NGINX 或 Apache 用戶
ServBay 使用 Caddy 作為默認網頁伺服器。對於從 NGINX 和 Apache 遷移到 ServBay 的用戶,有一些關鍵變更需要了解:
Caddy 配置
Caddy 已經內嵌在 ServBay 中,並且默認配置已經優化和調試過。開發者只需通過 ServBay 的 'Host' 功能管理網站,無需手動修改 Caddy 配置文件。
重寫規則和 .htaccess
在 NGINX 和 Apache 中,開發者通常需要自行編寫重寫規則和 .htaccess 文件來進行 URL 重寫等配置。然而在 ServBay 中,Caddy 的規則已經預先配置,除非有特別需要,否則開發者無需自行編寫這些規則。
了解更多
更多信息請參見 Rewrite and htaccess, How to Migrate Apache Website to ServBay, How to Migrate NGINX Website to ServBay.
建立 CodeIgniter 專案
TIP
ServBay 建議開發者將網站放置在 /Applications/ServBay/www
目錄中,便於管理。
安裝 Composer
Composer 已經預裝在 ServBay 中,無需額外安裝。
建立 CodeIgniter 專案
使用 Composer 建立一個新的 CodeIgniter 專案:
bashcd /Applications/ServBay/www mkdir servbay-codeigniter-app cd servbay-codeigniter-app composer create-project codeigniter4/appstarter .
1
2
3
4進入專案目錄
進入新創建的 CodeIgniter 專案目錄:
bashcd /Applications/ServBay/www/servbay-codeigniter-app
1
初始配置
配置資料庫連接
配置
app/Config/Database.php
文件中的資料庫連接設置:phppublic $default = [ 'DSN' => '', 'hostname' => '127.0.0.1', 'username' => 'root', 'password' => 'password', 'database' => 'servbay_codeigniter_app', 'DBDriver' => 'MySQLi', 'DBPrefix' => '', 'pConnect' => false, 'DBDebug' => (ENVIRONMENT !== 'production'), 'charset' => 'utf8mb4', 'DBCollat' => 'utf8mb4_general_ci', 'swapPre' => '', 'encrypt' => false, 'compress' => false, 'strictOn' => false, 'failover' => [], 'port' => 3306, ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
配置網頁伺服器
使用 ServBay 的 'Host' 功能通過網頁伺服器訪問 CodeIgniter 專案。在 ServBay 的 'Host' 設置中新增一個主機:
- 名稱:
My First CodeIgniter Dev Site
- 域名:
servbay-codeigniter-test.local
- 網站類型:
PHP
- PHP 版本:選擇
8.3
- 網站根目錄:
/Applications/ServBay/www/servbay-codeigniter-app/public
詳細的設置步驟,請參見 Adding the First Website。
添加示例代碼
在 app/Controllers/Home.php
文件中添加以下代碼:
namespace App\Controllers;
use CodeIgniter\Controller;
class Home extends Controller
{
public function index()
{
return 'Hello ServBay!';
}
public function memcached()
{
$cache = \Config\Services::cache();
$cache->save('key', 'Hello Memcached!', 60);
$value = $cache->get('key');
return $value;
}
public function redis()
{
$redis = \Config\Services::cache();
$redis->set('key', 'Hello Redis!');
$value = $redis->get('key');
return $value;
}
public function mysqlAdd()
{
$db = \Config\Database::connect();
$db->table('users')->insert([
'name' => 'ServBay',
'email' => '[email protected]',
]);
return 'User added';
}
public function mysql()
{
$db = \Config\Database::connect();
$users = $db->table('users')->get()->getResult();
return json_encode($users);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
訪問網站
打開瀏覽器並訪問以下 URL:
https://servbay-codeigniter-test.local
:你將看到頁面輸出Hello ServBay!
。
NoSQL 資料庫示例
Memcached 示例
安裝 Memcached 擴展
Memcached 擴展已經預裝在 ServBay 中,無需額外安裝。
配置 Memcached
在
app/Config/Cache.php
文件中配置 Memcached 的連接資訊:phppublic $memcached = [ 'host' => '127.0.0.1', 'port' => 11211, 'weight' => 1, ];
1
2
3
4
5使用 Memcached
在控制器中使用緩存:
phppublic function memcached() { $cache = \Config\Services::cache(); $cache->save('key', 'Hello Memcached!', 60); $value = $cache->get('key'); return $value; }
1
2
3
4
5
6
7在
app/Config/Routes.php
中添加路由:php$routes->get('/memcached', 'Home::memcached');
1打開瀏覽器並訪問
https://servbay-codeigniter-test.local/memcached
Redis 示例
安裝 Redis 擴展
Redis 擴展已經預裝在 ServBay 中,無需額外安裝。
配置 Redis
在
app/Config/Cache.php
文件中配置 Redis 的連接資訊:phppublic string $handler = 'redis'; public $default = [ 'host' => '127.0.0.1', 'password' => null, 'port' => 6379, 'timeout' => 0, 'database' => 0, ];
1
2
3
4
5
6
7
8
9使用 Redis
在控制器中使用緩存:
phppublic function redis() { $redis = \Config\Services::cache(); $redis->save('key', 'Hello Redis!'); $value = $redis->get('key'); return $value; }
1
2
3
4
5
6
7在
app/Config/Routes.php
中添加路由:php$routes->get('/redis', 'Home::redis');
1打開瀏覽器並訪問
https://servbay-codeigniter-test.local/redis
關係型資料庫示例
MySQL 示例
配置 MySQL
在
app/Config/Database.php
文件中配置 MySQL 的連接資訊:phppublic $default = [ 'DSN' => '', 'hostname' => '127.0.0.1', 'username' => 'root', 'password' => 'password', 'database' => 'servbay_codeigniter_app', 'DBDriver' => 'MySQLi', 'DBPrefix' => '', 'pConnect' => false, 'DBDebug' => (ENVIRONMENT !== 'production'), 'charset' => 'utf8mb4', 'DBCollat' => 'utf8mb4_general_ci', 'swapPre' => '', 'encrypt' => false, 'compress' => false, 'strictOn' => false, 'failover' => [], 'port' => 3306, ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19建立遷移文件
使用 CodeIgniter 的 CLI 工具來建立一個遷移文件:
bashphp spark make:migration create_users_table
1編輯遷移文件
在
app/Database/Migrations
目錄中找到新建的遷移文件並編輯它,定義資料庫表的結構:phpuse CodeIgniter\Database\RawSql; public function up() { $this->forge->addField([ 'id' => [ 'type' => 'INT', 'constraint' => 5, 'unsigned' => true, 'auto_increment' => true, ], 'name' => [ 'type' => 'VARCHAR', 'constraint' => '100', ], 'email' => [ 'type' => 'VARCHAR', 'constraint' => '100', 'unique' => true, ], 'created_at' => [ 'type' => 'TIMESTAMP', 'default' => new RawSql('CURRENT_TIMESTAMP'), ], 'updated_at' => [ 'type' => 'TIMESTAMP', 'default' => new RawSql('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), ], ]); $this->forge->addKey('id', true); $this->forge->createTable('users'); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32運行遷移
使用 CodeIgniter 的遷移命令運行遷移,創建資料庫表:
bashphp spark migrate
1新增路由
在
app/Config/Routes.php
中添加以下路由:php$routes->get('/mysql-add', 'Home::mysqlAdd'); $routes->get('/mysql', 'Home::mysql');
1
2插入用戶資料
在控制器中插入用戶資料:
phppublic function mysqlAdd() { $db = \Config\Database::connect(); $db->table('users')->insert([ 'name' => 'ServBay', 'email' => '[email protected]', ]); return 'User added'; }
1
2
3
4
5
6
7
8
9使用 MySQL
在控制器中調用資料庫:
phppublic function mysql() { $db = \Config\Database::connect(); $users = $db->table('users')->get()->getResult(); return json_encode($users); }
1
2
3
4
5
6打開瀏覽器並訪問
https://servbay-codeigniter-test.local/mysql-add
和https://servbay-codeigniter-test.local/mysql
PostgreSQL 示例
配置 PostgreSQL
在
app/Config/Database.php
文件中配置 PostgreSQL 的連接資訊:phppublic $default = [ 'DSN' => '', 'hostname' => '127.0.0.1', 'username' => 'root', 'password' => 'password', 'database' => 'servbay_codeigniter_app', 'DBDriver' => 'Postgre', 'DBPrefix' => '', 'pConnect' => false, 'DBDebug' => (ENVIRONMENT !== 'production'), 'charset' => 'utf8', 'DBCollat' => 'utf8_general_ci', 'swapPre' => '', 'encrypt' => false, 'compress' => false, 'strictOn' => false, 'failover' => [], 'port' => 5432, ];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19建立遷移文件
使用 CodeIgniter 的 CLI 工具來建立一個遷移文件:
bashphp spark make:migration create_users_table
1編輯遷移文件
phpuse CodeIgniter\Database\RawSql; public function up() { $this->forge->addField([ 'id' => [ 'type' => 'INT', 'constraint' => 5, 'unsigned' => true, 'auto_increment' => true, ], 'name' => [ 'type' => 'VARCHAR', 'constraint' => '100', ], 'email' => [ 'type' => 'VARCHAR', 'constraint' => '100', 'unique' => true, ], 'created_at' => [ 'type' => 'TIMESTAMP', 'default' => new RawSql('NOW()'), ], 'updated_at' => [ 'type' => 'TIMESTAMP', 'default' => new RawSql('NOW()'), ], ]); $this->forge->addKey('id', true); $this->forge->createTable('users'); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32運行遷移
使用 CodeIgniter 的遷移命令運行遷移,創建資料庫表:
bashphp spark migrate
1新增路由
在
app/Config/Routes.php
中添加以下路由:php$routes->get('/pgsql-add', 'Home::pgsqlAdd'); $routes->get('/pgsql', 'Home::pgsql');
1
2插入用戶資料
在控制器中插入用戶資料:
phppublic function pgsqlAdd() { $db = \Config\Database::connect(); $db->table('users')->insert([ 'name' => 'ServBay', 'email' => '[email protected]', ]); return 'User added'; }
1
2
3
4
5
6
7
8
9使用 PostgreSQL
在控制器中調用資料庫:
phppublic function pgsql() { $db = \Config\Database::connect(); $users = $db->table('users')->get()->getResult(); return json_encode($users); }
1
2
3
4
5
6打開瀏覽器並訪問
https://servbay-codeigniter-test.local/p;pgsql-add
和https://servbay-codeigniter-test.local/pgsql
通過以上步驟,您已經成功建立並運行了一個 CodeIgniter 專案,利用 ServBay 的功能管理和訪問專案,並連接多個資料庫進行數據交互。希望本文能幫助您快速上手 CodeIgniter 並應用於您的專案。