我有一个项目在 phalcon 5.3.0 上运行。但是在使用 php 8.2 升级到 phalcon 5.3.1 后,我遇到了一些错误。并且无法修复它。我没有做任何改变,但发生了错误。另外我没有得到任何与 phalcon 5.3.1 中的路由器/请求相关的更改记录。请告诉我我做错了什么?
[错误]
A dependency injection container is required to access the 'request' servicePhalcon\Mvc\Router\Exception:: A dependency injection container is required to access the 'request' service File=phalcon/Mvc/Router.zep Line=724 Trace=Array Previous= Line=0 #0 /var/www/html/f/apps/config/routes.php(52): Phalcon\Mvc\Router->handle()
#1 [internal function]: Closure->{closure}()
#2 [internal function]: Phalcon\Di\Service->resolve()
#3 [internal function]: Phalcon\Di\Di->get()
#4 [internal function]: Phalcon\Di\Di->getShared()
#5 /var/www/html/f/public/index.php(46): Phalcon\Mvc\Application->handle()
#6 {main}
[路由器]
use Phalcon\Mvc\Router;
$di->set('router', function(){
$router = new Router();
#
$router->setDefaultModule('front');
$router->setDefaultNamespace('F\Front\Controllers');
$router->setDefaultController('index');
$router->setDefaultAction('index');
# Remove Extra Trailing Slashes
$router->removeExtraSlashes(true);
# FRONTEND
$router->add('/f/', array(
'module' => 'front',
'namespace' => 'F\Front\Controllers',
'controller' => 1,
'action' => 2,
'params' => 3,
))->setName('front-default');
# BACKEND
$router->add('/back', [
'module' => 'back',
'namespace' => 'F\Back\Controllers',
'controller' => 'index',
'action' => 'index'
])->setName('back-default');
$router->add('/back/:controller/:action/:params', [
'module' => 'back',
'namespace' => 'F\Back\Controllers',
'controller' => 1,
'action' => 2,
'params' => 3
])->setName('back-full');
$router->add('/back/:controller/:action', [
'module' => 'back',
'namespace' => 'F\Back\Controllers',
'controller' => 1,
'action' => 2,
])->setName('back-short');
$router->add('/back/:controller', [
'module' => 'back',
'namespace' => 'F\Back\Controllers',
'controller' => 1
])->setName('backend-mini');
#
$uri = str_replace($_SERVER["SCRIPT_NAME"], "", $_SERVER["REQUEST_URI"]);
$router->handle($uri);
return $router;
});
[引导程序]
<?php declare(strict_types=1);
error_reporting(E_ALL);
setlocale(LC_ALL, 'en_US.UTF-8');
mb_internal_encoding('UTF-8');
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Application;
(new \Phalcon\Support\Debug())->listen();
defined('BASE_PATH') || define('BASE_PATH', dirname(__DIR__));
defined('APP_PATH') || define('APP_PATH', BASE_PATH . '/apps');
$di = new FactoryDefault();
require_once(APP_PATH . '/config/routes.php');
$app = new Application($di);
$app->registerModules(
array(
'front' => array(
'className' => \F\Front\Module::class,
'path' => APP_PATH . '/front/Module.php',
),
'back' => array(
'className' => \F\Back\Module::class,
'path' => APP_PATH . '/back/Module.php',
)
)
);
try {
$response = $app->handle(substr($_SERVER["REQUEST_URI"], strlen('/f/')-1));
$response->send();
} catch (\Exception $e) {
echo($e->getMessage());
echo get_class($e),':: ',$e->getMessage(), PHP_EOL;
echo " File=", $e->getFile(), PHP_EOL;
echo " Line=", $e->getLine(), PHP_EOL;
echo " Trace=", $e->getTrace(), PHP_EOL;
echo " Previous=", $e->getPrevious(), PHP_EOL;
echo " Line=", $e->getCode(), PHP_EOL;
echo nl2br(htmlentities($e->getTraceAsString()));
}
我相信 Phalcon 5.3.1 更新中的“修复 Router::handle() #16431 中的内存泄漏”导致了混乱。请参阅:https://github.com/phalcon/cphalcon/pull/16431 幸运的是,解决方案非常简单:
use Phalcon\Mvc\Router;
use Phalcon\Di\FactoryDefault;
$di->set('router', function() {
$router = new Router();
$router->setDI(new FactoryDefault());
// ...