Installation

To use Phalcon, you need to install the Phalcon extension. This can be done via PECL or by compiling it from source. Below are the steps for both methods:

Installing via PECL

  1. Make sure you have the necessary dependencies installed:
   sudo apt-get install php-dev gcc make
  1. Install Phalcon using PECL:
   sudo pecl install phalcon
  1. Enable the extension in your php.ini file:
   extension=phalcon.so

Installing from Source

  1. Clone the Phalcon repository:
   git clone --depth=1 https://github.com/phalcon/cphalcon.git
  1. Navigate to the directory and compile:
   cd cphalcon/build
   sudo ./install
  1. Enable the extension in your php.ini file as shown above.

Verifying Installation

To verify that Phalcon is installed correctly, run:

php -m | grep phalcon

You should see phalcon in the output.

Creating a Simple Application

Now that Phalcon is installed, let's create a simple application. We will set up a basic directory structure and create a sample controller.

Directory Structure

myapp/
├── public/
│   └── index.php
├── app/
│   ├── config/
│   ├── controllers/
│   └── models/
└── composer.json

Setting Up Composer

Create a composer.json file in the myapp directory:

{
    "require": {
        "phalcon/devtools": "^4.0"
    }
}

Run composer install to install the Phalcon DevTools.

Creating the Front Controller

In the public/index.php file, add the following code:

<?php

use Phalcon\Mvc\Application;

require '../vendor/autoload.php';

$app = new Application();
$app->registerModules([
    'frontend' => [
        'className' => 'MyApp\Modules\Frontend\Module',
        'path'      => '../app/modules/frontend/Module.php',
    ],
]);

$response = $app->handle($_SERVER["REQUEST_URI"]);
$response->send();

Creating a Sample Controller

Create a new controller in app/controllers/IndexController.php:

<?php

use Phalcon\Mvc\Controller;

class IndexController extends Controller
{
    public function indexAction()
    {
        echo "Welcome to Phalcon!";
    }
}

Configuring Routing

To set up routing, create a routes.php file in the app/config directory:

<?php

use Phalcon\Mvc\Router;

$router = new Router();

$router->add(
    '/',
    [
        'controller' => 'index',
        'action'     => 'index',
    ]
);

return $router;

Include the routing in your index.php:

$router = require '../app/config/routes.php';
$app->setDI(new \Phalcon\Di\FactoryDefault());
$app->getDI()->set('router', $router);

Using Phalcon ORM

Phalcon provides a powerful ORM that allows you to interact with your database easily. Below is an example of how to set up and use the ORM.

Database Configuration

In app/config/config.php, add your database configuration:

<?php

return [
    'database' => [
        'adapter'  => 'Mysql',
        'host'     => 'localhost',
        'username' => 'root',
        'password' => '',
        'dbname'   => 'test_db',
    ],
];

Creating a Model

Create a model in app/models/User.php:

<?php

use Phalcon\Mvc\Model;

class User extends Model
{
    public $id;
    public $name;
    public $email;
}

Fetching Data

You can now fetch data from the database using the model:

public function indexAction()
{
    $users = User::find();
    foreach ($users as $user) {
        echo $user->name . "<br>";
    }
}

Summary of Key Features

FeatureDescription
PerformanceBuilt as a C extension, offering high performance.
MVC ArchitectureSupports Model-View-Controller design pattern.
ORMBuilt-in ORM for easy database interaction.
Dependency InjectionIntegrated DI for better management of components.
RoutingSimple and flexible routing system.

Learn more with useful resources