创建并运行一个CodeIgniter项目
什么是CodeIgniter?
CodeIgniter是一个轻量级的PHP框架,适合开发快速高效的Web应用程序。它遵循MVC(模型-视图-控制器)设计模式,提供了一套丰富的功能和工具,使开发者能够快速构建高质量的Web应用程序。由于其简单性、高性能和易学性,CodeIgniter在开发者中非常受欢迎。
CodeIgniter的主要特性和优势
- 轻量级:CodeIgniter的核心系统非常小,仅包含必要的组件,使其加载速度非常快。
- 高性能:CodeIgniter以其高效的性能和速度而闻名,能够处理高并发。
- 易学:提供简单易用的API和全面的文档,使开发者能够快速入门。
- 灵活性:允许开发者自由选择和使用第三方库和插件,使扩展和自定义变得方便。
- 强大的社区支持:拥有活跃的开发者社区和丰富的第三方扩展。
CodeIgniter可以帮助开发者快速构建高性能、高质量的Web应用程序,适用于任何规模的项目。
使用ServBay创建并运行一个CodeIgniter项目
在本文中,我们将使用ServBay的PHP环境来创建并运行一个CodeIgniter项目。我们将利用ServBay的“Host”功能来设置Web服务器,并通过简单配置实现项目访问。
注意: 如果你是NGINX或Apache用户
ServBay使用Caddy作为默认的Web服务器。对于从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
配置Web服务器
使用ServBay的“Host”功能通过Web服务器访问CodeIgniter项目。在ServBay的“Host”设置中添加一个新主机:
- Name:
My First CodeIgniter Dev Site
- Domain:
servbay-codeigniter-test.local
- Site Type:
PHP
- PHP Version: 选择
8.3
- Site Root Directory:
/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
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
关系数据库示例
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并应用到你的项目中。