Before proceeding to install, I suggest you look for the latest options at https://phalconphp.com
The first step in creating a Phalcon app is to actually have it installed on your machine. Phalcon does not come with the default PHP installation, and as I am writing this recipe, none of the major hosting companies that I know of have as a default.
I have personally installed it on three different versions of Mac OS, installed it for MAMP and installed it on a variety of different Unix platforms within AWS/EC2 instances (Linux/RHEL and Ubuntu). I found that it was fairly straightforward, but you should follow the installation steps to the letter.
Be advised, this installation information is mostly derived directly from Phalcons site at https://phalconphp.com .
Install in Linux/Unix/Mac
Ubuntu 14.04+
To install Phalcon on Ubuntu you need to follow these steps:
sudo apt-add-repository ppa:phalcon/stable
sudo apt-get update
sudo apt-get install php5-phalcon
You will need these dependencies to compile Phalcon into a PHP binary (phalcon.so):
sudo apt-get install php5-dev php5-mysql gcc libpcre3-dev
If you are missing apt-add-repository run the following command:
sudo apt-get install software-properties-common
Important - See compile notes below
Fedora
sudo yum install php-devel php-mysqlnd gcc libtool
Important - See compile notes below
RHEL
sudo yum install php-devel php-mysql gcc libtool
Important - See compile notes below
Suse
yast2 -i php5-pear php5-devel php5-mysql gcc
Important - See compile notes below
Mac OS X
MAMP
I found it rather easy to get Phalcon up and running for MAMP following the instructions at: https://github.com/majksner/php-phalcon-mamp .
Using Homebrew
- brew tap homebrew/dupes
- brew tap homebrew/versions
- brew tap homebrew/php
- brew install php5x php5x-phalcon # Where "x" - minor number of PHP
Compiling for your Linux platform
You will need to compile Phalcon to generate a binary extension (phalcon.so) for your PHP installation. Issue these commands from your terminal (or command line) to generate the binary extension for your platform:
git clone git://github.com/phalcon/cphalcon.git
cd cphalcon/build
sudo ./install
Add the extension to your php.ini :
extension=phalcon.so
Finally, restart the webserver.
Install for cPanel
The cPanel developers have released a custom module that allows for installation of Phalcon in cPanel hosted sites. Information can be found here:
https://github.com/thecpaneladmin/EA-PhalconPHP
Although Phalcon does not binary link to other extensions, it does use some of them to offer functionality. The extensions used are:
- mbstring
- mcrypt
- openssl
- PDO
- PDO/Mysql
- PDO/Postgresql
- PDO/Sqlite
- PDO/Oracle
- Mongo
It is not necessary for all the above extensions to be present in the system that has Phalcon installed. You can only install the ones that meet your needs. For instance if you use a MySQL database, then you can only load PDO and PDO/MySQL ignoring the Oracle, SQlite, Postgresql and Mongo.
Verify your installation
Create a small PHP script to display PHPinfo and execute it in your browser:
Verify that you see a section for Phalcon in the list of settings. For my installation, I see this:
CONGRATS - You are done with Sprint 1
Sprint 2 - A Hello World app
Our first Sprint will get you up and running with the famous Hello World app. The main goals of this Sprint are to install essential folder structure and get a working response from the app by going to the apps home page.
Essential Folders and Files
Lets get our Sprint kicked off by installing the core folder structure. We will also add a few empty files just so I can talk about why they are needed before you actually start writing code. This is the fastest way to get into the underlying thinking of the Phalcon framework.
Create a project folder
Open a terminal window and go into your development directory. We will make a new folder where we want to store the app. Name it whatever you want, but if you are doing this for the first time, you may want to use my naming convention so you do not get lost.
I like to put all of my projects in a folder called Development off my main user folder. You should put your projects wherever works best for you, and it will be obvious where you can replace your folder names with mine.
cd Development
mkdir phalcon-starter
cd phalcon-starter
Remember, the Development folder is my personal location. Please use the one that works best for you.
Create your essential folders
The convention in Phalcon is to store all of your application files (controllers, models, views, configs, etc) in a folder called app and all of your publicly accessible files such as CSS, JS and images in a folder called public .
cd phalcon-starter
mkdir app
mkdir public
Create your APPs MVC folders
Phalcon is based on the Model-View-Controller (MVC) pattern for separating the core aspects of an application. Lets create the following folders under your app folder:
cd phalcon-starter/app
mkdir controllers
mkdir models
mkdir views
Controllers
Controllers hold the application logic for your app. Controllers are where you put actions that handle user interaction and determine the appropriate UI to respond back to the user. Controllers can also be used to check for member credentials, post form data, format JSON responses, handle errors and so forth.