Getting Started with Phalcon

By Stephan A. Miller
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies

About this book

Phalcon is an open source PHP framework which makes developing with PHP easier and more efficient by combining the speed and performance of C with the unique development features of the MVC architecture. Phalcon is installed as a PHP module so you don’t have to download an archive like you do with other frameworks, and building projects is made easier with its controllers and models. The Phalcon Query Language (PHQL) makes this tool even more expressive and clean. Its reputation as the most downloaded PHP tool is definitely justified by its rich offerings. This tutorial provides you with the knowledge you need to build your very own web application with the revolutionary Phalcon framework. This comprehensive guide will start by describing the installation of Phalcon PHP. You will then learn how to build projects and how to further utilize the Phalcon Developer Tools to build models, views, and controllers with the central example of a blog application. Features like PHQL are also explained and implemented effectively throughout the book. The genius in you will be revered by the stunning web application that you will be able to create by reading this book. This handy guide gives you a detailed introduction to using the remarkable Phalcon framework to develop web applications. You will begin by learning how to install the Phalcon module and how to build your own projects. The blog application is the central example throughout the book, and by using Phalcon Developer Tools and web tools, you will create and optimize the basic skeleton for your application with ease and efficiency. You will learn how to add rich features to your blog using Phalcon Views, Models, and Controllers. You will also gain expertise in Phalcon functionalities like the Volt template engine, view helpers, PHQL, and so on. This is an invaluable tutorial for enthusiasts and developers alike who wish to use the fascinating Phalcon PHP framework to rapidly design and develop impressive web applications.

Publication date:
January 2014
Publisher
Packt
Pages
138
ISBN
9781783287673

 

Chapter 1. Installing Phalcon

This chapter will walk you through installing Phalcon on your chosen platform. The installation process differs a little bit depending on which operating system you happen to be using. Since Phalcon is a PHP extension written in C, installation is a little bit more involved than downloading an archive of PHP files and uploading them to your server.

But installation should still take you less than an hour. Depending on your system and experience with PHP extensions, it could take only minutes. At the end of this chapter, you will have a high performance PHP framework that will simplify the process of developing your application and help you get your project up and running faster.

In this chapter, you will learn how to:

  • Meet Phalcon's system requirements

  • Find the correct Phalcon DLL for use on a Windows machine

  • Install the Phalcon DLL on a Windows machine

  • Install Phalcon on Linux

  • Install Phalcon on Mac

  • Install Phalcon on FreeBSD

  • Edit your php.ini to use Phalcon

  • Configure Apache for Phalcon

 

Phalcon system requirements


You will of course need a web server in order to use Phalcon. Apache is the most common, but if you have an Nginx or Cherokee web server, that will work too. You can even use PHP's built-in web server as your Phalcon development server if you wish. For the purposes of this book, we will assume you have Apache installed.

Along with that, you will need PHP installed. While Phalcon will work with any version of PHP higher than Version 5.3.1, it is recommended that you use PHP 5.3.11 or greater. Versions of PHP before 5.3.11 still have security flaws and memory leaks that were fixed in the later versions.

But that is about all you need. Phalcon is not really very picky. There are a few other requirements you may need to meet on each specific operating system, and we will cover them in the individual installation processes.

 

Installing Phalcon on Windows


In this section, we will walk through installing Phalcon on Windows. Even if you aren't installing the extension on Windows, please read through this section. Many of the steps involved, such as locating your php.ini, apply to all operating systems.

The Phalcon extension for Windows comes compiled as a DLL. However, you must locate the correct DLL for your version of PHP. The first requirement when installing Phalcon in Windows is a PHP binary compiled with Windows Visual Studio 9.0. It is possible to have a Windows PHP installation compiled with another version of Visual Studio. If you have one of these other versions, you will have to install the correct version of PHP if you want Phalcon to work on your system.

So, let's take a look at your PHP installation to discover just which version of PHP you have installed. First, you need to locate the document root of your web server. Or, in other words, you need to locate the folder on your computer that corresponds to the page you get when you browse to http://localhost or the folder on your server that maps to the home page of http://yourdomain.com. We are going to create a PHP file that will tell us everything we need to know to pick the right version of the PhalconPHP DLL and drop it in that folder.

So, open up your text editor or IDE and create a new file. Name it info.php. In that file, insert the following code:

<?php
  phpinfo();
?>

Save the file to your document root. Where your document root is depends upon which web server stack you have installed on your machine. It could be Apache directly from the official site, such as XAMPP, Wamp, or AMPPS, or one of the other versions of Apache, PHP, and MySQL stacks available. Your best bet is to visit the site where you downloaded your chosen version of Apache and read the documentation.

Once you have saved the file, browse to http://localhost/inf o.php and you should see a page like the following screenshot:

You will notice the PHP version at the top of the page. We will be paying attention to the first two numbers separated by a dot. They will either be 5.3 as shown in the previous screenshot or 5.4. If they are 5.2 or less, you will need to update your PHP installation.

Next, you want to make sure that your version of PHP was compiled with Microsoft Visual Studio 9.0. So, search for the word Compiler in your browser window. The command to open the search box on most browsers is Ctrl + F. It is usually near the top of the page as shown in the following screenshot. If your PHP was compiled with Visual Studio 9.0, you should see MSVC9 (Visual C++ 2008), as shown in the following screenshot:

Below the Compiler, you will see the Architecture listing. It will say x86 or x64. In the previous screenshot, PHP was compiled for the x86 architecture.

Next, we need to see if you have a thread-safe version of PHP or not. So, search for PHP Extension Build in your browser window. Next to this, you will see the build version of your PHP installation. If you see an NTS at the end of this number, then you do not have a thread-safe version of PHP.

Now that we have these pieces of information, we can go to http://phalconphp.com/en/download/windows.

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you. You can also find all the code used in the book by visting GitHub page http://eristoddle.github.io/phalconBlog/.

Here, we will see the page as shown in the following screenshot, which lists multiple versions of the Phalcon PHP extension:

It may look daunting, but we just did the research that will enable us to choose the correct version. We know whether we have a thread-safe version or not. We know whether we have an x86 or x64 architecture. And we know whether we have PHP 5.3 or 5.4. Don't worry about the last number in the PHP version. We just need to download the extension that fits our configuration. The Phalcon extension for PHP 5.3.9 will work for all versions of PHP 5.3 and the extension for PHP 5.4.0 will work for all versions of PHP 5.4.

After downloading the extension, unzip it. Inside the zip file will be a DLL. Next, we need to move this DLL into the folder where your other PHP extensions exist. If you have XAMPP installed, this folder will most likely be c:\xampp\php\ext, and with Wamp, that would be c:\wamp\bin\php_version_number\ext, where php_version_number is replaced with the version number of PHP you have installed. For other versions of web server stacks, you will have to visit the documentation to find the PHP extension folder.

Now we have the extension where it needs to be, but we aren't done yet. We need to have PHP load the extension when it runs. So we have to locate the php.ini file and edit it. But guess what, we can use our info.php file to tell us where this file is. So load it again in your browser and search for Loaded Configuration File. Listed beside this entry is its location, as shown in the following screenshot:

Once you locate this file, open it with your text editor or IDE and scroll to the very bottom. This file can be quite long. At the very bottom of this file, you will want to add the following line to tell PHP to load Phalcon:

extension=php_phalcon.dll

Save the file. Some server stacks like XAMPP have desktop software to stop and start Apache, while others like Apache from the official site only have a tray icon to control stopping and starting of the server. Or, you can just restart Apache from the command line. Restart Apache now and Phalcon is installed. But let's check to make sure. Load http://localh ost/info.php in your browser again and search for Phalcon and you should be greeted by a page as shown in the following screenshot:

 

Installing Phalcon on Linux


Linux is the most common operating system for a web server. Phalcon does require you to have root access to your web server. If you are on a shared server, you won't get root access, but you may be able to talk to your hosting provider to install Phalcon for you. If you have a VPS, cloud server, or dedicated server, you most likely have root access. If not, you can ask your hosting provider to give you access.

It is beyond the scope of this book to walk you through installing Phalcon on each and every Linux installation, but there are prebuilt versions of the Phalcon extension for some versions of Linux.

For Debian Linux, you will find a repo at the following link:

http://debrepo.frbit.com/

For Arch Linux, you can go to the following link to download a PKGBUILD:

http://aur.archlinux.org/packages.php?ID=61950

And for OpenSUSE, you can find a package at the following link:

http://software.opensuse.org/package/php5-phalcon

For all other versions of Linux, we will have to compile Phalcon ourselves. This is not hard, and if you have been using Linux for a while, you are probably used to compiling your own software by now. First, we have to be sure we have the following software installed:

  • Git

  • GCC Compiler

  • PHP development resources

If you don't have these installed, you can install them via the command line.

For Ubuntu, the command line is as follows:

sudo apt-get install git
sudo apt-get php5-devphp-mysqlgcc

For Fedora, CentOS, and RHEL, the command line is as follows:

sudo yum install git
sudo yum install php-develphp-mysqlnd ccc libtool

For OpenSUSE, the command line is as follows:

yast2 -igit
yast2 -iphp5-pear php5-develphp5-mysqlgcc

For Solaris, the command line is as follows:

pkginstall git
pkg install gcc-45 php-53 apache-php53

Once you have met all the requirements, open up a terminal window and type the following commands to download the Git repository and compile the Phalcon extension:

git clone git://github.com/phalcon/phalcon.git
cd cphalcon/build
sudo ./install

On some Linux installations, the compilation may fail because libpcre3-dev is missing. You can fix this issue by installing this package with your Linux package manager.

You just compiled your first PHP extension. Now we need to add the reference to our extension to the php.ini file. We can find it by creating the info.php file as discussed in the Installing Phalcon on Windows section of this chapter, loading it in our browser, and searching the resulting page for Loaded Configuration File. Locate the file listed here in your file-system and add the following line to the very bottom of the file:

extension=phalcon.so

Now save the file. If you do happen to be installing Phalcon for PHP 5.4 on a Debian type Linux, the procedure for installing a new PHP extension has changed. It is no longer a best practice to edit the main php.ini file. In the same folder where you found the php.ini file, usually with the /etc/apache5/ path, you will find a mods-available folder. It is in this folder that you will be creating a custom .ini file just for Phalcon, and you will call it phalcon.ini. This file will be empty because we aren't adding any custom configuration settings to Phalcon. Then, to enable the extension, we just run the following in the command line:

php5enmod phalcon

Now restart your web server. The command to restart Apache differs depending on the type of Linux. For Debian and Ubuntu type Linuxes, you can type the following command:

sudo service apache2 restart
And for Red Hat, CentOS or Fedora, you would type:
sudo service httpd restart
 

Installing Phalcon on Mac


To install Phalcon on Mac, the steps are basically the same as installing it on Linux. You must compile the extension from the source code. But first there are some requirements. You need to have Git, php5-devel, and Xcode with the command line tools installed. Xcode is available for free from the official Mac site, but is not installed by default.

Also, Xcode's command line tools are not installed with Xcode by default. In order to make sure to install these tools, open Xcode's Preference panel, choose the Download tab, and click on the Install button located next to the Command Line Tools listing.

To install Git, just point your web browser to http://git-scm.com/download/mac, download the file, and install it. You will also need to install php5-devel if you don't already have it on your system. How you install this depends on the package manager you have installed on your Mac. There are also some prebuilt php5-devel binaries available for the Mac. A search on Google for "install Mac php5-devel" should point you in the right direction.

Now that we have met all the requirements, the steps are the same as installing Phalcon on Linux. First, use Git to pull the source code down to your computer and build the extension by issuing the following commands in the Terminal:

git clone git://github.com/phalcon/phalcon.git
cd cphalcon/build
sudo ./install

Then, use the steps we went over in the Installing Phalcon on Windows section of this chapter to locate your php.ini file. Once you have found your php.ini file, open it and add the following line to the very bottom of your file:

extension=phalcon.so

Next, restart Apache and you're set.

 

Installing Phalcon on FreeBSD


If you happen to be installing Phalcon on FreeBSD, you've got it easy. There is a port of Phalcon available. It can be installed with the following command:

pkg_add -r phalcon

Or

export CFLAGS="-O2 -fno-delete-null-pointer-checks"cd/usr/ports/www/phalcon&& make install clean
 

Optional Phalcon dependencies


By now, you should have a working installation of Phalcon. But to use some of Phalcon's functionality, you may need to install one, some, or all of the following PHP extensions:

  • PDO

  • PDO/MySQL

  • PDO/PostgreSQL

  • PDO/SQLite

  • PDO/Oracle

  • mbstring

  • Mcrypt

  • openSSL

  • Mongo

You will only need these if you plan to use the functionality they provide. For the purposes of this book, you will need to make sure that you have the PDO, PDO/MySQL, Mcrypt, and mbstring extensions installed.

 

Finding help


Phalcon is constantly being improved, but every now and then, you may run into issues where the standard installation process may not work or you may run into other bugs. It is beyond the scope of this book to cover everything that could happen while installing or using Phalcon. To find help with issues like this, I suggest three places to look:

 

Summary


In this chapter, we learned how to choose the correct version of the Phalcon PHP extension for our operating system. We also learned how to install the extension on a multitude of systems. We edited our php.ini file so that Phalcon will now load with our PHP installation and become an integral part of the language.

In the next chapter, we will begin developing our Phalcon blog by putting together the skeleton structure of our site.

About the Author

  • Stephan A. Miller

    Stephan A. Miller is an SEO specialist, writer, and software developer from Kansas City, MO. He has expertise in operating systems, a few databases, and a handful of programming languages. He is currently working as a software contractor. He is also the author of Piwik Web Analytics Essentials published by Packt Publishing . He also blogs when he has the time at http://stephanmiller.com.

    Browse publications by this author