Some web developers may not be familiar with Phalcon PHP framework which is written in Zephir, a language mid-way between C and PHP that helps in developing PHP extensions without having to know C. Since Zephir compiles into C, which is what PHP extensions are made of, it’s lets PHP developers create highly optimized modules for their programming language environment without the overhead of learning a completely new language, because Zephir compiles into C, which is what PHP extensions are made of.
Phalcon is incredibly fast as it was an extension written in pure C. Moreover, Phalcon is also good in introducing massive overhead in fixing bugs or inspecting what’s going on under the hood when there are errors are detected. It also took ages to add new features, since developing in C is so much more difficult. Phalcon was rewritten in Zephir in version 2, but Zephir could not compile code to PHP 7 compatible extensions and was left behind in PHP 5 land as PHP 7 was looming on the horizon and announced a dramatic departure from the extension API of PHP 5+.
However, the cost of installing Phalcon was no longer worth it in any way since with our Homestead Improved box up and running and powering all our tutorials, and with PHP 7’s performance advances making up for any kind of speed gains Phalcon may have offered in PHP 5 time, the cost of installing Phalcon was no longer worth it in any way.
The biggest update by far is the fact that Zephir now supports PHP 7, and allows for not only the compilation of Phalcon, but also the compilation of any other Zephir code into PHP 7 as well. You can gain more benefits form compiling into PHP 7 in a jiffy, such as having unparalleled advantages in terms of speed and resource conservation.
Setting it Up
After understanding, what is PHP framework on PHP7, now you may be curious to use it. So, let’s learn on how to set up Phalcon 3 on a PHP 7. This instruction will be specific to an Ubuntu 16.04, but you can use any kind of environment that you like.
Installing Phalcon
sudo apt-get install software-properties-common
sudo apt-add-repository ppa:phalcon/stable
sudo apt-get update
sudo apt-get install php7.0-phalcon
sudo phpenmod -v 7.0 -s ALL phalcon
sudo service php7.0-fpm restart
The second to last command is a shortcut command to enable a PHP extension. The –v flag tells it which PHP version to activate it for (we want only 7.0 here) and which SAPI (command line, FPM, or both – we want both).
You can just copy the ini file into the extensions folder like so:
sudocp
/etc/php/7.0/mods-available/phalcon.ini /etc/php/7.0/fpm/conf.d/20-phalcon.ini
sudocp
/etc/php/7.0/mods-available/phalcon.ini /etc/php/7.0/cli/conf.d/20-phalcon.ini
The 20 prefix indicates priority, in case some extensions need to be loaded before others. This is not the case here.
We’ve now unleased the beast. Looking at phpinfo(), it’s there.
Demo Phalcon App
We can test things on a demo Phalcon application – the finished Invo app.
Configuring Nginx for Phalcon
We need to add our app into the Sites block of Homestead
.yaml
:
- map: phalcon-tut.app
to: /home/vagrant/Code/phalcon-tut/public
Make sure phalcon-tut.app or what ever vhost name you choose is added to your host OS /etc/hosts file in order to resolve to the VM’s IP address.
Of coure, in order to apply this Nginx setup, we also need to run vagrant provision outside the VM. Finally, we edit Nginx’s location block, and change from.
location / {
try_files $uri $uri/ /index.php?$query_string;
}
To
location / {
try_files $uri $uri/ /index.php?_url=$uri&$args;
}
To apply the changes, we need to restart Nginx:
sudo service nginx restart
Bootstrapping the app
cd /home/vagrant/Code
git clone https://github.com/phalcon/invo phalcon-tut
We need to initialize its database, as per instructions, once the app has been cloned. Due to make it a bit more modern, we use utf8mb4
instead of utf8
.
echo'CREATE DATABASE invo CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'
|
mysql -u homestead -psecret
catschemas/invo.sql
|mysql -u homestead -psecret invo
There’s one more step which isn’t described in the app’s repo – changing the baseUri in the configuration. You need to go into app/config/config.ini and change baseUri to /, otherwise assets (JS/CSS) will fail to load.