Phalcon MVC Framework

My first experience With the PHP framework named Phalcon

During this semester, I was given the assignment of creating a PHP CMS application through which customers could make appointments. I already had some experience using PHP so for me using standard PHP wasn’t really a challenge. That’s why I chose A MVC framework so I could improve my coding skills to become a better programmer.

For this Project I chose Phalcon, because it’s a great framework that works really fast. Is a full-stack PHP framework delivered as a C-extension. The code is compiled and isn’t interpreted because is already compiled to a specific platform and processor.

So in this article I’m going to explain how what my experience was with using Phalcon.

So first of all I must admit installing and configuring the Phalcon extension requires a great deal of work. First you’ve to download all the files needed to run phalcon and after that you need to add the extension to the php.ini. And you also need to install the downloaded files. especially on Linux this is troublesome, because you’ve to do it all through the terminal.

Documentation in Phalcon

The documentation in Phalcon is great. They’ve a lot of information on how to use Phalcon. Everything is well explained so that everyone that is familiar with programming can easily apply his knowledge to create a create web app that is both secure and fast. SO for example if you would like to know to code a user login system Phalcon will have a detailed on how to accomplish this.

The only downside is that the community of Phalcon is not that big because it’s relative new framework. But you can find some communities here and there. For example Slack has a great Phalcon community where users will respond to your question pretty fast.

coding in Phalcon MVC

After installing it’s very easy to use. With Phalcon devtools you can easily create the folder structure needed for your project as you can see in the example below.

Phalcon’s MVC structure creates a very organized code. instead of writing complex SQL queries you can just easily access tables through the model. This can easily be done by naming the model after the table in your database. Because you made a connection with the database through the config file Phalcon already knows that de model user relates to a table user within the database. This avoids a lot of problems when you try to access data from a database.

Using controllers in Phalcon is not that hard. In Phalcon you create a function. Every function ends with “Action”. every action can refer to a page in the view. For example if you have a controller named UserCont0roller and an action names addAction you should create a folder named user in lowercase letters and a file named add.volt that corresponds to the action name.

Data from the models can easily be accessed by calling the model from a controller like this “$user =User::find();”. With this simple line of code you can easily extract any data from the table.

Displaying data in the view is also very easy because the controller and the view are tightly connected in Phalcon. All you have to do is make the variable accessible for the view by using “$this->view->setVar(‘User’, $User)”;

Phalcon uses a HTML template engine called .Volt. You’re not obligated to use this but it’s very useful for writing clean and elegant code. For example if we want to display all the users from the user table we can easily display this with the following code:

<ul>
{% for u in user %}
<ul>{{ u.name | escape_attr}}</ul>
<ul>{{ u.lastname | escape_attr}}</ul>
<ul>{{ u.age | escape_attr}}</ul>
{% endfor %}
</ul>

So as you can see Phalcon is perfect for creating rapid prototypes to give the customer on idea how the application will work and what features will be available on the website.

The most important reason to use Phalcon is because of its Dependency injection or DI. With this we can easily use functions of Phalcon initializing them in the config file. For example if we wanted to use Sessions within are application we could do this as follows:

$di->setShared('session', function () {
$session = new \Phalcon\Session\Adapter\Files();
$session->start();
return $session;
});

This is very useful because now I don’t have to call this function in every file because the index.php already takes care of that.

I had a great time learning Phalcon it thought me to write cleaner code which is important to all of us. I hope I gave you’ll some new insights on coding so you can apply this knowledge to your own projects.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.