Create and Run a Phalcon Project
What is Phalcon?
Phalcon is a high-performance PHP framework implemented as a C extension, delivering exceptional execution speed. It follows the MVC (Model-View-Controller) architecture, providing developers with a fast and flexible development experience while maintaining high performance.
Main Features and Advantages of Phalcon
- High Performance: Since Phalcon is implemented as a C extension, it offers outstanding performance.
- Rich Features: Includes functionality such as ORM, template engine, routing, caching, queues, and more to meet various development needs.
- Low Memory Usage: Due to its unique implementation, Phalcon consumes very little memory at runtime.
- Easy to Use: Offers a straightforward API and clear documentation to help developers get started quickly.
- Modular Design: Allows you to choose and use different components as needed, offering high flexibility.
Phalcon is an ideal choice for building high-performance web applications and APIs, suitable for projects ranging from small applications to large enterprise systems.
Create and Run a Phalcon Project Using ServBay
In this article, we will use the PHP environment provided by ServBay to create and run a Phalcon project. We will utilize ServBay's "Host" feature to set up a web server and achieve project access through simple configurations.
Note: If you were an NGINX or Apache user
ServBay uses Caddy as the default web server. For users migrating from NGINX and Apache to ServBay, there are some key changes to be aware of:
Caddy Configuration
ServBay comes with Caddy pre-configured and optimized. Developers only need to manage sites through ServBay’s "Host" feature without manually editing Caddy configuration files.
Rewrite rules and .htaccess
In NGINX and Apache, developers usually write their own rewrite rules and .htaccess files to handle URL rewriting and other settings. However, ServBay comes pre-configured with Caddy rules. Unless special needs arise, developers do not need to write these rules themselves.
Learn More
For more information, please refer to Rewrite and .htaccess, How to Migrate an Apache Website to ServBay, and How to Migrate an NGINX Website to ServBay.
Note: Enable the Phalcon Module
ServBay has the Phalcon module built-in. Users need to enable it and restart PHP. Please refer to How to Enable the Built-in Phalcon Module in ServBay for instructions on enabling the Phalcon module.
Phalcon and DevTools Versions
Depending on the PHP version, you need to install different versions of Phalcon DevTools:
- PHP 5.6, 7.0, 7.1: Phalcon 3.4.5
- Corresponding Phalcon DevTools version:
3.4.x
- Corresponding Phalcon DevTools version:
- PHP 7.2, 7.3, 7.4: Phalcon 4.1.2
- Corresponding Phalcon DevTools version:
4.3.x
- Corresponding Phalcon DevTools version:
- PHP 8.0, 8.1, 8.2, 8.3, 8.4: Phalcon 5.7.0
- Corresponding Phalcon DevTools version:
5.0.x
(Since official DevTools support for PHP 8.x is not good, it is recommended to use the fixed version)
- Corresponding Phalcon DevTools version:
Create a Phalcon Project
TIP
ServBay recommends that developers place websites in the /Applications/ServBay/www
directory for easier management.
Install Composer
ServBay has Composer built-in, so no separate installation is needed.
Create Project Directory
Create a project directory and enter it:
bashcd /Applications/ServBay/www mkdir servbay-phalcon-app cd servbay-phalcon-app
1
2
3Install Phalcon DevTools
Install the corresponding Phalcon DevTools according to the PHP version:
PHP 5.6, 7.0, 7.1:
bashcomposer require phalcon/devtools:"^3.4"
1PHP 7.2, 7.3, 7.4:
bashcomposer require phalcon/devtools:"~4.1"
1PHP 8.0, 8.1, 8.2, 8.3, 8.4:
Create a
composer.json
file and enter the following content:json{ "repositories": [ { "url": "https://github.com/daleffe/phalcon-devtools-5.x-fixed.git", "type": "git" } ], "require": { "phalcon/devtools": "dev-master" }, "minimum-stability": "dev", "prefer-stable": true }
1
2
3
4
5
6
7
8
9
10
11
12
13Then run:
bashcomposer update
1Create Phalcon Project
Use Phalcon DevTools to create a new Phalcon project:
bashvendor/bin/phalcon project servbay-phalcon-app
1Enter Project Directory
Enter the newly created Phalcon project directory:
bashcd servbay-phalcon-app
1
Initialize Configuration
Configure Environment Variables
Configure database connection information and other environment variables in the
app/config/config.php
file. Ensure the following configurations are set correctly:phpreturn new \Phalcon\Config([ 'database' => [ 'adapter' => 'Mysql', 'host' => '127.0.0.1', 'username' => 'root', 'password' => 'password', 'dbname' => 'servbay_phalcon_app', ], ]);
1
2
3
4
5
6
7
8
9
Configure Web Server
Use ServBay's "Host" feature to access the Phalcon project through the web server. In ServBay's "Host" settings, add a new host:
- Name:
My First Phalcon Dev Site
- Domain:
servbay-phalcon-test.local
- Website Type:
PHP
- PHP Version: Select the corresponding PHP version
- Website Root Directory:
/Applications/ServBay/www/servbay-phalcon-app/servbay-phalcon-app/public
For detailed setup steps, please refer to Adding the First Website.
Add Example Code
In the app/config/routes.php
file, add the following code to output "Hello ServBay!":
$router->add(
'/',
[
'controller' => 'index',
'action' => 'index',
]
);
2
3
4
5
6
7
In the app/controllers/IndexController.php
file, add the following code:
namespace App\Controllers;
use Phalcon\Mvc\Controller;
class IndexController extends Controller
{
public function indexAction()
{
echo 'Hello ServBay!';
}
}
2
3
4
5
6
7
8
9
10
11
Access the Website
Open your browser and visit https://servbay-phalcon-test.local
, and you should see the page output Hello ServBay!
.
NoSQL Database Example
Memcached Example
Install Memcached Extension
Memcached extension is pre-installed in ServBay, so no additional installation is needed.
Configure Memcached
Add Memcached connection information in the
app/config/config.php
file:phpreturn new \Phalcon\Config([ 'database' => [ 'adapter' => 'Mysql', 'host' => '127.0.0.1', 'username' => 'root', 'password' => 'password', 'dbname' => 'servbay_phalcon_app', ], 'cache' => [ 'adapter' => 'Memcached', 'host' => '127.0.0.1', 'port' => 11211, ], ]);
1
2
3
4
5
6
7
8
9
10
11
12
13
14Configure Routing
Add the following code in
app/config/routes.php
:php$router->add( '/memcached', [ 'controller' => 'index', 'action' => 'memcached', ] );
1
2
3
4
5
6
7Use Memcached
Use cache in the controller:
phpnamespace App\Controllers; use Phalcon\Mvc\Controller; use Phalcon\Cache\Backend\Memcached; use Phalcon\Cache\Frontend\Data as FrontData; class IndexController extends Controller { public function memcachedAction() { $frontCache = new FrontData([ 'lifetime' => 86400 ]); $cache = new Memcached($frontCache, [ 'host' => '127.0.0.1', 'port' => 11211, ]); $cacheKey = 'my_cache_key'; $cachedData = $cache->get($cacheKey); if ($cachedData === null) { $cachedData = 'Hello Memcached!'; $cache->save($cacheKey, $cachedData); } echo $cachedData; } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30Open your browser and visit
https://servbay-phalcon-test.local/memcached
Redis Example
Install Redis Extension
Redis extension is pre-installed in ServBay, so no additional installation is needed.
Configure Redis
Add Redis connection information in the
app/config/config.php
file:phpreturn new \Phalcon\Config([ 'database' => [ 'adapter' => 'Mysql', 'host' => '127.0.0.1', 'username' => 'root', 'password' => 'password', 'dbname' => 'servbay_phalcon_app', ], 'cache' => [ 'adapter' => 'Redis', 'host' => '127.0.0.1', 'port' => 6379, ], ]);
1
2
3
4
5
6
7
8
9
10
11
12
13
14Configure Routing
Add the following code in
app/config/routes.php
:php$router->add( '/redis', [ 'controller' => 'index', 'action' => 'redis', ] );
1
2
3
4
5
6
7Use Redis
Use cache in the controller:
phpnamespace App\Controllers; use Phalcon\Mvc\Controller; use Phalcon\Cache\Backend\Redis; use Phalcon\Cache\Frontend\Data as FrontData; class IndexController extends Controller { public function redisAction() { $frontCache = new FrontData([ 'lifetime' => 86400 ]); $cache = new Redis($frontCache, [ 'host' => '127.0.0.1', 'port' => 6379, ]); $cacheKey = 'my_cache_key'; $cachedData = $cache->get($cacheKey); if ($cachedData was null) { $cachedData = 'Hello Redis!'; $cache saves the cacheKey, the cachedData); } echo $cachedData; } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30Open your browser and visit
https://servbay-phalcon-test.local/redis
Relational Database Example
Create Database Structure and Migration Files
Create Migration Files
Use Phalcon DevTools to create migration files:
bashvendor/bin/phalcon migration generate --directory=servbay-phalcon-app
1Edit Migration Files
In the
migrations
directory, create a new migration file and edit it to define the database table structure:phpuse Phalcon\Db\Column; use Phalcon\Db\Index; use Phalcon\Migrations\Mvc\Model\Migration; class UsersMigration_100 extends Migration { public function morph() { $this->morphTable('users', [ 'columns' => [ new Column( 'id', [ 'type' => Column::TYPE_INTEGER, 'autoIncrement' => true, 'notNull' => true, 'primary' => true, ] ), new Column( 'name', [ 'type' => Column::TYPE_VARCHAR, 'size' => 255, 'notNull' => true, ] ), new Column( 'email', [ 'type' => Column::TYPE_VARCHAR, 'size' => 255, 'notNull' => true, 'unique' => true, ] ), ], 'indexes' => [ new Index('PRIMARY', ['id'], 'PRIMARY'), new Index('email_UNIQUE', ['email'], 'UNIQUE'), ], ]); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44Run Migration
Use Phalcon DevTools command to run the migration and create the database table:
bashvendor/bin/phalcon migration run --directory=servbay-phalcon-app
1
MySQL Example
Configure MySQL
Configure MySQL connection information in the
app/config/config.php
file:phpreturn new \Phalcon\Config([ 'database' => [ 'adapter' => 'Mysql', 'host' => '127.0.0.1', 'username' => 'root', 'password' => 'password', 'dbname' => 'servbay_phalcon_app', ], ]);
1
2
3
4
5
6
7
8
9Configure Routing
Add the following code in
app/config/routes.php
:php$router->add( '/mysql-add', [ 'controller' => 'index', 'action' => 'mysqlAdd', ] ); $router->add( '/mysql', [ 'controller' => 'index', 'action' => 'mysql', ] );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15Insert User Data
Insert user data in the controller:
phpnamespace App\Controllers; use Phalcon\Mvc\Controller; use Phalcon\Db\Adapter\Pdo\Mysql; class IndexController extends Controller { public function mysqlAddAction() { $connection = new Mysql([ 'host' => '127.0.0.1', 'username' => 'root', 'password' => 'password', 'dbname' => 'servbay_phalcon_app', ]); $success = $connection->insert( 'users', ['ServBay', '[email protected]'], ['name', 'email'] ); echo $success ? 'User added' : 'Failed to add user'; } public function mysqlAction() { $connection = new Mysql([ 'host' => '127.0.0.1', 'username' => 'root', 'password' => 'password', 'dbname' => 'servbay_phalcon_app', ]); $users = $connection->fetchAll('SELECT * FROM users', \Phalcon\Db\Enum::FETCH_ASSOC); echo json_encode($users); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39Open your browser and visit
https://servbay-phalcon-test.local/mysql-add
andhttps://servbay-phalcon-test.local/mysql
PostgreSQL Example
Configure PostgreSQL
Configure PostgreSQL connection information in the
app/config/config.php
file:phpreturn new \Phalcon\Config([ 'database' => [ 'adapter' => 'Postgresql', 'host' => '127.0.0.1', 'username' => 'root', 'password' => 'password', 'dbname' => 'servbay_phalcon_app', ], ]);
1
2
3
4
5
6
7
8
9Configure Routing
Add the following code in
app/config/routes.php
:php$router->add( '/pgsql-add', [ 'controller' => 'index', 'action' => 'pgsqlAdd', ] ); $router->add( '/pgsql', [ 'controller' => 'index', 'action' => 'pgsql', ] );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15Insert User Data
Insert user data in the controller:
phpnamespace App\Controllers; use Phalcon\Mvc\Controller; use Phalcon\Db\Adapter\Pdo\Postgresql; class IndexController extends Controller { public function pgsqlAddAction() { $connection = new Postgresql([ 'host' => '127.0.0.1', 'username' => 'root', 'password' => 'password', 'dbname' => 'servbay_phalcon_app', ]); $success = $connection->insert( 'users', ['ServBay', '[email protected]'], ['name', 'email'] ); echo $success ? 'User added' : 'Failed to add user'; } public function pgsqlAction() { $connection = new Postgresql([ 'host' => '127.0.0.1', 'username' => 'root', 'password' => 'password', 'dbname' => 'servbay_phalcon_app', ]); $users = $connection->fetchAll('SELECT * FROM users', \Phalcon\Db\Enum::FETCH_ASSOC); echo json_encode($users); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39Open your browser and visit
https://servbay-phalcon-test.local/pgsql-add
andhttps://servbay-phalcon-test.local/pgsql
Through the above steps, you have successfully created and run a Phalcon project, used the features provided by ServBay to manage and access your project, connected to various databases, and called data.