How to Enable the Built-in Phalcon Module in ServBay
As a powerful integrated web development tool, ServBay comes with a built-in Phalcon module, and the activation process is very straightforward. Phalcon, known for its high performance, allows developers to easily build efficient web applications due to its modular design.
Introduction to the Phalcon Framework
Phalcon is an open-source, full-featured PHP framework renowned for its speed and low resource usage. Unlike traditional PHP frameworks, Phalcon is written in C and loaded as a PHP extension. This gives Phalcon a significant performance advantage while providing a rich set of features.
Main Features
- High Performance: Since Phalcon runs as a C extension, it's faster and consumes fewer resources compared to pure PHP frameworks.
- Low Overhead: Phalcon's architecture is designed to use less memory and CPU resources when handling requests.
- Rich Features: Provides a complete MVC structure, ORM, template engine, routing, caching, queues, etc., catering to various web development needs.
- Ease of Use: Although written in C, developers can use familiar PHP syntax, reducing the learning curve.
- Modular Design: Phalcon components are highly modular, allowing developers to choose different components according to their needs.
Versions of Phalcon Module Pre-installed in ServBay
ServBay supports multiple PHP versions, with pre-installed Phalcon modules for each version as follows:
- PHP 5.6, 7.0, 7.1: Phalcon 3.4.5
- PHP 7.2, 7.3, 7.4: Phalcon 4.1.2
- PHP 8.0, 8.1, 8.2, 8.3, 8.4: Phalcon 5.7.0
How to Enable the Phalcon Module
By default, the Phalcon module is disabled. Enabling it is simple: navigate to Language
- PHP
, select the PHP version where you want to enable the module, such as PHP 8.4
, click on Extensions
on the right, and toggle the switch next to the Phalcon
module, then save the changes.
Users can also manually enable or modify module configurations as detailed below.
Step 1: Locate the Configuration File
First, navigate to the conf.d
directory of the corresponding PHP version. For instance, to enable the Phalcon module for PHP 8.3, edit the following file:
/Applications/ServBay/etc/php/8.3/conf.d/phalcon.ini
Step 2: Edit the Configuration File
Open the phalcon.ini
file and uncomment the following line:
[Phalcon]
; Uncomment the following line to enable Phalcon
extension = phalcon.so
2
3
Step 3: Enable the PSR Module (Only for PHP 7.2 - PHP 7.4)
For PHP versions 7.2 to 7.4, you also need to load the PSR module (already included in ServBay):
; Uncomment both Psr & Phalcon to enable Phalcon
[Psr]
extension = psr.so
[Phalcon]
extension = phalcon.so
2
3
4
5
Step 4: Restart PHP Services
In ServBay's service management panel, restart the corresponding PHP service, for example, PHP 8.3. After the restart, the Phalcon module will be successfully loaded.
Verify if the Phalcon Module is Successfully Loaded
To verify if the Phalcon module is loaded successfully, you can create a simple PHP file. Create a phpinfo.php
file in the root directory of your web server with the following content:
<?php
phpinfo();
?>
2
3
Visit https://servbay.host/phpinfo.php
and look for Phalcon module information on the PHP info page. Seeing Phalcon's details confirms successful loading.
Example Usage
Once the Phalcon module is enabled, you can use it in your PHP code for web development. Here's a simple example of creating a basic web application with Phalcon:
Example Code
- Create Project Directory Structure
phalcon
├── app
│ ├── controllers
│ │ └── IndexController.php
│ └── views
│ └── index
│ └── index.phtml
└── public
└── index.php
2
3
4
5
6
7
8
9
- Front Controller: public/index.php
<?php
use Phalcon\Autoload\Loader;
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url;
use Phalcon\Mvc\Application;
define('BASE_PATH', dirname(__DIR__ . '/../../'));
define('APP_PATH', BASE_PATH . '/app');
$loader = new Loader();
$loader->setDirectories(
[
APP_PATH . '/controllers/',
APP_PATH . '/models/',
]
);
$loader->register();
$container = new FactoryDefault();
$container->set(
'view',
function () {
$view = new View();
$view->setViewsDir(APP_PATH . '/views/');
return $view;
}
);
$container->set(
'url',
function () {
$url = new Url();
$url->setBaseUri('/');
return $url;
}
);
$application = new Application($container);
try {
// Handle the request
$response = $application->handle(
$_SERVER["REQUEST_URI"]
);
$response->send();
} catch (\Exception $e) {
echo 'Exception: ', $e->getMessage();
}
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
44
45
46
47
48
49
50
51
52
53
54
55
- Controller: app/controllers/IndexController.php
<?php
use Phalcon\Mvc\Controller;
use Phalcon\Support\Version;
class IndexController extends Controller
{
public function indexAction()
{
// Get Phalcon version
$phalconVersion = new Version();
// Pass the version to the view
$this->view->phalconVersion = $phalconVersion->get();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- View: app/views/index/index.phtml
<!DOCTYPE html>
<html>
<head>
<title>Hello ServBay</title>
</head>
<body>
<h1>Hello ServBay!</h1>
<p>Phalcon Version: <?= $phalconVersion ?></p>
</body>
</html>
2
3
4
5
6
7
8
9
10
Conclusion
ServBay provides a convenient way to manage and enable the Phalcon module. With simple configuration and restart operations, developers can quickly enable Phalcon modules across different PHP versions, taking full advantage of its high performance and rich features to enhance web application development efficiency. Phalcon's high performance, low overhead, and rich features make it an excellent choice for building efficient, scalable web applications.