Phalcon4框架入门(踩坑)
问题描述
拿Phalcon 官方的小例子入门Phalcon时,发现一个很奇怪的问题:涉及到 models 的操作均无法正常执行。models下面定义了一个 Users 类,IndexController 中的 indexAction 使用了 Users::find() 方法,SignupController 中的registerAction创建了一个 Users 类型的对象。
浏览器中访问 localhost/index与 lcoalhost/signup/register时都报错502 Bad Gateway。并且signup中只要U大写,localhost/signup便不能正常访问。(Linux区分大小写所致)
环境:
Ubuntu 20.04.2 LTS
php 7.4.3 (通过apt安装,ubuntu官方源)
nginx 1.18.0
php 7.4-fpm及扩展若干 (包括php-pear)
phalcon4 (通过添加apt source安装)
phalcon4采用phalcon官方给出的Ubuntu安装方案
原因查找
查日志
NGINX: recv() failed (104: Connection reset by peer) while reading response header from upstream
PHP-FPM: WARNING: [pool www] child xxxx exited with code 1 after 0.xxxxx seconds from start
child异常退出导致连接重置,可见问题不在NGINX,而在PHP这边。
这时联想到在安装Phalcon时,最初使用pecl安装失败(php-pear版本1.10.9),频繁报错
Notice: Trying to access array offset on value of type bool in PEAR/REST.php on line 187
PHP Notice: Trying to access array offset on value of type bool in /usr/share/php/PEAR/REST.php on line 187
此条报错不影响正常运行,安装新版pear即可解决,见 “解决方案” “2” 中的"# 安装新版pear"
重装Phalcon4试试吧
解决方案
重新安装Phalcon
1. 清除旧版本PHP
如果php是从PPA安装的,若要卸载可参考另一篇文章。
apt purge php*
apt autopurge
apt clean
apt update
apt install php及其需要的php组件
2. 安装依赖
推 荐 操 作
sudo apt install php-psr
如果上述操作依然报错,可尝试如下:
此方案为手动编译。较为耗时
sudo apt install php7.4-xml php7.4-dev
######################################
# 安装新版pear
sudo apt purge php-pear php-psr
wget https://pear.php.net/go-pear.phar
sudo php go-pear.phar
sudo pecl channel-update pecl.php.net
######################################
sudo pecl uninstall PSR
sudo pecl install PSR
编译完成后,在配置文件中添加psr
vim /etc/php/7.4/mods-available/psr.ini
The PSR extension is required to be loaded before Phalcon. Please ensure that it is available in your system
添加如下内容
; priority=15
[psr]
extension = psr.so
3. 安装Phalcon4
参考:https://packagecloud.io/phalcon/stable-41/install
curl -s https://packagecloud.io/install/repositories/phalcon/stable-41/script.deb.sh | sudo bash
sudo apt install php7.4-phalcon
# 进行第四步即可
# 不推荐此种方式
也可采用pecl编译安装(推荐方式),注意选中版本
sudo pecl install http://pecl.php.net/get/phalcon-4.1.2.tgz
最后在配置文件/etc/php/7.4/mods-available/phalcon.ini
中添加相应内容
; This file is part of the Phalcon.
;
; (c) Phalcon Team <team@phalcon.io>
;
; For the full copyright and license information, please view
; the LICENSE.txt file that was distributed with this source code.
;
; If you did not receive a copy of the license it is available
; through the world-wide-web at the following url:
; https://license.phalcon.io
; configuration for php phalcon module
; priority=50
[phalcon]
extension = phalcon.so
; ----- Options to use the Phalcon Framework
; phalcon.db.escape_identifiers = On
; phalcon.db.force_casting = Off
; phalcon.orm.events = On
; phalcon.orm.virtual_foreign_keys = On
; phalcon.orm.column_renaming = On
; phalcon.orm.not_null_validations = On
; phalcon.orm.exception_on_failed_save = Off
; phalcon.orm.enable_literals = On
; phalcon.orm.late_state_binding = Off
; phalcon.orm.enable_implicit_joins = On
; phalcon.orm.cast_on_hydrate = Off
; phalcon.orm.ignore_unknown_columns = Off
; phalcon.orm.update_snapshot_on_save = On
; phalcon.orm.disable_assign_setters = Off
; phalcon.orm.resultset_prefetch_records = Off
; phalcon.orm.cast_last_insert_id_to_int = Off
4. 启用扩展
sudo phpenmod -v 7.4 -s ALL psr phalcon
5. 测试问题是否出现
经测试,请求正常响应
P A S S
总结反思
多参考 官方教程 和 github issue 中的解决方案
善用搜索引擎