FuelPHP is a community-driven open source PHP framework based on the OOP hierarchical Model-View-Controller (MVC) architecture. It features a great command line tool called Oil that allows you to speed up the development of your project. You can use it to generate code, debug or perform database migrations.
In this tutorial we will get started with a small FuelPHP application. To illustrate this, I will use an Ubuntu powered VPS with the LAMP stack installed. If you want more information about how to set this up, please consult this great tutorial. FuelPHP requires though a web server installed (has been tested with Apache, Nginx and IIS) and PHP 5.3+.
The first thing we need to do is get the quick installer onto our VPS using curl. Run the following command:
curl get.fuelphp.com/oil | sh
This will download the oil package and place it into the /usr/bin folder on your cloud server.
Quickly install Git as oil will need it to fetch the application code:
sudo apt-get update sudo apt-get install git-core
Next, navigate to the web server document root (/var/www for Apache) and run the following command for creating your FuelPHP application:
oil create fuel
This will fetch the necessary files using Git and dependent libraries using Composer and put everything you need into a folder called fuel. If you now navigate in the browser to your-ip-address/fuel/public you should see the front page of your application.
If you want, you can change the web server's document root to point directly to the public folder of your FuelPHP application so that you can access it straight from your IP address (or domain name if you have one pointing at it). To do this, edit the following virtual host file:
And change this line from this:
Then restart Apache for the changes to take effect:
sudo service apache2 restart
If you want FuelPHP to have clean URLs (to remove the ugly index.php segment from its URLs) you should make sure its .htaccess files can override default Apache instructions. For this, mod_rewrite needs to be enabled.
To check if it is available, use the following command:
If you see "rewrite_module" in the list, you are fine. If not, use the following command to enable the module:
Next, edit the Apache default virtual host file (again) and make sure that Allow Overrides is set to All under the /var/www/ directory. This includes also the fuel application folders. Edit the file with the following command:
And where you see this block, make the changes to correspond to the following:
<Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory>
Before we restart the server, let's quickly also set a default timezone in the php.ini file:
And in this file where you see the following line:
Remove the preceding semi-colon and set a PHP type timezone in the following format:
date.timezone = America/Toronto
Save the file and then restart Apache for the all the changes to take effect:
sudo service apache2 restart
Next up, let's create an empty database to use with our application. Assuming you are using MySQL or MariaDB, log into the appropriate terminal and run the following command:
create database fuel;
This will create an empty database called fuel. Exit the MySQL terminal and return to your application as we now need to edit its configuration file and enable some things. From the root of the FuelPHP application, edit the config file:
In there, uncomment the always_load array, the packages array inside and the orm package. This will make sure FuelPHP loads the ORM package it comes with to help us create our working models. Save the file and exit. Now it's time to set the database connection. Still inside the app/config folder, inside a folder called development (given that you are by default in the development environment of FuelPHP), edit the db.php file:
In there set the database name and credentials to connect to it, then save and exit.
Next, we'll need something called scaffolding. This is basically a set of controllers, models and views that define our model and contain the default CRUD operations code. Let's say our small application deals with articles for which we need a title and a body. So let's generate the scaffolding with the following command (from within the root application folder - /var/www/fuel):
oil generate scaffold articles title:string body:text
This command generates the scaffolding for our articles model for which we define a title of the type string (varchar(255)) and body of the type text. If you look in your fuel/app/classes folder you should see a new controller, model and views files. One last command we need to run for this model - the database migration command - which will create the table to house our article content:
oil refine migrate
This will use the instructions in the /var/www/fuel/fuel/app/
Now you can navigate in the browser to the articles controller (ip-address/articles if your web server's document root points to the FuelPHP application's public folder) and you can from there perform all the CRUD operations on your article model. You can add, view, update and delete articles immediately.
In this tutorial we've seen how to install FuelPHP from the command line and how to quickly generate a bare bones application. Additionally we've seen how to generate a scaffolding for our articles model that creates automatically the necessary controller and model classes, as well as the views for performing CRUD operations on this model.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.Sign up now
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!