Cách kích hoạt mô-đun Phalcon tích hợp sẵn của ServBay
Là một công cụ phát triển Web tích hợp mạnh mẽ, ServBay có mô-đun Phalcon tích hợp sẵn và quy trình kích hoạt rất đơn giản. Phalcon là một khung PHP hiệu suất cao, thiết kế mô-đun của nó giúp các nhà phát triển dễ dàng xây dựng các ứng dụng web hiệu quả.
Giới thiệu về khung Phalcon
Phalcon là một khung PHP mã nguồn mở và đầy đủ tính năng, nổi tiếng về tốc độ và tiêu thụ tài nguyên thấp. Khác với các khung PHP truyền thống, Phalcon được viết bằng C và tải như một tiện ích mở rộng của PHP. Điều này giúp Phalcon có lợi thế rõ ràng về hiệu suất, đồng thời cung cấp bộ tính năng phong phú.
Đặc điểm chính
- Hiệu suất cao: Vì Phalcon chạy dưới dạng tiện ích mở rộng C, nó nhanh hơn các khung thực hiện bằng PHP thuần túy và tiêu thụ tài nguyên ít hơn.
- Chi phí thấp: Thiết kế kiến trúc của Phalcon giúp nó chiếm dụng ít bộ nhớ và tài nguyên CPU khi xử lý yêu cầu.
- Đầy đủ tính năng: Phalcon cung cấp cấu trúc MVC hoàn chỉnh, ORM, động cơ mẫu, định tuyến, bộ nhớ đệm, hàng đợi và nhiều tính năng khác, đáp ứng mọi nhu cầu phát triển web.
- Dễ sử dụng: Mặc dù Phalcon được viết bằng C, các nhà phát triển có thể sử dụng cú pháp PHP quen thuộc để phát triển, giảm chi phí học tập.
- Thiết kế mô-đun: Các thành phần của Phalcon rất mô-đun, các nhà phát triển có thể chọn sử dụng các thành phần khác nhau tùy theo nhu cầu.
Phiên bản mô-đun Phalcon tích hợp sẵn của ServBay
ServBay hỗ trợ nhiều phiên bản PHP và cho mỗi phiên bản được cài đặt sẵn mô-đun Phalcon tương ứng. Các phiên bản cụ thể như sau:
- 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
Cách kích hoạt mô-đun Phalcon
Mặc định mô-đun Phalcon bị vô hiệu hóa. Để kích hoạt mô-đun Phalcon, chỉ cần chỉnh sửa tệp cấu hình của phiên bản PHP tương ứng. Các bước cụ thể như sau:
Bước một: Tìm tệp cấu hình
Đầu tiên, định vị thư mục conf.d
của phiên bản PHP tương ứng. Ví dụ, để kích hoạt mô-đun Phalcon cho PHP 8.3, chúng ta cần chỉnh sửa tệp sau:
/Applications/ServBay/etc/php/8.3/conf.d/phalcon.ini
Bước hai: Chỉnh sửa tệp cấu hình
Mở tệp phalcon.ini
và bỏ chú thích dòng sau:
[Phalcon]
; Uncomment the following line to enable Phalcon
extension = phalcon.so
2
3
Bước ba: Kích hoạt mô-đun PSR (chỉ áp dụng cho PHP 7.2 - PHP 7.4)
Đối với các phiên bản PHP 7.2 đến PHP 7.4, cần tải thêm mô-đun PSR (ServBay đã bao gồm):
; Uncomment both Psr & Phalcon to enable Phalcon
[Psr]
extension = psr.so
[Phalcon]
extension = phalcon.so
2
3
4
5
Bước bốn: Khởi động lại dịch vụ PHP
Trong bảng điều khiển quản lý dịch vụ của ServBay, khởi động lại dịch vụ PHP tương ứng. Ví dụ, khởi động lại dịch vụ PHP 8.3. Sau khi khởi động lại hoàn tất, mô-đun Phalcon sẽ được tải thành công.
Kiểm tra mô-đun Phalcon đã được tải thành công chưa
Có thể tạo một tệp PHP đơn giản để kiểm tra mô-đun Phalcon đã được tải thành công hay chưa. Trong thư mục gốc của máy chủ web, tạo một tệp phpinfo.php
với nội dung sau:
<?php
phpinfo();
?>
2
3
Truy cập https://servbay.host/phpinfo.php
, trong trang thông tin PHP đầu ra tìm thông tin liên quan đến mô-đun Phalcon. Nếu thấy thông tin liên quan đến Phalcon, nghĩa là mô-đun đã được tải thành công.
Ví dụ sử dụng
Sau khi kích hoạt mô-đun Phalcon, có thể sử dụng khung Phalcon trong mã PHP để phát triển web. Dưới đây là một ví dụ đơn giản cho thấy cách sử dụng Phalcon để tạo một ứng dụng web cơ bản:
Mã ví dụ
- Tạo cấu trúc thư mục dự án
phalcon
├── app
│ ├── controllers
│ │ └── IndexController.php
│ └── views
│ └── index
│ └── index.phtml
└── public
└── index.php
2
3
4
5
6
7
8
9
- Bộ điều khiển phía trước: 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 {
// Xử lý yêu cầu
$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
- Bộ điều khiển: app/controllers/IndexController.php
<?php
use Phalcon\Mvc\Controller;
use Phalcon\Support\Version;
class IndexController extends Controller
{
public function indexAction()
{
// Lấy phiên bản Phalcon
$phalconVersion = new Version();
// Truyền phiên bản tới 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
Kết luận
ServBay cung cấp một cách thuận tiện để quản lý và kích hoạt mô-đun Phalcon. Bằng cách cấu hình và khởi động lại đơn giản, các nhà phát triển có thể nhanh chóng kích hoạt mô-đun Phalcon trong các phiên bản PHP khác nhau, từ đó tận dụng hiệu suất cao và tính năng phong phú của nó, nâng cao hiệu quả phát triển ứng dụng web. Hiệu suất cao, chi phí thấp và đầy đủ tính năng của Phalcon làm cho nó trở thành lựa chọn hàng đầu để xây dựng các ứng dụng web hiệu quả và mở rộng.