Create and Run a FuelPHP Project
What is FuelPHP?
FuelPHP is a flexible and modular PHP framework designed for developing modern web applications. It follows the HMVC (Hierarchical Model-View-Controller) design pattern and provides a rich set of 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 choice for many developers.
Key Features and Advantages of FuelPHP
- HMVC Architecture: Supports hierarchical MVC design patterns, facilitating code reuse and modular development.
- High Performance: FuelPHP is known for its efficient performance and speed, capable of handling high-concurrency requests.
- Easy to Extend: Offers a rich extension mechanism that allows developers to easily add custom functionalities.
- Security: Includes multiple built-in security features like input validation, output filtering, and CSRF protection.
- Strong Community Support: Has an active developer community and a wealth of third-party extensions.
FuelPHP can help developers quickly build high-performance, high-quality web applications suitable for projects of various sizes.
Creating and Running a FuelPHP Project with ServBay
In this article, we will use the PHP environment provided by ServBay to create and run a FuelPHP project. We will set up a web server using ServBay's "Host" feature and achieve project access through simple configuration.
Note: For users who have previously used NGINX or Apache
ServBay defaults to using Caddy as the web server. For users migrating from NGINX and Apache to ServBay, there are some key changes to note:
Caddy Configuration
ServBay has Caddy integrated, and the default configuration is optimized and debugged. Developers only need to manage sites via 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 for URL rewriting and other configurations. However, ServBay comes pre-configured with Caddy rules, so developers do not need to write these rules unless there are special requirements.
Learn More
For more information, please refer to Rewrite and htaccess, How to Migrate Apache Websites to ServBay, and How to Migrate NGINX Websites to ServBay.
Creating a FuelPHP Project
TIP
ServBay recommends placing websites in the /Applications/ServBay/www
directory for easier management.
Install Composer
Composer is pre-installed with ServBay, so no separate installation is required.
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
4Navigate to the Project Directory
Enter the newly created FuelPHP project directory:
bashcd /Applications/ServBay/www/servbay-fuelphp-app
1
Initial Configuration
Configure Database Connection
Configure 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
Configuring the Web Server
Use ServBay's "Host" feature to access the FuelPHP project via a web server. In ServBay's "Host" settings, add a new host:
- Name:
My First FuelPHP Dev Site
- Domain:
servbay-fuelphp-test.local
- Website Type:
PHP
- PHP Version: select
8.3
- Website Root Directory:
/Applications/ServBay/www/servbay-fuelphp-app/public
For detailed setup steps, please refer to Adding the First Website.
Adding Sample Code
Add the following code in the fuel/app/classes/controller/welcome.php
file:
use Fuel\Core\Controller;
use Fuel\Core\Response;
use Fuel\Core\Cache;
use Fuel\Core\DB;
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()
{
Cache::set('key', 'Hello Redis!', 60);
$value = Cache::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
43
44
45
Access the Website
Open your browser and visit the following URL:
https://servbay-fuelphp-test.local
: You will see the page outputHello ServBay!
.
NoSQL Database Examples
Memcached Example
Install Memcached Extension
Memcached extension is pre-installed with ServBay, no additional installation is required.
Configure Memcached
Configure Memcached connection information in the
fuel/core/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
13
14Add Routes
Add the following routes to
fuel/app/config/routes.php
:phpreturn array( 'memcached' => 'welcome/memcached', )
1
2
3Using 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 a browser and visit https://servbay-fuelphp-test.local/memcached
Redis Example
Install Redis Extension
Redis extension is pre-installed with 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 routes to
fuel/app/config/routes.php
:phpreturn array( 'redis' => 'welcome/redis', )
1
2
3Using 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 a browser and visit https://servbay-fuelphp-test.local/redis
Relational Database Examples
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
9Create Migration Files
Use FuelPHP's oil tool to create migration files:
bashphp oil g migration create_users_table
1Edit Migration Files
Find the newly created migration file in the
fuel/app/migrations
directory and edit it 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 Migration
Use FuelPHP's oil tool to run the migration and create the database table:
bashphp oil refine migrate
1Add Routes
Add the following routes to
fuel/app/config/routes.php
:phpreturn array( 'mysql_add' => 'welcome/mysql_add', 'mysql' => 'welcome/mysql', )
1
2
3
4Inserting User Data
Insert 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
9Using 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
6Open a browser and visit
https://servbay-fuelphp-test.local/mysql_add
andhttps://servbay-fuelphp-test.local/mysql
Through the above steps, you have successfully created and run a FuelPHP project using ServBay's features to manage and access your project while connecting to and querying multiple databases. Hopefully, this article will help you quickly get started with FuelPHP and apply it to your projects.