FuelPHP - Simple Web Application


Advertisements

In this chapter, we will see how to create a simple application in FuelPHP framework. As discussed earlier, you know how to create a new project in Fuel. We can take an example of Employee details.

Let’s start by creating a project named Employee using the following command.

oil create employee

After executing the command, an employee project is created with the following file structure

employee 
├── CHANGELOG.md 
├── composer.json 
├── composer.lock 
├── composer.phar 
├── CONTRIBUTING.md 
├── fuel 
│   ├── app 
│   │   ├── bootstrap.php 
│   │   ├── cache 
│   │   ├── classes 
│   │   ├── config 
│   │   ├── lang 
│   │   ├── logs 
│   │   ├── migrations 
│   │   ├── modules 
│   │   ├── tasks 
│   │   ├── tests 
│   │   ├── themes 
│   │   ├── tmp 
│   │   ├── vendor 
│   │   └── views 
│   ├── core 
│   │   ├── base56.php 
│   │   ├── base.php 
│   │   ├── bootstrap.php
│   │   ├── bootstrap_phpunit.php 
│   │   ├── classes 
│   │   ├── composer.json 
│   │   ├── config 
│   │   ├── CONTRIBUTING.md 
│   │   ├── lang 
│   │   ├── phpunit.xml 
│   │   ├── tasks 
│   │   ├── tests 
│   │   ├── vendor 
│   │   └── views 
│   ├── packages 
│   │   ├── auth 
│   │   ├── email 
│   │   ├── oil 
│   │   ├── orm 
│   │   └── parser 
│   └── vendor 
│       ├── autoload.php 
│       ├── composer 
│       ├── fuelphp 
│       ├── michelf 
│       ├── monolog 
│       ├── phpseclib 
│       └── psr 
├── LICENSE.md 
├── oil 
├── public 
│   ├── assets 
│   │   ├── css 
│   │   ├── fonts 
│   │   ├── img 
│   │   └── js 
│   ├── favicon.ico 
│   ├── index.php 
│   └── web.config 
├── README.md 
└── TESTING.md  
42 directories, 21 files

Structure of the Application

FuelPHP framework provides a well-organized application structure. Let us check some of the important files and folders of the application.

  • fuel − Contains all the PHP files.

  • public − Contains all the assets which are directly accessed through the browser such as JavaScript, CSS, images, etc.

  • oil − An executable used to run command line tasks such as generating code or interactive debugging within your application. It's optional.

  • fuel/app/ − Contains all application-specific PHP files. It contains Models, Views, and Controllers.

  • fuel/core/ − This is where Fuel framework itself lives.

  • fuel/packages/ − Contains all fuel packages. By default, fuel will contain three packages: oil, auth, and orm. These packages will not be loaded unless you require them.

  • fuel/app/config/ − Contains all application-related configuration files. The main application configuration file, config.php file is located here.

  • fuel/app/classes/ − Contains all the application specific MVC based PHP files. It contains controllers, models, helper classes, libraries, etc.

  • fuel/app/classes/controller/ − Controllers are placed here.

  • fuel/app/classes/model/ − Models are placed here.

  • fuel/app/views/ − Contains view files. There are no specific naming conventions for views.

Add a Controller

As discussed earlier, FuelPHP is based on the Model-View-Controller (MVC) development pattern. MVC is a software approach that separates application logic from presentation. In MVC pattern, the controller plays an important role and every webpage in an application needs to be handled by a controller. By default, controllers are located in fuel/app/classes/controller/ folder. You can create your own Controller class here.

Move to the location fuel/app/classes/controller/ and create employee.php file. To create a new controller, just extend the Controller class provided by FuelPHP, defined as follows.

employee.php

<?php 
   class Controller_Employee extends Controller { 
      public function action_home() { 
         
         // functionality of the home page  
         echo "FuelPHP-Employee application!"; 
      } 
   }

Now, we have created an Employee Controller and added a public method, action_home, which prints a simple text.

Routing

Routing resolves the web page URI into specific controller and action. Every webpage in a FuelPHP application should go through routing before the actual execution of the controller. By default, each controller can be resolved using the following URI pattern.

<controller>/<action>

Where,

  • controller is the name of the controller minus namespace, employee

  • action is the name of the method minus action_ keyword, home

The newly created controller can be accessed by http://localhost:8080/employee/home and it will produce the following result.

Result

Employee Application
Advertisements