Complete Phalcon Tutorial for Beginners

In this Phalcon tutorial, we’ll introduce you to this framework and give you step-by-step instructions to create your first project. All of ours include the extension falcon by default, so you can try out this awesome framework without much effort!

By the end of this Phalcon tutorial, you will have a functional registration form that will store the collected information in a MySQL database.

What is Falcon?

Phalcon is a high-performance PHP framework based on the model-view-controller (MVC) architecture. Its initial release dates back to 2012 and has always been open source. The essentials are written in the C and Zephir programming languages. Phalcon is also famous for being the first framework to implement object-relational mapping (ORM) in C.

Phalcon is highly customizable due to the presence of Zephir, a high level programming language. But that shouldn’t intimidate you, as Phalcon can be used to develop PHP applications without any knowledge of the C language. Let’s start this Phalcon tutorial by comparing it to other well-known PHP frameworks.

Phalcon – P for Performance

The motivation for developing Phalcon was to create an extension for a web server that would improve execution speeds, reduce resource usage, and ensure higher request processing compared to other PHP frameworks.

Phalcon’s biggest competitive advantage is efficient memory utilization. This eliminates the need to keep any information out of memory; while most other PHP frameworks use file reading and file stats, which are expensive operations. By eliminating the need for these, Phalcon optimizes performance and improves efficiency.

Let’s look at some benchmarking results:

Because Phalcon does not require core files, the memory usage per request is considerably less compared to other PHP frameworks.

We can also see that Phalcon was able to complete more than 1400 requests per second. Meanwhile, other frameworks had a harder time.

Fewer files per request means faster processing and Phalcon does an amazing job on this test.

The Phalcon framework can also complete 1000 requests at lightning speed. While other frameworks take a long time to reach the same amount.

Falcon vs. Laravel

Although Laravel is widely considered the best choice for PHP frameworks, it can’t compete with Phalcon when it comes to speed, scalability, and performance.

However, Laravel is comparatively easier to learn and has better debugging support, while in Phalcon you need knowledge of the C language to debug code. Another difference is that Laravel can be installed on any server, while the Phalcon installation requires root access to the server.

See also  Hacked WordPress: Effective steps to recover your site

Falcon vs. codeigniter

When there were no more PHP frameworks, there was . Although it has been around for a long time, its development has been a bit slow in recent years.

Both Phalcon and CodeIgniter have comparable features, extensive documentation, and a large development community, although Phalcon is considerably faster. However, CodeIgniter can catch up with Phalcon with proper hardware optimization.

Falcon vs. Symphony

Both Symfony and Phalcon are open source projects with multilingual and ORM support. They are extensions of the MVC architecture, they have their own CLIs and detailed documentations. If you plan on building RESTful APIs, both are solid options.

But once again, when it comes to performance benchmarking, Phalcon takes the win. On average, Phalcon can process 50 percent more requests per second compared to Symfony. However, platform-level debugging can be a bit difficult in Phalcon, since you have to review code written in C. Therefore, debugging in Symfony is relatively easier.

PHP Phalcon Framework Configuration

With Phalcon, all you have to do is install a PHP extension, which takes up very little space. However, you will need root access to do so. Fortunately, it is already installed in our shared hosting plans, so the initial configuration will not be difficult. We will also see how to configure Phalcon on a Linux running on Ubuntu 16.04. To make it a bit easier, we’ll use the Phalcon developer tools for both cases.

Without further ado, let’s get down to business!

Option 1 – Phalcon Setup on Shared Hosting

First of all, we will start by installing .

Important! For this task, we will use . for more information on how to install it.

To start, .

Use the pwd command to check your current location. We recommend setting it in a directory above public_html; this will keep things more organized. If the command returns the following results, you are in the right place:

-bash-4.1$ pwd /home/YOUR_USERNAME

Now let’s create a file composer.json with the following content:

{ “require-dev”: { “phalcon/devtools”: “~4.0” } }

You can do this using File Manager or by typing the following command in SSH:

nano composer.json

This will create your file and open a text editor. Paste the necessary contents and press CTRL + X to save everything. To install the Phalcon developer tools, run the following command:

composer install

Wait for the process to finish, as it may take a few minutes. Next, we will create an alias so that the development tools are fully functional. If you followed the exact steps in this tutorial, you can create it with the following syntax:

See also  How to be a web developer: Complete Guide

phalcon alias=/home/YOUR_USERNAME/vendor/phalcon/devtools/phalcon.php

Important! Don’t forget to replace YOUR_USERNAME with your actual username.

We already have functional Phalcon developer tools. To check if they really work, type the following command:

phalcon

And you should receive a result similar to this:

Phalcon DevTools (3.2.12) Available commands: info (alias of: i) commands (alias of: list, enumerate) controller (alias of: create-controller) module (alias of: create-module) model (alias of: create -model) all-models (alias of: create-all-models) project (alias of: create-project) scaffold (alias of: create-scaffold) migration (alias of: create-migration) webtools (alias of: create- webtools) serve (aliases of: server) console (aliases of: shell, psysh)

Since our shared hosting servers already have all the ingredients to run the Phalcon framework, you won’t have to install additional extensions. The Phalcon framework is slightly coupledwhich means you can have any directory structure you see fit.

But for simplicity, we will use the structure that the Phalcon developer tools provide us. Let’s go to the public_html directory using the following command:

cd public_html

When you’re there, use the Phalcon developer tool syntax to configure everything:

phalcon create-project example

Now if you’re going to YourDomain.com/example in the browser, you will see a similar screen:

Option 2: Phalcon setup on a VPS

Now, let’s learn how to configure the PHP Phalcon framework in a . This will take a little more time, as we will have to install more components.

We will guide you through the process so that it is not so complicated. In this part we will be using a VPS running on Ubuntu 16.04.

First of all, we will need these components:

  • Apache
  • mysql
  • PHP5.5+
  • Composer

Apache, MySQL and PHP are included by default in most VPS templates. Therefore, you will only have to continue. When you’re done with that, you’ll need to configure the required Phalcon PHP extension. Run the following command to do so:

curl -s https://packagecloud.io/install/repositories/phalcon/stable/script.deb.sh | sudo bash

Now that we have all the components, let’s configure the Phalcon framework. We’ll start by getting the Phalcon developer tools, which will simplify the process of using the framework. Start by going one directory above the root of the domain. In our example, it would be:

cd /var/www

Use this command to create a file composer.json:

See also  .mx domain | Get to Mexico Using This Domain Today!

nano composer.json

This will create the file and open it in a text editor interface. Paste the following content there:

{ “require-dev”: { “phalcon/devtools”: “~3.2” } }

press CTRL + X to save it. Proceed with the developer tool installation by entering this command:

composer install

When you’re done, create an alias to ensure the Phalcon developer tools are functional. In our example, the syntax should look like this:

aliases phalcon=/var/www/vendor/phalcon/devtools/phalcon.php

Let’s see if they work by running this command:

phalcon

The result should be:

Phalcon DevTools (3.2.12) Available commands: info (alias of: i) commands (alias of: list, enumerate) controller (alias of: create-controller) module (alias of: create-module) model (alias of: create -model) all-models (alias of: create-all-models) project (alias of: create-project) scaffold (alias of: create-scaffold) migration (alias of: create-migration) webtools (alias of: create- webtools) serve (aliases of: server) console (aliases of: shell, psysh)

Now we are ready to create a project. This framework is slightly coupled, which allows you to have any directory structure. In this example, we are going to go with the default directory structure provided by the developer tools. Start by navigating to the root directory of your domain. In our example, it is:

cd /var/www/-tutorials.com/public_html

Next, use this command to create the directory structure and get started:

phalcon create-project example

Now you can open your domain in the browser (for example, YourDomain.com/example) and you should see something like this:

Creating your first project with the PHP Phalcon framework

Now, let’s try to create a basic application. Imagine that you are creating a project and you want to gather a list of possible users or clients before the launch. We can do this by creating a registration form that stores information in a MySQL database.

Understanding the Phalcon Directory Structure

As noted in our Phalcon tutorial, this framework is slightly coupled. This means that you can create any directory structure. However, for this demo, we will use the default structure created using the Phalcon developer tools. Let’s see how it works!

The main directory of the application is known as the folder root. Any folder outside of the root directory is inaccessible to the web server and is not…

Loading Facebook Comments ...
Loading Disqus Comments ...