FuelPHP – Simple Web Application

FuelPHP - Simple Web Application

In this FuelPHP – Simple Web Application chapter, we will see how to create a simple web 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

FuelPHP - Simple Web Application

Next Topic : Click Here

Leave a Reply