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
Next Topic : Click Here