Create and Run a CodeIgniter Project
What is CodeIgniter?
CodeIgniter is a lightweight PHP framework suitable for developing fast and efficient web applications. It follows the MVC (Model-View-Controller) design pattern, offering a rich set of features and tools that enable developers to quickly build high-quality web applications. Known for its simplicity, high performance, and ease of learning, CodeIgniter is a popular choice among developers.
Main Features and Advantages of CodeIgniter
- Lightweight: The core system of CodeIgniter is very small, containing only the necessary components, which makes it load very quickly.
- High Performance: CodeIgniter is renowned for its efficient performance and speed, capable of handling high concurrency.
- Easy to Learn: Provides a simple and easy-to-use API along with comprehensive documentation, allowing developers to rapidly get started.
- Flexibility: Allows developers to freely choose and use third-party libraries and plugins, making extensions and customizations convenient.
- Strong Community Support: There is an active developer community and a wealth of third-party extensions.
CodeIgniter can help developers quickly build high-performance, high-quality web applications, suitable for projects of any scale.
Create and Run a CodeIgniter Project Using ServBay
In this article, we will use ServBay’s PHP environment to create and run a CodeIgniter project. We will utilize ServBay's 'Host' function to set up the web server and achieve project access through simple configuration.
Note: If you were a NGINX or Apache user
ServBay uses Caddy as the default web server. For users migrating from NGINX and Apache to ServBay, there are some key changes to be aware of:
Caddy Configuration
Caddy is already embedded in ServBay, and the default configuration has been optimized and debugged. Developers only need to manage the site through ServBay's ‘Host’ function without manually modifying Caddy configuration files.
Rewrite Rules and .htaccess
In NGINX and Apache, developers usually need to write their own rewrite rules and .htaccess files for URL rewriting and other configurations. However, with ServBay, the rules for Caddy come pre-configured, meaning that unless there's a special requirement, developers do not need to write these rules themselves.
Learn More
For more information, please refer to Rewrite and htaccess, How to Migrate Apache Website to ServBay, How to Migrate NGINX Website to ServBay.
Create a CodeIgniter Project
TIP
ServBay recommends developers to place their websites in the /Applications/ServBay/www
directory for easier management.
Install Composer
Composer comes pre-installed with ServBay, so there's no need to install it separately.
Create a CodeIgniter Project
Use Composer to create a new CodeIgniter project:
bashcd /Applications/ServBay/www mkdir servbay-codeigniter-app cd servbay-codeigniter-app composer create-project codeigniter4/appstarter .
1
2
3
4Enter Project Directory
Enter the newly created CodeIgniter project directory:
bashcd /Applications/ServBay/www/servbay-codeigniter-app
1
Initial Configuration
Configure Database Connection
Configure the database connection settings in the
app/Config/Database.php
file: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
Configure the Web Server
Use ServBay's 'Host' function to access the CodeIgniter project via the web server. Add a new host in ServBay's 'Host' settings:
- Name:
My First CodeIgniter Dev Site
- Domain:
servbay-codeigniter-test.local
- Site Type:
PHP
- PHP Version: Select
8.3
- Site Root Directory:
/Applications/ServBay/www/servbay-codeigniter-app/public
For detailed setup steps, please refer to Adding the First Website.
Add Sample Code
Add the following code in the app/Controllers/Home.php
file:
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
Access the Website
Open your browser and visit the following URL:
https://servbay-codeigniter-test.local
: You will see the page outputHello ServBay!
.
NoSQL Database Examples
Memcached Example
Install Memcached Extension
The Memcached extension is pre-installed in ServBay, so no additional installation is required.
Configure Memcached
Configure the Memcached connection information in the
app/Config/Cache.php
file:phppublic $memcached = [ 'host' => '127.0.0.1', 'port' => 11211, 'weight' => 1, ];
1
2
3
4
5Use Memcached
Use cache in the controller:
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
7Add route to
app/Config/Routes.php
:php$routes->get('/memcached', 'Home::memcached');
1Open a browser and visit https://servbay-codeigniter-test.local/memcached
Redis Example
Install Redis Extension
The Redis extension is pre-installed in ServBay, so no additional installation is required.
Configure Redis
Configure the Redis connection information in the
app/Config/Cache.php
file: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
9Use Redis
Use cache in the controller:
phppublic function redis() { $redis = \Config\Services::cache(); $redis->save('key', 'Hello Redis!'); $value = $redis->get('key'); return $value; }
1
2
3
4
5
6
7Add route to
app/Config/Routes.php
:php$routes->get('/redis', 'Home::redis');
1Open a browser and visit https://servbay-codeigniter-test.local/redis
Relational Database Examples
MySQL Example
Configure MySQL
Configure the MySQL connection information in the
app/Config/Database.php
file: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
19Create Migration File
Use CodeIgniter's CLI tool to create a migration file:
bashphp spark make:migration create_users_table
1Edit Migration File
Find the newly created migration file in the
app/Database/Migrations
directory and edit it to define the database table structure: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
32Run Migration
Use the migration command of CodeIgniter to run the migration and create the database table:
bashphp spark migrate
1Add Route
Add the following routes to
app/Config/Routes.php
:php$routes->get('/mysql-add', 'Home::mysqlAdd'); $routes->get('/mysql', 'Home::mysql');
1
2Insert User Data
Insert user data in the controller:
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
9Use MySQL
Call the database in the controller:
phppublic function mysql() { $db = \Config\Database::connect(); $users = $db->table('users')->get()->getResult(); return json_encode($users); }
1
2
3
4
5
6Open a browser and visit
https://servbay-codeigniter-test.local/mysql-add
andhttps://servbay-codeigniter-test.local/mysql
PostgreSQL Example
Configure PostgreSQL
Configure the PostgreSQL connection information in the
app/Config/Database.php
file: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
19Create Migration File
Use CodeIgniter's CLI tool to create a migration file:
bashphp spark make:migration create_users_table
1Edit Migration File
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
32Run Migration
Use the migration command of CodeIgniter to run the migration and create the database table:
bashphp spark migrate
1Add Routes
Add the following routes to
app/Config/Routes.php
:php$routes->get('/pgsql-add', 'Home::pgsqlAdd'); $routes->get('/pgsql', 'Home::pgsql');
1
2Insert User Data
Insert user data in the controller:
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
9Use PostgreSQL
Call the database in the controller:
phppublic function pgsql() { $db = \Config\Database::connect(); $users = $db->table('users')->get()->getResult(); return json_encode($users); }
1
2
3
4
5
6Open a browser and visit
https://servbay-codeigniter-test.local/p;pgsql-add
andhttps://servbay-codeigniter-test.local/pgsql
Through the above steps, you have successfully created and run a CodeIgniter project, used ServBay features to manage and access your project, and connected to multiple databases to interact with data. I hope this article helps you quickly get started with CodeIgniter and apply it to your projects.