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 and provides rich features and tools that enable developers to quickly build high-quality web applications. CodeIgniter is known for its simplicity, high performance, and ease of learning, making it a preferred framework for many developers.
Key Features and Advantages of CodeIgniter
- Lightweight: The core system of CodeIgniter is very small, containing only the necessary components, resulting in very fast loading speeds.
- High Performance: CodeIgniter is known for its efficient performance and speed, capable of handling high concurrent requests.
- Easy to Learn: It provides a simple and easy-to-use API and extensive documentation, allowing developers to quickly get started.
- Flexibility: Developers can freely choose and use third-party libraries and plugins, making it easy to extend and customize functions.
- Strong Community Support: It has an active developer community and a wealth of third-party extensions.
CodeIgniter can help developers quickly build high-performance and high-quality web applications, suitable for projects of all sizes.
Create and Run a CodeIgniter Project Using ServBay
In this article, we will use the PHP environment provided by ServBay to create and run a CodeIgniter project. We will take advantage of the 'Host' feature of ServBay to set up a web server and implement project access through simple configuration.
Note: If you were an NGINX or Apache user
ServBay uses Caddy as the web server by default. For users migrating from NGINX or Apache to ServBay, there are some key changes to note:
Caddy Configuration
ServBay comes with Caddy pre-installed and optimized by default. Developers can manage sites through ServBay's 'Host' feature without manually modifying the Caddy configuration file.
Rewrite Rules and .htaccess
In NGINX and Apache, developers usually need to write their own rewrite rules and .htaccess files to handle URL rewriting and other configurations. However, ServBay already configures Caddy rules out of the box, so unless there are special requirements, 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 place their websites in the /Applications/ServBay/www
directory for easier management.
Install Composer
ServBay comes with Composer pre-installed, so there is no need for separate installation.
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 the Project Directory
Navigate to the newly created CodeIgniter project directory:
bashcd /Applications/ServBay/www/servbay-codeigniter-app
1
Initialization Configuration
Configure Database Connection
Configure the database 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
19
Configure Web Server
Use ServBay's 'Host' feature to access the CodeIgniter project through the web server. In the 'Host' settings of ServBay, add a new host:
- Name:
My First CodeIgniter Dev Site
- Domain:
servbay-codeigniter-test.local
- Website Type:
PHP
- PHP Version: Choose
8.3
- Website Root Directory:
/Applications/ServBay/www/servbay-codeigniter-app/public
For detailed setup steps, please refer to Adding the First Website.
Add Example 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
In ServBay, the Memcached extension is pre-installed, so no additional installation is needed.
Configure Memcached
Configure 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 the route in the
app/Config/Routes.php
file:php$routes->get('/memcached', 'Home::memcached');
1Open your browser and visit: https://servbay-codeigniter-test.local/memcached
Redis Example
Install Redis Extension
In ServBay, the Redis extension is pre-installed, so no additional installation is needed.
Configure Redis
Configure 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 the route in the
app/Config/Routes.php
file:php$routes->get('/redis', 'Home::redis');
1Open your browser and visit: https://servbay-codeigniter-test.local/redis
Relational Database Examples
Create Database Structure and Migration Files
Create Migration File
Use the CLI tool of CodeIgniter to create a migration file:
bashphp spark make:migration create_users_table
1Edit Migration File
Edit the newly created migration file in the
app/Database/Migrations
directory 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
1
MySQL Example
Configure MySQL
Configure 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' => 'utf8', 'DBCollat' => 'utf8_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
19Add Routes
Add the following routes in the
app/Config/Routes.php
file: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 your browser and visit:
https://servbay-codeigniter-test.local/mysql-add
andhttps://servbay-codeigniter-test.local/mysql
PostgreSQL Example
Configure PostgreSQL
Configure 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 Database Migration File
Generate the database migration file using the following command:
bashphp spark make:migration create_users_table
1Edit Database 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 Database Migration
Execute the following command to run the database migration and create the table structure:
bashphp spark migrate
1Add Routes
Add the following routes in the
app/Config/Routes.php
file: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 your browser and visit:
https://servbay-codeigniter-test.local/pgsql-add
andhttps://servbay-codeigniter-test.local/pgsql
By following the above steps, you successfully created and ran a CodeIgniter project, managed and accessed your project using ServBay's features, and connected to multiple databases and retrieved data. We hope this article helps you quickly get started with CodeIgniter and apply it to your projects.