9.1 C
Pakistan
Sunday, January 26, 2025

Launch and Maintain a FuelPHP Project

FuelPHP: What is it?

A versatile and adaptable PHP framework ideal for creating cutting-edge online applications is FuelPHP. With its wealth of features and tools, it complies with the HMVC (Hierarchical Model-View-Controller) design pattern and enables developers to create professional-grade web applications fast. With its reputation for being highly performant, extensible, and flexible, FuelPHP is a framework that many developers favor.

Essential Elements and Benefits of FuelPHP

  • Code reuse and modular development are made easier by the hierarchical MVC design pattern supported by the HMVC architecture.
  • High Performance: FuelPHP is well known for its quickness and effectiveness, and it can handle requests with a lot of concurrency.
  • Extension Mechanism Richness: Provides a wealth of extension mechanisms that make it simple for developers to include new features.
  • Security: CSRF prevention, output filtering, and input validation are examples of built-in security measures.
  • Rich third-party extensions and an active development community make up the strong community support.
  • FuelPHP facilitates the quick development of high-caliber, high-performance online applications for any kind of project.

ServBay for FuelPHP Project Creation and Operation

In this post, we’ll establish and manage a FuelPHP project using the PHP environment provided by ServBay. Through easy configuration, we will use ServBay’s Host capability to set up the web server and access the project.

Building a FuelPHP Initiative

  1. Install ComposerServBay comes with Composer pre-installed, so you don’t need to install it separately.
  2. Create a FuelPHP ProjectUse Composer to create a new FuelPHP project:
    cd /Applications/ServBay/www
    mkdir servbay-fuelphp-app
    cd servbay-fuelphp-app
    composer create-project fuel/fuel .

3. Enter the Project Directory

Enter the newly created FuelPHP project directory:

cd /Applications/ServBay/www/servbay-fuelphp-app

Initial Configuration​

  1. Configure Database ConnectionConfigure the database connection information in the fuel/app/config/development/db.php file:
return [
    'default' => [
         'connection' => [
 		    'dsn'      => 'mysql:host=localhost;dbname=fuel_dev',
 		    'username' => 'root',
 		    'password' => 'root',
         ],
    ],
];

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

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' => 'demo@servbay.test',  ])->execute();

  return Response::forge('User added');

  }  public function action_mysql()  { 

 $users = \DB::select()->from('users')->execute()->as_array(); 

 return Response::forge(json_encode($users));  } 

}

Go to the Website

Launch the web browser and go to the following address:

At servbay-fuelphp-test.local, the URL The page output will be visible to you. Hi there, ServBay.

Example NoSQL Database

Example of Memcached

1. Put Memcached Extension in Place

There is no need for extra installation because the Memcached extension comes pre-installed in ServBay.

Configure Memcached

Configure Memcached connection information in the fuel/app/config/cache.php file:

return [ 

 'driver' => 'memcached',  'memcached' => [

  'cache_id' => 'fuel',  'servers' => [ 

 'default' => [  'host' => '127.0.0.1',  'port' => 11211,  'weight' => 100, 

 ],

  ], 

 ], ];

Add Routes

Add the following route to the fuel/app/config/routes.php file:

php

return array(
    'memcached' => 'welcome/memcached',
)

Use Memcached

Use cache in the controller:

php

public function action_memcached()
{
    Cache::set('key', 'Hello Memcached!', 60);
    $value = Cache::get('key');

    return Response::forge($value);
}

Redis Example​

  1. Install Redis ExtensionRedis extension is pre-installed in ServBay, no additional installation is required.
  2. Configure RedisConfigure Redis connection information in the fuel/app/config/redis.php file:

    php

    return [
        'driver' => 'redis',
        'redis' => [
            'database' => 'default',
        ],
    ];
    1. Add RoutesAdd the following route to the fuel/app/config/routes.php file:

      php

      return array(
          'redis' => 'welcome/redis',
      )
    2. Use RedisUse cache in the controller:

      php

      public function action_redis()
      {
          Cache::set('key', 'Hello Redis!', 60);
          $value = Cache::get('key');
      
          return Response::forge($value);
      }

      Relational Database Example​

      Creating Database Schema and Migration Files​

      1. Create Migration FileUse FuelPHP’s Oil tool to create a migration file:

        bash

        php oil g migration create_users_table
      2. Edit Migration FileEdit 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. Run MigrationsUse FuelPHP’s Oil tool to run the migration and create the database table:

          bash

          php oil refine migrate

        MySQL Example​

        1. Configure MySQLConfigure MySQL connection information in the fuel/app/config/development/db.php file:
          return [  'default' => [  'connection' => [  'dsn' => 'mysql:host=localhost;dbname=fuel_dev',  'username' => 'root',  'password' => 'root',  ],  ], ];
          1. Write User DataWrite user data in the controller:

            php

            public function action_mysql_add()
            {
                DB::insert('users')->set([
                    'name' => 'ServBay',
                    'email' => 'demo@servbay.test',
                ])->execute();
            
                return Response::forge('User added');
            }
          2. Use MySQLCall the database in the controller:

            php

            public function action_mysql()
            {
                $users = DB::select()->from('users')->execute()->as_array();
                return Response::forge(json_encode($users));
            }

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles