Create and Run a FuelPHP Project
What is FuelPHP?
FuelPHP is a flexible and modular PHP framework suitable for developing modern web applications. It follows the HMVC (Hierarchical Model-View-Controller) design pattern and provides rich features and tools that enable developers to quickly build high-quality web applications. Known for its flexibility, high performance, and ease of extension, FuelPHP is a preferred framework for many developers.
Key Features and Advantages of FuelPHP
- HMVC Architecture: Supports hierarchical MVC design pattern, facilitating code reuse and modular development.
- High Performance: FuelPHP is renowned for its efficient performance and speed, capable of handling high concurrency requests.
- Ease of Extension: Offers rich extension mechanisms, allowing developers to easily add custom functionalities.
- Security: Built-in security features like input validation, output filtering, and CSRF protection.
- Strong Community Support: Active developer community and a wealth of third-party extensions.
FuelPHP helps developers rapidly build high-performance, high-quality web applications suitable for projects of any scale.
Creating and Running a FuelPHP Project using ServBay
In this article, we will use ServBay’s PHP environment to create and run a FuelPHP project. We will utilize ServBay's Host feature to set up the web server and access the project through simple configuration.
Note: For Former NGINX or Apache Users
ServBay uses Caddy by default as the web server. For users transitioning from NGINX or Apache to ServBay, there are some key changes to note:
Caddy Configuration
ServBay already includes Caddy, and the default configuration is optimized and debugged. Developers only need to manage sites through ServBay’s Host feature without manually modifying Caddy configuration files.
Rewrite Rules and .htaccess
In NGINX and Apache, developers typically write their own rewrite rules and .htaccess files to handle URL rewriting and other configurations. However, ServBay’s default configuration already includes Caddy rules, so developers don’t need to write these rules unless there are special requirements.
Learn More
For more information, please refer to Rewrite and htaccess, Migrating Apache Website to ServBay, Migrating NGINX Website to ServBay.
Creating a FuelPHP Project
TIP
ServBay recommends placing your websites in the /Applications/ServBay/www
directory for easy management.
Install Composer
ServBay comes with Composer pre-installed, so you don’t need to install it separately.
Create a FuelPHP Project
Use Composer to create a new FuelPHP project:
bashcd /Applications/ServBay/www mkdir servbay-fuelphp-app cd servbay-fuelphp-app composer create-project fuel/fuel .
1
2
3
4Enter the Project Directory
Enter the newly created FuelPHP project directory:
bashcd /Applications/ServBay/www/servbay-fuelphp-app
1
Initial Configuration
Configure Database Connection
Configure the database connection information in the
fuel/app/config/development/db.php
file:phpreturn [ 'default' => [ 'connection' => [ 'dsn' => 'mysql:host=localhost;dbname=fuel_dev', 'username' => 'root', 'password' => 'root', ], ], ];
1
2
3
4
5
6
7
8
9
Configure Web Server
Use ServBay’s Host feature to access the FuelPHP project through the web server. In ServBay’s Host settings, add a new host:
- Name:
My First FuelPHP Dev Site
- Domain:
servbay-fuelphp-test.local
- Site Type:
PHP
- PHP Version: Select
8.3
- Document Root:
/Applications/ServBay/www/servbay-fuelphp-app/public
For detailed setup steps, please refer to Adding the First Website.
Add Sample Code
Add the following code to the fuel/app/classes/controller/welcome.php
file:
use Fuel\Core\Controller;
use Fuel\Core\Response;
use Fuel\Core\Cache;
class Controller_Welcome extends Controller
{
public function action_index()
{
return Response::forge('Hello ServBay!');
}
public function action_memcached()
{
Cache::set('key', 'Hello Memcached!', 60);
$value = Cache::get('key');
return Response::forge($value);
}
public function action_redis()
{
$redis = \Redis::instance();
$redis->set('key', 'Hello Redis!');
$value = $redis->get('key');
return Response::forge($value);
}
public function action_mysql_add()
{
\DB::insert('users')->set([
'name' => 'ServBay',
'email' => '[email protected]',
])->execute();
return Response::forge('User added');
}
public function action_mysql()
{
$users = \DB::select()->from('users')->execute()->as_array();
return Response::forge(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
Access the Website
Open the browser and visit the following URL:
https://servbay-fuelphp-test.local
: You will see the page outputHello ServBay!
.
NoSQL Database Example
Memcached Example
Install Memcached Extension
Memcached extension is pre-installed in ServBay, no additional installation is required.
Configure Memcached
Configure Memcached connection information in the
fuel/app/config/cache.php
file:phpreturn [ 'driver' => 'memcached', 'memcached' => [ 'cache_id' => 'fuel', 'servers' => [ 'default' => [ 'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100, ], ], ], ];
1
2
3
4
5
6
7
8
9
10
11
12
13Add Routes
Add the following route to the
fuel/app/config/routes.php
file:phpreturn array( 'memcached' => 'welcome/memcached', )
1
2
3Use Memcached
Use cache in the controller:
phppublic function action_memcached() { Cache::set('key', 'Hello Memcached!', 60); $value = Cache::get('key'); return Response::forge($value); }
1
2
3
4
5
6
7Open the browser and visit: https://servbay-fuelphp-test.local/memcached
Redis Example
Install Redis Extension
Redis extension is pre-installed in ServBay, no additional installation is required.
Configure Redis
Configure Redis connection information in the
fuel/app/config/redis.php
file:phpreturn [ 'driver' => 'redis', 'redis' => [ 'database' => 'default', ], ];
1
2
3
4
5
6Add Routes
Add the following route to the
fuel/app/config/routes.php
file:phpreturn array( 'redis' => 'welcome/redis', )
1
2
3Use Redis
Use cache in the controller:
phppublic function action_redis() { Cache::set('key', 'Hello Redis!', 60); $value = Cache::get('key'); return Response::forge($value); }
1
2
3
4
5
6
7Open the browser and visit: https://servbay-fuelphp-test.local/redis
Relational Database Example
Creating Database Schema and Migration Files
Create Migration File
Use FuelPHP’s Oil tool to create a migration file:
bashphp oil g migration create_users_table
1Edit Migration File
Edit the newly created migration file in the
fuel/app/migrations
directory to define the database table structure:php<?php namespace Fuel\Migrations; use Fuel\Core\DBUtil; class Create_users_table { public function up() { DBUtil::create_table('users', [ 'id' => ['type' => 'int', 'constraint' => 11, 'auto_increment' => true], 'name' => ['type' => 'varchar', 'constraint' => 100], 'email' => ['type' => 'varchar', 'constraint' => 100, 'unique' => true], ], ['id']); } public function down() { DBUtil::drop_table('users'); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21Run Migrations
Use FuelPHP’s Oil tool to run the migration and create the database table:
bashphp oil refine migrate
1
MySQL Example
Configure MySQL
Configure MySQL connection information in the
fuel/app/config/development/db.php
file:phpreturn [ 'default' => [ 'connection' => [ 'dsn' => 'mysql:host=localhost;dbname=fuel_dev', 'username' => 'root', 'password' => 'root', ], ], ];
1
2
3
4
5
6
7
8
9Write User Data
Write user data in the controller:
phppublic function action_mysql_add() { DB::insert('users')->set([ 'name' => 'ServBay', 'email' => '[email protected]', ])->execute(); return Response::forge('User added'); }
1
2
3
4
5
6
7
8
9Use MySQL
Call the database in the controller:
phppublic function action_mysql() { $users = DB::select()->from('users')->execute()->as_array(); return Response::forge(json_encode($users)); }
1
2
3
4
5
By following the above steps, you have successfully created and run a FuelPHP project, used ServBay features to manage and access your project, and connected to various databases to fetch data. We hope this article helps you get started with FuelPHP and apply it to your projects.