How to Enable the Built-in Phalcon Module in ServBay
As a powerful integrated web development tool, ServBay comes with the Phalcon module, and the enabling process is very simple. Phalcon, being a high-performance PHP framework, allows developers to easily build efficient web applications with its modular design.
Introduction to the Phalcon Framework
Phalcon is an open-source, full-featured PHP framework known for its speed and low resource consumption. Unlike traditional PHP frameworks, Phalcon is written in C and loaded as a PHP extension, providing significant performance advantages along with a rich feature set.
Main Features
- High Performance: Running as a C extension, Phalcon is faster and consumes fewer resources than frameworks implemented purely in PHP.
- Low Overhead: Phalcon's architecture ensures minimal memory and CPU usage when handling requests.
- Rich Features: Phalcon offers a complete MVC structure, ORM, template engine, routing, caching, queues, and more to meet various web development needs.
- Easy to Use: Despite being written in C, developers can use familiar PHP syntax, reducing the learning curve.
- Modular Design: Phalcon's components are highly modular, allowing developers to choose and use different components as needed.
Versions of the Built-in Phalcon Module in ServBay
ServBay supports multiple PHP versions, with pre-installed Phalcon modules for each version:
- 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. The steps to enable the Phalcon module are straightforward; you just need to modify the configuration file for the corresponding PHP version. Here are the detailed steps:
Step 1: Find the Configuration File
First, locate the conf.d
directory for the corresponding PHP version. For example, 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 (Applicable Only for PHP 7.2 - PHP 7.4)
For PHP 7.2 to PHP 7.4, also load the PSR module (ServBay includes it):
; Uncomment both Psr & Phalcon to enable Phalcon
[Psr]
extension = psr.so
[Phalcon]
extension = phalcon.so
2
3
4
5
Step 4: Restart the PHP Service
In the ServBay service management panel, restart the respective PHP service. For instance, restart the PHP 8.3 service. After the restart, the Phalcon module will be successfully loaded.
Verify Phalcon Module is Loaded Successfully
You can create a simple PHP file to verify if the Phalcon module is successfully loaded. Create a phpinfo.php
file in the web server's root directory with the following content:
<?php
phpinfo();
?>
2
3
Access https://servbay.host/phpinfo.php
and check the PHP information page for Phalcon-related information. If you see Phalcon details, the module has been successfully loaded.
Usage Example
After enabling the Phalcon module, you can use the Phalcon framework in your PHP code to develop web applications. Here is a simple example showing how to create a basic web application using 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. By following simple configuration and restart steps, developers can quickly enable the Phalcon module in different PHP versions, fully utilizing its high performance and rich functionality to enhance web application development efficiency. With its high performance, low overhead, and rich features, Phalcon becomes a top choice for building efficient and scalable web applications.